Skip to content

Commit

Permalink
doc: add more examples for fallible methods
Browse files Browse the repository at this point in the history
  • Loading branch information
bluurryy committed Oct 7, 2024
1 parent ae89f9d commit 3b6e2e2
Show file tree
Hide file tree
Showing 8 changed files with 720 additions and 104 deletions.
62 changes: 56 additions & 6 deletions src/bump_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,11 +444,11 @@ where
do panics
/// Panics if `idx` is larger than the `String`'s length, or if it does not
/// lie on a [`char`] boundary.
impl
do examples
/// ```
/// # use bump_scope::{ Bump, BumpString };
/// # let bump: Bump = Bump::new();
/// #
/// let mut s = BumpString::with_capacity_in(3, &bump);
///
/// s.insert(0, 'f');
Expand All @@ -457,8 +457,21 @@ where
///
/// assert_eq!("foo", s);
/// ```
impl
for fn insert
do examples
/// ```
/// # #![cfg_attr(feature = "nightly-allocator-api", feature(allocator_api))]
/// # use bump_scope::{ Bump, BumpString };
/// # let bump: Bump = Bump::try_new()?;
/// let mut s = BumpString::try_with_capacity_in(3, &bump)?;
///
/// s.try_insert(0, 'f')?;
/// s.try_insert(1, 'o')?;
/// s.try_insert(2, 'o')?;
///
/// assert_eq!("foo", s);
/// # Ok::<(), bump_scope::allocator_api2::alloc::AllocError>(())
/// ```
for fn try_insert
use fn generic_insert(&mut self, idx: usize, ch: char) {
assert!(self.is_char_boundary(idx));
Expand All @@ -477,19 +490,30 @@ where
do panics
/// Panics if `idx` is larger than the `BumpString`'s length, or if it does not
/// lie on a [`char`] boundary.
impl
do examples
/// ```
/// # use bump_scope::{ Bump, BumpString };
/// # let bump: Bump = Bump::new();
/// #
/// let mut s = BumpString::from_str_in("bar", &bump);
///
/// s.insert_str(0, "foo");
///
/// assert_eq!("foobar", s);
/// ```
impl
for fn insert_str
do examples
/// ```
/// # #![cfg_attr(feature = "nightly-allocator-api", feature(allocator_api))]
/// # use bump_scope::{ Bump, BumpString };
/// # let bump: Bump = Bump::try_new()?;
/// let mut s = BumpString::try_from_str_in("bar", &bump)?;
///
/// s.try_insert_str(0, "foo")?;
///
/// assert_eq!("foobar", s);
/// # Ok::<(), bump_scope::allocator_api2::alloc::AllocError>(())
/// ```
for fn try_insert_str
use fn generic_insert_str(&mut self, idx: usize, string: &str) {
assert!(self.is_char_boundary(idx));
Expand All @@ -503,11 +527,11 @@ where
do panics
/// Panics if the starting point or end point do not lie on a [`char`]
/// boundary, or if they're out of bounds.
impl
do examples
/// ```
/// # use bump_scope::{ Bump, BumpString };
/// # let bump: Bump = Bump::new();
/// #
/// let mut string = BumpString::from_str_in("abcde", &bump);
///
/// string.extend_from_within(2..);
Expand All @@ -519,8 +543,24 @@ where
/// string.extend_from_within(4..8);
/// assert_eq!(string, "abcdecdeabecde");
/// ```
impl
for fn extend_from_within
do examples
/// ```
/// # #![cfg_attr(feature = "nightly-allocator-api", feature(allocator_api))]
/// # use bump_scope::{ Bump, BumpString };
/// # let bump: Bump = Bump::try_new()?;
/// let mut string = BumpString::try_from_str_in("abcde", &bump)?;
///
/// string.try_extend_from_within(2..)?;
/// assert_eq!(string, "abcdecde");
///
/// string.try_extend_from_within(..2)?;
/// assert_eq!(string, "abcdecdeab");
///
/// string.try_extend_from_within(4..8)?;
/// assert_eq!(string, "abcdecdeabecde");
/// # Ok::<(), bump_scope::allocator_api2::alloc::AllocError>(())
/// ```
for fn try_extend_from_within
use fn generic_extend_from_within<{R}>(&mut self, src: R)
where {
Expand All @@ -545,6 +585,16 @@ where
/// assert_eq!(string, "What?\0\0\0");
/// ```
for fn extend_zeroed
do examples
/// ```
/// # #![cfg_attr(feature = "nightly-allocator-api", feature(allocator_api))]
/// # use bump_scope::{ Bump, BumpString };
/// # let bump: Bump = Bump::try_new()?;
/// let mut string = BumpString::try_from_str_in("What?", &bump)?;
/// string.try_extend_zeroed(3)?;
/// assert_eq!(string, "What?\0\0\0");
/// # Ok::<(), bump_scope::allocator_api2::alloc::AllocError>(())
/// ```
for fn try_extend_zeroed
use fn generic_extend_zeroed(&mut self, additional: usize) {
self.generic_reserve(additional)?;
Expand Down
Loading

0 comments on commit 3b6e2e2

Please sign in to comment.