|
41 | 41 | //! This module includes functionality to transform an `OwnedBuf` from and to a `Vec<u8>` or
|
42 | 42 | //! `Vec<MaybeUninit<u8>>`. `OwnedBuf` is designed to be usable with any owned sequence of bytes. To
|
43 | 43 | //! create an `OwnedBuf` use [`OwnedBuf::new`], you'll supply a data pointer, some metadata, and a
|
44 |
| -//! destructor function (see below). To transform from an `OwnedBuf`, you use FIXME(#9) to get the |
45 |
| -//! internal state of the `OwnedBuf` and then create the sequence type in the usual way. |
| 44 | +//! destructor function (see below). To transform from an `OwnedBuf`, you use `into_raw_parts` to |
| 45 | +//! get the internal state of the `OwnedBuf` and then create the sequence type in the usual way. |
46 | 46 | //!
|
47 | 47 | //! An `OwnedBuf`'s destructor function has type `&'static dyn Fn(&mut OwnedBuf<A>)`, it passes a
|
48 | 48 | //! mutable reference to the destructor, however, it is guaranteed that the `OwnedBuf` will not be
|
|
52 | 52 | //!
|
53 | 53 | //! # Examples
|
54 | 54 | //!
|
55 |
| -//! TODO |
56 |
| -//! create from Vec, write into it, read out of it, convert back to Vec, dtor |
| 55 | +//! Creating an `OwnedBuf` from a `Vec`. |
| 56 | +//! |
| 57 | +//! ``` |
| 58 | +//! # use owned_buf::OwnedBuf; |
| 59 | +//! let vec = vec![1u8, 2, 3]; |
| 60 | +//! let buf: OwnedBuf = vec.into(); |
| 61 | +//! |
| 62 | +//! // Use `filled` to view `buf` as an `OwnedSlice` to read from it. |
| 63 | +//! assert_eq!(1, buf.filled()[0]); |
| 64 | +//! |
| 65 | +//! // `Vec`'s destructor will be called via `OwnedBuf`'s. |
| 66 | +//! ``` |
| 67 | +//! |
| 68 | +//! Writing into an `OwnedBuf` and converting it back into a `Vec`. |
| 69 | +//! |
| 70 | +//! ``` |
| 71 | +//! # use owned_buf::OwnedBuf; |
| 72 | +//! let vec: Vec<u8> = Vec::with_capacity(8); |
| 73 | +//! let buf: OwnedBuf = vec.into(); |
| 74 | +//! assert_eq!(0, buf.filled_len()); |
| 75 | +//! assert_eq!(8, buf.capacity()); |
| 76 | +//! |
| 77 | +//! // Get a cursor to write into the `OwnedBuf`. |
| 78 | +//! let mut cursor = buf.unfilled(); |
| 79 | +//! cursor.write_slice(&[0, 1, 2, 3]); |
| 80 | +//! |
| 81 | +//! // Convert the cursor back into an `OwnedBuf`. |
| 82 | +//! let buf = cursor.into_buf(); |
| 83 | +//! assert_eq!(4, buf.filled_len()); |
| 84 | +//! |
| 85 | +//! let vec = unsafe { buf.into_vec() }; |
| 86 | +//! assert_eq!(4, vec.len()); |
| 87 | +//! assert_eq!(8, vec.capacity()); |
| 88 | +//! assert_eq!(3, vec[3]); |
| 89 | +//! ``` |
57 | 90 |
|
58 | 91 | #![feature(maybe_uninit_slice)]
|
59 | 92 | #![feature(maybe_uninit_write_slice)]
|
@@ -154,6 +187,18 @@ impl OwnedBuf {
|
154 | 187 | Vec::from_raw_parts(data, filled, capacity)
|
155 | 188 | }
|
156 | 189 |
|
| 190 | + /// Returns the total size of the buffer. |
| 191 | + #[inline] |
| 192 | + pub fn capacity(&self) -> usize { |
| 193 | + self.capacity |
| 194 | + } |
| 195 | + |
| 196 | + /// Returns the length of the filled part of the buffer. |
| 197 | + #[inline] |
| 198 | + pub fn filled_len(&self) -> usize { |
| 199 | + self.filled |
| 200 | + } |
| 201 | + |
157 | 202 | /// Returns the length of the initialized part of the buffer.
|
158 | 203 | #[inline]
|
159 | 204 | pub fn init_len(&self) -> usize {
|
|
0 commit comments