bytes-text 0.1.0

Reference-counted UTF-8 text
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
use std::{
    borrow::Borrow,
    convert::TryFrom,
    fmt::{Debug, Display},
    hash::Hash,
    ops::{Deref, RangeBounds},
    str::Utf8Error,
};

use bytes::Bytes;

use crate::TextMut;

/// Immutable, reference counted, UTF-8 text
///
/// # Example
///
/// ```
/// # use bytes_text::Text;
/// # use bytes::{Bytes, Buf};
/// let mut s = String::from("Text, woo!");
///
/// let text = Text::from(s);
/// assert_eq!(text, "Text, woo!");
/// let (a, b) = text.split_at(5).unwrap();
/// assert_eq!(a, "Text,");
/// assert_eq!(b, " woo!");
/// ```
#[derive(Default, Clone)]
pub struct Text(Bytes);

impl Text {
    /// Creates a new, empty, text buffer.
    ///
    /// # Example
    ///
    /// ```
    /// # use bytes_text::Text;
    /// let mut text = Text::new();
    /// assert!(text.is_empty());
    /// ```
    pub fn new() -> Self {
        Self(Bytes::new())
    }

    /// Converts `Bytes` to `Text`.
    ///
    /// # Example
    ///
    /// ```
    /// # use bytes_text::Text;
    /// # use bytes::{Bytes, Buf};
    /// let mut buf = Bytes::from_static(&[
    ///     0x69, 0x27, 0x6d, 0x20, 0x69, 0x6e, 0x20, 0x61,
    ///     0x20, 0x62, 0x75, 0x66, 0x66, 0x65, 0x72, 0x21
    /// ]);
    ///
    /// let text = Text::from_utf8(buf).unwrap();
    /// assert_eq!(text, "i'm in a buffer!");
    /// ```
    pub fn from_utf8(b: Bytes) -> Result<Self, Utf8Error> {
        // run utf-8 validation
        let _ = std::str::from_utf8(b.as_ref())?;
        Ok(Self(b))
    }

    /// Converts `Bytes` to `Text` without verifying that it's valid UTF-8
    ///
    /// # Safety
    ///
    /// Must be valid UTF-8
    ///
    /// # Example
    ///
    /// ```
    /// # use bytes_text::Text;
    /// # use bytes::{Bytes, Buf};
    /// let mut buf = Bytes::from_static(&[
    ///     0x69, 0x27, 0x6d, 0x20, 0x69, 0x6e, 0x20, 0x61,
    ///     0x20, 0x62, 0x75, 0x66, 0x66, 0x65, 0x72, 0x21
    /// ]);
    ///
    /// let text = unsafe { Text::from_utf8_unchecked(buf) };
    /// assert_eq!(text, "i'm in a buffer!");
    /// ```
    #[inline]
    pub const unsafe fn from_utf8_unchecked(b: Bytes) -> Self {
        Self(b)
    }

    /// Copies the provided string into a new buffer.
    ///
    /// # Example
    ///
    /// ```
    /// # use bytes_text::Text;
    /// let mut s = format!("the answer is: {}", 42);
    /// let text = Text::copy_from(s);
    /// assert_eq!(text, "the answer is: 42");
    /// ```
    pub fn copy_from(s: impl AsRef<str>) -> Self {
        // copy the bytes and wrap it
        // guaranteed to be valid
        Self(Bytes::copy_from_slice(s.as_ref().as_bytes()))
    }

    /// Creates `Text` from a static `str`
    ///
    /// # Example
    ///
    /// ```
    /// # use bytes_text::Text;
    /// let text = Text::from_static("Static!");
    /// ```
    /// Can also use `from`
    /// ```
    /// # use bytes_text::Text;
    /// let text = Text::from("Also static!");
    /// ```
    pub const fn from_static(s: &'static str) -> Self {
        Self(Bytes::from_static(s.as_bytes()))
    }

    /// The number of bytes in this text
    ///
    /// ```
    /// # use bytes_text::Text;
    /// let text = Text::from_static("Hello!");
    /// assert_eq!(text.len(), 6);
    /// ```
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Checks if this text is empty
    ///
    /// # Example
    ///
    /// ```
    /// # use bytes_text::Text;
    /// let text = Text::new();
    /// assert!(text.is_empty());
    /// ```
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Get a reference to the inner bytes
    ///
    /// # Example
    ///
    /// ```
    /// # use bytes_text::Text;
    /// # use bytes::Bytes;
    /// let text = Text::from("Woah");
    /// let bytes: &Bytes = text.as_bytes();
    /// assert_eq!(bytes, &b"Woah"[..])
    /// ```
    pub const fn as_bytes(&self) -> &Bytes {
        &self.0
    }

