/rust/registry/src/index.crates.io-1949cf8c6b5b557f/bstr-1.13.0/src/bstring.rs
Line | Count | Source |
1 | | use alloc::vec::Vec; |
2 | | |
3 | | use crate::bstr::BStr; |
4 | | |
5 | | /// A wrapper for `Vec<u8>` that provides convenient string oriented trait |
6 | | /// impls. |
7 | | /// |
8 | | /// A `BString` has ownership over its contents and corresponds to |
9 | | /// a growable or shrinkable buffer. Its borrowed counterpart is a |
10 | | /// [`BStr`](struct.BStr.html), called a byte string slice. |
11 | | /// |
12 | | /// Using a `BString` is just like using a `Vec<u8>`, since `BString` |
13 | | /// implements `Deref` to `Vec<u8>`. So all methods available on `Vec<u8>` |
14 | | /// are also available on `BString`. |
15 | | /// |
16 | | /// # Examples |
17 | | /// |
18 | | /// You can create a new `BString` from a `Vec<u8>` via a `From` impl: |
19 | | /// |
20 | | /// ``` |
21 | | /// use bstr::BString; |
22 | | /// |
23 | | /// let s = BString::from("Hello, world!"); |
24 | | /// ``` |
25 | | /// |
26 | | /// # Deref |
27 | | /// |
28 | | /// The `BString` type implements `Deref` and `DerefMut`, where the target |
29 | | /// types are `&Vec<u8>` and `&mut Vec<u8>`, respectively. `Deref` permits all of the |
30 | | /// methods defined on `Vec<u8>` to be implicitly callable on any `BString`. |
31 | | /// |
32 | | /// For more information about how deref works, see the documentation for the |
33 | | /// [`std::ops::Deref`](https://doc.rust-lang.org/std/ops/trait.Deref.html) |
34 | | /// trait. |
35 | | /// |
36 | | /// # Representation |
37 | | /// |
38 | | /// A `BString` has the same representation as a `Vec<u8>` and a `String`. |
39 | | /// That is, it is made up of three word sized components: a pointer to a |
40 | | /// region of memory containing the bytes, a length and a capacity. |
41 | | #[derive(Clone)] |
42 | | #[repr(transparent)] |
43 | | pub struct BString { |
44 | | bytes: Vec<u8>, |
45 | | } |
46 | | |
47 | | impl BString { |
48 | | /// Constructs a new `BString` from the given [`Vec`]. |
49 | | /// |
50 | | /// # Examples |
51 | | /// |
52 | | /// ``` |
53 | | /// use bstr::BString; |
54 | | /// |
55 | | /// let mut b = BString::new(Vec::with_capacity(10)); |
56 | | /// ``` |
57 | | /// |
58 | | /// This function is `const`: |
59 | | /// |
60 | | /// ``` |
61 | | /// use bstr::BString; |
62 | | /// |
63 | | /// const B: BString = BString::new(vec![]); |
64 | | /// ``` |
65 | | #[inline] |
66 | 53.7M | pub const fn new(bytes: Vec<u8>) -> BString { |
67 | 53.7M | BString { bytes } |
68 | 53.7M | } <bstr::bstring::BString>::new Line | Count | Source | 66 | 1.18M | pub const fn new(bytes: Vec<u8>) -> BString { | 67 | 1.18M | BString { bytes } | 68 | 1.18M | } |
<bstr::bstring::BString>::new Line | Count | Source | 66 | 52.5M | pub const fn new(bytes: Vec<u8>) -> BString { | 67 | 52.5M | BString { bytes } | 68 | 52.5M | } |
<bstr::bstring::BString>::new Line | Count | Source | 66 | 17.8k | pub const fn new(bytes: Vec<u8>) -> BString { | 67 | 17.8k | BString { bytes } | 68 | 17.8k | } |
|
69 | | |
70 | | #[inline] |
71 | 2.67M | pub(crate) fn as_bytes(&self) -> &[u8] { |
72 | 2.67M | &self.bytes |
73 | 2.67M | } <bstr::bstring::BString>::as_bytes Line | Count | Source | 71 | 2.62M | pub(crate) fn as_bytes(&self) -> &[u8] { | 72 | 2.62M | &self.bytes | 73 | 2.62M | } |
<bstr::bstring::BString>::as_bytes Line | Count | Source | 71 | 43.0k | pub(crate) fn as_bytes(&self) -> &[u8] { | 72 | 43.0k | &self.bytes | 73 | 43.0k | } |
<bstr::bstring::BString>::as_bytes Line | Count | Source | 71 | 3.64k | pub(crate) fn as_bytes(&self) -> &[u8] { | 72 | 3.64k | &self.bytes | 73 | 3.64k | } |
|
74 | | |
75 | | #[inline] |
76 | 0 | pub(crate) fn as_bytes_mut(&mut self) -> &mut [u8] { |
77 | 0 | &mut self.bytes |
78 | 0 | } Unexecuted instantiation: <bstr::bstring::BString>::as_bytes_mut Unexecuted instantiation: <bstr::bstring::BString>::as_bytes_mut Unexecuted instantiation: <bstr::bstring::BString>::as_bytes_mut |
79 | | |
80 | | #[inline] |
81 | 70.7k | pub(crate) fn as_bstr(&self) -> &BStr { |
82 | 70.7k | BStr::new(&self.bytes) |
83 | 70.7k | } <bstr::bstring::BString>::as_bstr Line | Count | Source | 81 | 3.68k | pub(crate) fn as_bstr(&self) -> &BStr { | 82 | 3.68k | BStr::new(&self.bytes) | 83 | 3.68k | } |
<bstr::bstring::BString>::as_bstr Line | Count | Source | 81 | 21.4k | pub(crate) fn as_bstr(&self) -> &BStr { | 82 | 21.4k | BStr::new(&self.bytes) | 83 | 21.4k | } |
<bstr::bstring::BString>::as_bstr Line | Count | Source | 81 | 45.6k | pub(crate) fn as_bstr(&self) -> &BStr { | 82 | 45.6k | BStr::new(&self.bytes) | 83 | 45.6k | } |
|
84 | | |
85 | | #[inline] |
86 | 0 | pub(crate) fn as_mut_bstr(&mut self) -> &mut BStr { |
87 | 0 | BStr::new_mut(&mut self.bytes) |
88 | 0 | } Unexecuted instantiation: <bstr::bstring::BString>::as_mut_bstr Unexecuted instantiation: <bstr::bstring::BString>::as_mut_bstr Unexecuted instantiation: <bstr::bstring::BString>::as_mut_bstr |
89 | | |
90 | | #[inline] |
91 | 190M | pub(crate) fn as_vec(&self) -> &Vec<u8> { |
92 | 190M | &self.bytes |
93 | 190M | } <bstr::bstring::BString>::as_vec Line | Count | Source | 91 | 2.49M | pub(crate) fn as_vec(&self) -> &Vec<u8> { | 92 | 2.49M | &self.bytes | 93 | 2.49M | } |
<bstr::bstring::BString>::as_vec Line | Count | Source | 91 | 188M | pub(crate) fn as_vec(&self) -> &Vec<u8> { | 92 | 188M | &self.bytes | 93 | 188M | } |
Unexecuted instantiation: <bstr::bstring::BString>::as_vec |
94 | | |
95 | | #[inline] |
96 | 233M | pub(crate) fn as_vec_mut(&mut self) -> &mut Vec<u8> { |
97 | 233M | &mut self.bytes |
98 | 233M | } Unexecuted instantiation: <bstr::bstring::BString>::as_vec_mut <bstr::bstring::BString>::as_vec_mut Line | Count | Source | 96 | 233M | pub(crate) fn as_vec_mut(&mut self) -> &mut Vec<u8> { | 97 | 233M | &mut self.bytes | 98 | 233M | } |
Unexecuted instantiation: <bstr::bstring::BString>::as_vec_mut |
99 | | |
100 | | #[inline] |
101 | 0 | pub(crate) fn into_vec(self) -> Vec<u8> { |
102 | 0 | self.bytes |
103 | 0 | } Unexecuted instantiation: <bstr::bstring::BString>::into_vec Unexecuted instantiation: <bstr::bstring::BString>::into_vec Unexecuted instantiation: <bstr::bstring::BString>::into_vec |
104 | | |
105 | | #[inline] |
106 | 0 | pub(crate) fn from_vec_ref(v: &Vec<u8>) -> &BString { |
107 | | // SAFETY: `BString` is a `repr(transparent)` wrapper around `Vec<u8>`, and accepts any |
108 | | // `Vec<u8>` without validation. |
109 | | // |
110 | | // MSRV: Switch this to use `ptr::from_ref` once bstr can require at least Rust 1.72. |
111 | 0 | unsafe { &*(v as *const Vec<u8> as *const BString) } |
112 | 0 | } Unexecuted instantiation: <bstr::bstring::BString>::from_vec_ref Unexecuted instantiation: <bstr::bstring::BString>::from_vec_ref Unexecuted instantiation: <bstr::bstring::BString>::from_vec_ref |
113 | | |
114 | | #[inline] |
115 | 0 | pub(crate) fn from_vec_mut(v: &mut Vec<u8>) -> &mut BString { |
116 | | // SAFETY: `BString` is a `repr(transparent)` wrapper around `Vec<u8>`, and accepts any |
117 | | // `Vec<u8>` without validation. |
118 | | // |
119 | | // MSRV: Switch this to use `ptr::from_mut` once bstr can require at least Rust 1.72. |
120 | 0 | unsafe { &mut *(v as *mut Vec<u8> as *mut BString) } |
121 | 0 | } Unexecuted instantiation: <bstr::bstring::BString>::from_vec_mut Unexecuted instantiation: <bstr::bstring::BString>::from_vec_mut Unexecuted instantiation: <bstr::bstring::BString>::from_vec_mut |
122 | | } |