Skip to content

Commit ecd8f5a

Browse files
committed
Update documentation for string retain
1 parent 942e77f commit ecd8f5a

File tree

1 file changed

+37
-14
lines changed

1 file changed

+37
-14
lines changed

src/string.rs

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,33 @@ use core::{slice, str::from_utf8_unchecked_mut};
22

33
use alloc::string::String;
44

5-
/// Extension methods on [`String`].
5+
/// More advanced versions of [`String::retain`], implemented as extension
6+
/// methods on [`String`].
67
///
78
/// This trait is sealed and cannot be implemented for types outside of
8-
/// `retain_more`.
9+
/// `retain_more`
910
pub trait RetainMoreString: sealed::AllocMoreSealedString {
10-
/// A version of [`String::retain`] which allows the predicate mutable
11+
/// Retains only the characters specified by the predicate.
12+
///
13+
/// In other words, remove all characters for which `f` returns false. This
14+
/// method operates in place, visiting each character exactly once
15+
/// once in the original order, and preserves the order of the retained
16+
/// characters.
17+
///
18+
/// This version of [`String::retain`] allows the predicate mutable
1119
/// access to the valid parts of the full string.
1220
///
1321
/// The arguments of the predicate are:
14-
/// - 0: `&mut str`; The already retained parts of `self`, for which
15-
/// predicate returned `true`
16-
/// - 1: [`char`]; The current character being considered
22+
/// - 0: `&mut str`; Contents of `self` which have already been retained,
23+
/// i.e. those for which predicate has already returned `true`.
24+
/// - 1: [`char`]; The current character being considered.
1725
/// - 2: `&mut str`; The parts of `self` yet to be considered.
1826
///
1927
/// # Usage
2028
///
2129
/// ```
2230
/// # use retain_more::RetainMoreString as _;
23-
/// let mut my_string = "Super secret code: -100054321;-78912. EOF\
31+
/// let mut my_string = "Super secret code: -100054321-78912EOF\
2432
/// Here is some content which shouldn't be seen"
2533
/// .to_string();
2634
/// /// Remove all numbers from the string, including a single leading `'-'` and
@@ -36,12 +44,13 @@ pub trait RetainMoreString: sealed::AllocMoreSealedString {
3644
/// }
3745
/// }
3846
/// my_string.retain_all(cleanup);
39-
/// assert_eq!(&my_string, "Super secret code: ;. EOF");
47+
/// assert_eq!(&my_string, "Super secret code: EOF");
4048
/// ```
4149
fn retain_all<F: FnMut(&mut str, char, &mut str) -> bool>(&mut self, f: F);
4250

4351
/// A helper for the common case where only access to the parts of the
44-
/// [`String`] which haven't been considered are required
52+
/// [`String`] which haven't been considered yet is required, i.e. the
53+
/// predicate only uses arguments 1 and 2 from [`Self::retain_all`].
4554
fn retain_after<F: FnMut(char, &mut str) -> bool>(&mut self, mut f: F) {
4655
self.retain_all(move |_, current, after| f(current, after))
4756
}
@@ -53,8 +62,21 @@ pub trait RetainMoreString: sealed::AllocMoreSealedString {
5362
/// [`retain_all`](`RetainMoreString::retain_all`) is a strictly more
5463
/// powerful abstraction than [`String::retain`] from [`alloc`].
5564
///
65+
/// The predicate therefore only uses argument 1 from [`Self::retain_all`].
66+
///
67+
/// ## Standard retain docs
68+
///
69+
/// This documentation is taken from [`String::retain`] from [`alloc`].
70+
///
71+
/// Retains only the characters specified by the predicate.
72+
///
73+
/// In other words, remove all characters `c` such that `f(c)` returns
74+
/// false. This method operates in place, visiting each character exactly
75+
/// once in the original order, and preserves the order of the retained
76+
/// characters.
77+
///
5678
/// # Examples
57-
/// (Taken from alloc docs)
79+
///
5880
/// ```
5981
/// let mut s = String::from("f_o_ob_ar");
6082
///
@@ -131,9 +153,10 @@ impl RetainMoreString for String {
131153
// SAFETY: UTF-8 is maintained in the before section by only copying
132154
// a full character at a time.
133155
from_utf8_unchecked_mut(slice::from_raw_parts_mut(ptr, idx - del_bytes)),
134-
// SAFETY: idx + ch_len <= len because self, hence `idx + ch_len` is within the allocation of self.
135-
// was valid UTF-8 by invariant, hence after is valid.
136-
// This does not alias with `before`, because `-del_bytes < ch_len`
156+
// SAFETY: idx + ch_len <= len because self, hence `idx + ch_len` is within the
157+
// allocation of self. was valid UTF-8 by invariant, hence
158+
// after is valid. This does not alias with `before`,
159+
// because `-del_bytes < ch_len`
137160
from_utf8_unchecked_mut(slice::from_raw_parts_mut(
138161
ptr.add(idx + ch_len),
139162
len - idx - ch_len,
@@ -156,7 +179,7 @@ impl RetainMoreString for String {
156179
// 'Point' idx to the next char
157180
idx += ch_len;
158181
}
159-
// len-del_bytes <= len <= capacity
182+
// len - del_bytes <= len <= capacity
160183
unsafe {
161184
self.as_mut_vec().set_len(len - del_bytes);
162185
}

0 commit comments

Comments
 (0)