1 use crate::bstr::BStr; 2 3 /// A wrapper for `Vec<u8>` that provides convenient string oriented trait 4 /// impls. 5 /// 6 /// A `BString` has ownership over its contents and corresponds to 7 /// a growable or shrinkable buffer. Its borrowed counterpart is a 8 /// [`BStr`](struct.BStr.html), called a byte string slice. 9 /// 10 /// Using a `BString` is just like using a `Vec<u8>`, since `BString` 11 /// implements `Deref` to `Vec<u8>`. So all methods available on `Vec<u8>` 12 /// are also available on `BString`. 13 /// 14 /// # Examples 15 /// 16 /// You can create a new `BString` from a `Vec<u8>` via a `From` impl: 17 /// 18 /// ``` 19 /// use bstr::BString; 20 /// 21 /// let s = BString::from("Hello, world!"); 22 /// ``` 23 /// 24 /// # Deref 25 /// 26 /// The `BString` type implements `Deref` and `DerefMut`, where the target 27 /// types are `&Vec<u8>` and `&mut Vec<u8>`, respectively. `Deref` permits all of the 28 /// methods defined on `Vec<u8>` to be implicitly callable on any `BString`. 29 /// 30 /// For more information about how deref works, see the documentation for the 31 /// [`std::ops::Deref`](https://doc.rust-lang.org/std/ops/trait.Deref.html) 32 /// trait. 33 /// 34 /// # Representation 35 /// 36 /// A `BString` has the same representation as a `Vec<u8>` and a `String`. 37 /// That is, it is made up of three word sized components: a pointer to a 38 /// region of memory containing the bytes, a length and a capacity. 39 #[derive(Clone, Hash)] 40 pub struct BString { 41 pub(crate) bytes: Vec<u8>, 42 } 43 44 impl BString { 45 #[inline] 46 pub(crate) fn as_bytes(&self) -> &[u8] { 47 &self.bytes 48 } 49 50 #[inline] 51 pub(crate) fn as_bstr(&self) -> &BStr { 52 BStr::new(&self.bytes) 53 } 54 55 #[inline] 56 pub(crate) fn as_mut_bstr(&mut self) -> &mut BStr { 57 BStr::new_mut(&mut self.bytes) 58 } 59 } 60