Skip to content

Commit 25a27a7

Browse files
committed
More string-wrapper docs.
1 parent 2ffad87 commit 25a27a7

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "string-wrapper"
3-
version = "0.1.3"
3+
version = "0.1.4"
44
authors = ["Simon Sapin <[email protected]>"]
55
license = "MIT / Apache-2.0"
66
repository = "https://github.com/SimonSapin/rust-std-candidates"

lib.rs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@ pub unsafe trait Buffer {
2222
}
2323

2424
impl<T> StringWrapper<T> where T: Buffer {
25+
/// Create an empty string from its backing storage.
2526
pub fn new(buffer: T) -> Self {
2627
StringWrapper {
2728
len: 0,
2829
buffer: buffer,
2930
}
3031
}
3132

33+
/// Unsafely create a string from its components.
34+
///
3235
/// Users must ensure that:
3336
///
3437
/// * The buffer length is at least `len`
@@ -40,46 +43,58 @@ impl<T> StringWrapper<T> where T: Buffer {
4043
}
4144
}
4245

46+
/// Consume the string and return the backing storage.
4347
pub fn into_buffer(self) -> T {
4448
self.buffer
4549
}
4650

51+
/// View the backing storage as a bytes slice.
4752
pub fn buffer(&self) -> &[u8] {
4853
self.buffer.as_ref()
4954
}
5055

51-
/// Users must ensure that:
56+
57+
/// View the backing storage as a bytes slice.
5258
///
53-
/// * The buffer length does not shrink below `self.len()`
54-
/// * The prefix up to `self.len()` remains well-formed UTF-8.
59+
/// Users must ensure that the prefix bytes up to `self.len()` remains well-formed UTF-8.
5560
pub unsafe fn buffer_mut(&mut self) -> &mut [u8] {
5661
self.buffer.as_mut()
5762
}
5863

64+
/// Return the number of bytes in the string.
5965
pub fn len(&self) -> usize {
6066
self.len
6167
}
6268

69+
/// Return whether the string contains no bytes.
6370
pub fn is_empty(&self) -> bool {
6471
self.len == 0
6572
}
6673

74+
/// Unsafely change the length in bytes of the string.
75+
///
6776
/// Users must ensure that the string remains well-formed UTF-8.
6877
pub unsafe fn set_len(&mut self, new_len: usize) {
6978
self.len = new_len
7079
}
7180

81+
/// Shortens a string to the specified length.
82+
///
83+
/// Panics if `new_len` > current length, or if `new_len` is not a character boundary.
7284
pub fn truncate(&mut self, new_len: usize) {
85+
assert!(new_len <= self.len);
7386
if new_len < self.len {
7487
assert!(starts_well_formed_utf8_sequence(self.buffer.as_ref()[new_len]));
75-
self.len = new_len;
7688
}
89+
self.len = new_len;
7790
}
7891

92+
/// Return the maximum number of bytes the string can hold.
7993
pub fn capacity(&self) -> usize {
8094
self.buffer.as_ref().len()
8195
}
8296

97+
/// Return by how many bytes the string can grow.
8398
pub fn extra_capacity(&self) -> usize {
8499
self.capacity() - self.len
85100
}
@@ -89,6 +104,9 @@ impl<T> StringWrapper<T> where T: Buffer {
89104
&mut self.buffer.as_mut()[self.len..]
90105
}
91106

107+
/// Append a code point to the string if the extra capacity is sufficient.
108+
///
109+
/// Return `Ok` with the code point appended, or `Err` with the string unchanged.
92110
pub fn push(&mut self, c: char) -> Result<(), ()> {
93111
let new_len = self.len + c.len_utf8();
94112
if new_len <= self.capacity() {
@@ -101,16 +119,19 @@ impl<T> StringWrapper<T> where T: Buffer {
101119
}
102120
}
103121

104-
/// Panics if `s.len() <= self.extra_capacity()`
122+
/// Append a string slice to the string.
123+
///
124+
/// Panics if the extra capacity is not sufficient.
105125
pub fn push_str(&mut self, s: &str) {
106126
copy_memory(s.as_bytes(), self.extra_bytes_mut());
107127
self.len += s.len();
108128
}
109129

110-
/// Append as much as possible of the given string
111-
/// (within 3 bytes of `self.extra_capacity()`)
112-
/// Return `Ok(())` if the capacity was sufficient,
113-
/// or `Err(number_of_bytes_written)`.
130+
/// Append as much as possible of a string slice to the string.
131+
///
132+
/// Return `Ok(())` if the extra capacity was sufficient,
133+
/// or `Err(n)` where `n` is the number of bytes pushed.
134+
/// `n` is within 3 bytes of the extra capacity.
114135
pub fn push_partial_str(&mut self, s: &str) -> Result<(), usize> {
115136
let mut i = self.extra_capacity();
116137
let (s, result) = if i < s.len() {

0 commit comments

Comments
 (0)