@@ -22,13 +22,16 @@ pub unsafe trait Buffer {
22
22
}
23
23
24
24
impl < T > StringWrapper < T > where T : Buffer {
25
+ /// Create an empty string from its backing storage.
25
26
pub fn new ( buffer : T ) -> Self {
26
27
StringWrapper {
27
28
len : 0 ,
28
29
buffer : buffer,
29
30
}
30
31
}
31
32
33
+ /// Unsafely create a string from its components.
34
+ ///
32
35
/// Users must ensure that:
33
36
///
34
37
/// * The buffer length is at least `len`
@@ -40,46 +43,58 @@ impl<T> StringWrapper<T> where T: Buffer {
40
43
}
41
44
}
42
45
46
+ /// Consume the string and return the backing storage.
43
47
pub fn into_buffer ( self ) -> T {
44
48
self . buffer
45
49
}
46
50
51
+ /// View the backing storage as a bytes slice.
47
52
pub fn buffer ( & self ) -> & [ u8 ] {
48
53
self . buffer . as_ref ( )
49
54
}
50
55
51
- /// Users must ensure that:
56
+
57
+ /// View the backing storage as a bytes slice.
52
58
///
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.
55
60
pub unsafe fn buffer_mut ( & mut self ) -> & mut [ u8 ] {
56
61
self . buffer . as_mut ( )
57
62
}
58
63
64
+ /// Return the number of bytes in the string.
59
65
pub fn len ( & self ) -> usize {
60
66
self . len
61
67
}
62
68
69
+ /// Return whether the string contains no bytes.
63
70
pub fn is_empty ( & self ) -> bool {
64
71
self . len == 0
65
72
}
66
73
74
+ /// Unsafely change the length in bytes of the string.
75
+ ///
67
76
/// Users must ensure that the string remains well-formed UTF-8.
68
77
pub unsafe fn set_len ( & mut self , new_len : usize ) {
69
78
self . len = new_len
70
79
}
71
80
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.
72
84
pub fn truncate ( & mut self , new_len : usize ) {
85
+ assert ! ( new_len <= self . len) ;
73
86
if new_len < self . len {
74
87
assert ! ( starts_well_formed_utf8_sequence( self . buffer. as_ref( ) [ new_len] ) ) ;
75
- self . len = new_len;
76
88
}
89
+ self . len = new_len;
77
90
}
78
91
92
+ /// Return the maximum number of bytes the string can hold.
79
93
pub fn capacity ( & self ) -> usize {
80
94
self . buffer . as_ref ( ) . len ( )
81
95
}
82
96
97
+ /// Return by how many bytes the string can grow.
83
98
pub fn extra_capacity ( & self ) -> usize {
84
99
self . capacity ( ) - self . len
85
100
}
@@ -89,6 +104,9 @@ impl<T> StringWrapper<T> where T: Buffer {
89
104
& mut self . buffer . as_mut ( ) [ self . len ..]
90
105
}
91
106
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.
92
110
pub fn push ( & mut self , c : char ) -> Result < ( ) , ( ) > {
93
111
let new_len = self . len + c. len_utf8 ( ) ;
94
112
if new_len <= self . capacity ( ) {
@@ -101,16 +119,19 @@ impl<T> StringWrapper<T> where T: Buffer {
101
119
}
102
120
}
103
121
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.
105
125
pub fn push_str ( & mut self , s : & str ) {
106
126
copy_memory ( s. as_bytes ( ) , self . extra_bytes_mut ( ) ) ;
107
127
self . len += s. len ( ) ;
108
128
}
109
129
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.
114
135
pub fn push_partial_str ( & mut self , s : & str ) -> Result < ( ) , usize > {
115
136
let mut i = self . extra_capacity ( ) ;
116
137
let ( s, result) = if i < s. len ( ) {
0 commit comments