Skip to content

Commit c021fb0

Browse files
committed
Add examples to docs
Closes nrc#1 Signed-off-by: Nick Cameron <nrc@ncameron.org>
1 parent c3a0885 commit c021fb0

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed

src/lib.rs

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
//! This module includes functionality to transform an `OwnedBuf` from and to a `Vec<u8>` or
4242
//! `Vec<MaybeUninit<u8>>`. `OwnedBuf` is designed to be usable with any owned sequence of bytes. To
4343
//! 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.
4646
//!
4747
//! An `OwnedBuf`'s destructor function has type `&'static dyn Fn(&mut OwnedBuf<A>)`, it passes a
4848
//! mutable reference to the destructor, however, it is guaranteed that the `OwnedBuf` will not be
@@ -52,8 +52,41 @@
5252
//!
5353
//! # Examples
5454
//!
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+
//! ```
5790
5891
#![feature(maybe_uninit_slice)]
5992
#![feature(maybe_uninit_write_slice)]
@@ -154,6 +187,18 @@ impl OwnedBuf {
154187
Vec::from_raw_parts(data, filled, capacity)
155188
}
156189

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+
157202
/// Returns the length of the initialized part of the buffer.
158203
#[inline]
159204
pub fn init_len(&self) -> usize {

0 commit comments

Comments
 (0)