    /// Convert into bytes
    ///
    /// # Example
    ///
    /// ```
    /// # use bytes_text::Text;
    /// # use bytes::Bytes;
    /// let text = Text::from("Woah");
    /// let bytes: Bytes = text.into_bytes();
    /// assert_eq!(&bytes, &b"Woah"[..])
    /// ```
    pub fn into_bytes(self) -> Bytes {
        self.0
    }

    /// Get a sub-body of text
    ///
    /// # Example
    ///
    /// ```
    /// # use bytes_text::Text;
    /// # use bytes::Bytes;
    /// let text = Text::from("Hi, I'm some text!");
    /// let end = text.get(4..).unwrap();
    /// assert_eq!(end, "I'm some text!");
    /// let middle = text.get(8..12).unwrap();
    /// assert_eq!(middle, "some");
    /// ```
    pub fn get(&self, r: impl RangeBounds<usize>) -> Option<Text> {
        let start = match r.start_bound() {
            std::ops::Bound::Included(&i) => i,
            std::ops::Bound::Excluded(&i) => i.checked_add(1)?,
            std::ops::Bound::Unbounded => 0,
        };
        let end = match r.end_bound() {
            std::ops::Bound::Included(&i) => i.checked_add(1)?,
            std::ops::Bound::Excluded(&i) => i,
            std::ops::Bound::Unbounded => self.len(),
        };
        // str::is_char_boundary returns false if the index is out of bounds,
        // so there's no need to check for it here
        soft_assert::soft_assert!(self.is_char_boundary(start) && self.is_char_boundary(end));
        Some(Self(self.0.slice(start..end)))
    }

    /// Splits the text into two halves
    ///
    /// Returns `Err(self)` if the index is not a valid char boundary
    ///
    /// # Example
    ///
    /// ```
    /// # use bytes_text::Text;
    /// let text = Text::from("Woo, split!");
    /// let (a, b) = text.split_at(4).unwrap();
    /// assert_eq!(a, "Woo,");
    /// assert_eq!(b, " split!");
    /// ```
    pub fn split_at(mut self, index: usize) -> Result<(Self, Self), Self> {
        soft_assert::soft_assert!(self.is_char_boundary(index), Err(self));
        let right = self.0.split_off(index);
        Ok((Self(self.0), Self(right)))
    }

    /// Splits the text into two halves, `self` being the start half and
    /// returning the end half
    ///
    /// Returns `None` if the index is not a valid char boundary. If this
    /// returns `None`, `self` remains unchanged.
    ///
    /// # Example
    ///
    /// ```
    /// # use bytes_text::Text;
    /// let mut text = Text::from("Woo, split!");
    /// let end = text.split_off(4).unwrap();
    /// assert_eq!(text, "Woo,");
    /// assert_eq!(end, " split!");
    /// ```
    pub fn split_off(&mut self, index: usize) -> Option<Self> {
        soft_assert::soft_assert!(self.is_char_boundary(index));
        let right = self.0.split_off(index);
        Some(Self(right))
    }

    /// Splits the text into two halves, `self` being the end half and
    /// returning the start half
    ///
    /// Returns `None` if the index is not a valid char boundary. If this
    /// returns `None`, `self` remains unchanged.
    ///
    /// # Example
    ///
    /// ```
    /// # use bytes_text::Text;
    /// let mut text = Text::from("Woo, split!");
    /// let start = text.split_to(4).unwrap();
    /// assert_eq!(start, "Woo,");
    /// assert_eq!(text, " split!");
    /// ```
    pub fn split_to(&mut self, index: usize) -> Option<Self> {
        soft_assert::soft_assert!(self.is_char_boundary(index));
        let right = self.0.split_to(index);
        Some(Self(right))
    }

    fn as_str(&self) -> &str {
        unsafe { std::str::from_utf8_unchecked(self.0.as_ref()) }
    }
}

// ## Conversions

impl AsRef<str> for Text {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl Borrow<str> for Text {
    fn borrow(&self) -> &str {
        self.as_str()
    }
}

impl Deref for Text {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        self.as_str()
    }
}

impl From<&'static str> for Text {
    fn from(s: &'static str) -> Self {
        Self::from_static(s)
    }
}

impl From<String> for Text {
    fn from(s: String) -> Self {
        Self(Bytes::from(s.into_bytes()))
    }
}

impl TryFrom<Bytes> for Text {
    type Error = Utf8Error;

    fn try_from(b: Bytes) -> Result<Self, Self::Error> {
        Self::from_utf8(b)
    }
}

impl From<Text> for Bytes {
    fn from(t: Text) -> Self {
        t.into_bytes()
    }
}

// ## Formatting

impl Display for Text {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Display::fmt(self.as_str(), f)
    }
}

impl Debug for Text {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        Debug::fmt(self.as_str(), f)
    }
}

// ## Comparisons

// ### Self comparisons

impl PartialEq for Text {
    fn eq(&self, other: &Self) -> bool {
        (&**self).eq(&**other)
    }
}

impl Eq for Text {}

impl PartialEq<&Text> for Text {
    fn eq(&self, other: &&Text) -> bool {
        (&**self).eq(&***other)
    }
}

impl PartialEq<&mut Text> for Text {
    fn eq(&self, other: &&mut Text) -> bool {
        (&**self).eq(&***other)
    }
}

impl PartialOrd for Text {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Text {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        (&**self).cmp(&**other)
    }
}
// ### str comparisons

impl PartialEq<str> for Text {
    fn eq(&self, other: &str) -> bool {
        (&**self).eq(other)
    }
}

impl PartialEq<&str> for Text {
    fn eq(&self, other: &&str) -> bool {
        (&**self).eq(*other)
    }
}

impl PartialEq<&mut str> for Text {
    fn eq(&self, other: &&mut str) -> bool {
        (&**self).eq(*other)
    }
}

impl PartialOrd<str> for Text {
    fn partial_cmp(&self, other: &str) -> Option<std::cmp::Ordering> {
        (&**self).partial_cmp(other)
    }
}

impl PartialOrd<&str> for Text {
    fn partial_cmp(&self, other: &&str) -> Option<std::cmp::Ordering> {
        (&**self).partial_cmp(*other)
    }
}

impl PartialOrd<&mut str> for Text {
    fn partial_cmp(&self, other: &&mut str) -> Option<std::cmp::Ordering> {
        (&**self).partial_cmp(*other)
    }
}

// ### String comparisons

impl PartialEq<String> for Text {
    fn eq(&self, other: &String) -> bool {
        (&**self).eq(other)
    }
}

impl PartialEq<&String> for Text {
    fn eq(&self, other: &&String) -> bool {
        (&**self).eq(*other)
    }
}

impl PartialEq<&mut String> for Text {
    fn eq(&self, other: &&mut String) -> bool {
        (&**self).eq(*other)
    }
}

impl PartialOrd<String> for Text {
    fn partial_cmp(&self, other: &String) -> Option<std::cmp::Ordering> {
        (&**self).partial_cmp(&**other)
    }
}

impl PartialOrd<&String> for Text {
    fn partial_cmp(&self, other: &&String) -> Option<std::cmp::Ordering> {
        (&**self).partial_cmp(&***other)
    }
}

impl PartialOrd<&mut String> for Text {
    fn partial_cmp(&self, other: &&mut String) -> Option<std::cmp::Ordering> {
        (&**self).partial_cmp(&***other)
    }
}

// ### TextMut Comparisons

impl PartialEq<TextMut> for Text {
    fn eq(&self, other: &TextMut) -> bool {
        (&**self).eq(&**other)
    }
}

impl PartialEq<&TextMut> for Text {
    fn eq(&self, other: &&TextMut) -> bool {
        (&**self).eq(&***other)
    }
}

impl PartialEq<&mut TextMut> for Text {
    fn eq(&self, other: &&mut TextMut) -> bool {
        (&**self).eq(&***other)
    }
}

impl PartialOrd<TextMut> for Text {
    fn partial_cmp(&self, other: &TextMut) -> Option<std::cmp::Ordering> {
        (&**self).partial_cmp(&**other)
    }
}

impl PartialOrd<&TextMut> for Text {
    fn partial_cmp(&self, other: &&TextMut) -> Option<std::cmp::Ordering> {
        (&**self).partial_cmp(&***other)
    }
}

impl PartialOrd<&mut TextMut> for Text {
    fn partial_cmp(&self, other: &&mut TextMut) -> Option<std::cmp::Ordering> {
        (&**self).partial_cmp(&***other)
    }
}

// ## Hash

impl Hash for Text {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        (&**self).hash(state);
    }
}