@@ -59,71 +59,6 @@ pub type String<const N: usize> = StringInner<OwnedVecStorage<u8, N>>;
59
59
/// A dynamic capacity [`String`](https://doc.rust-lang.org/std/string/struct.String.html).
60
60
pub type StringView = StringInner < ViewVecStorage < u8 > > ;
61
61
62
- impl StringView {
63
- /// Removes the specified range from the string in bulk, returning all
64
- /// removed characters as an iterator.
65
- ///
66
- /// The returned iterator keeps a mutable borrow on the string to optimize
67
- /// its implementation.
68
- ///
69
- /// # Panics
70
- ///
71
- /// Panics if the starting point or end point do not lie on a [`char`]
72
- /// boundary, or if they're out of bounds.
73
- ///
74
- /// # Leaking
75
- ///
76
- /// If the returned iterator goes out of scope without being dropped (due to
77
- /// [`core::mem::forget`], for example), the string may still contain a copy
78
- /// of any drained characters, or may have lost characters arbitrarily,
79
- /// including characters outside the range.
80
- ///
81
- /// # Examples
82
- ///
83
- /// ```
84
- /// use heapless::String;
85
- ///
86
- /// let mut s = String::<32>::try_from("α is alpha, β is beta").unwrap();
87
- /// let beta_offset = s.find('β').unwrap_or(s.len());
88
- ///
89
- /// // Remove the range up until the β from the string
90
- /// let t: String<32> = s.drain(..beta_offset).collect();
91
- /// assert_eq!(t, "α is alpha, ");
92
- /// assert_eq!(s, "β is beta");
93
- ///
94
- /// // A full range clears the string, like `clear()` does
95
- /// s.drain(..);
96
- /// assert_eq!(s, "");
97
- /// ```
98
- pub fn drain < R > ( & mut self , range : R ) -> Drain < ' _ >
99
- where
100
- R : RangeBounds < usize > ,
101
- {
102
- // Memory safety
103
- //
104
- // The `String` version of `Drain` does not have the memory safety issues
105
- // of the `Vec` version. The data is just plain bytes.
106
- // Because the range removal happens in `Drop`, if the `Drain` iterator is leaked,
107
- // the removal will not happen.
108
- let Range { start, end } = crate :: slice:: range ( range, ..self . len ( ) ) ;
109
- assert ! ( self . is_char_boundary( start) ) ;
110
- assert ! ( self . is_char_boundary( end) ) ;
111
-
112
- // Take out two simultaneous borrows. The &mut String won't be accessed
113
- // until iteration is over, in Drop.
114
- let self_ptr = self as * mut _ ;
115
- // SAFETY: `slice::range` and `is_char_boundary` do the appropriate bounds checks.
116
- let chars_iter = unsafe { self . get_unchecked ( start..end) } . chars ( ) ;
117
-
118
- Drain {
119
- start,
120
- end,
121
- iter : chars_iter,
122
- string : self_ptr,
123
- }
124
- }
125
- }
126
-
127
62
impl < const N : usize > String < N > {
128
63
/// Constructs a new, empty `String` with a fixed capacity of `N` bytes.
129
64
///
@@ -266,7 +201,9 @@ impl<const N: usize> String<N> {
266
201
pub fn into_bytes ( self ) -> Vec < u8 , N > {
267
202
self . vec
268
203
}
204
+ }
269
205
206
+ impl < S : VecStorage < u8 > + ?Sized > StringInner < S > {
270
207
/// Removes the specified range from the string in bulk, returning all
271
208
/// removed characters as an iterator.
272
209
///
@@ -306,11 +243,30 @@ impl<const N: usize> String<N> {
306
243
where
307
244
R : RangeBounds < usize > ,
308
245
{
309
- self . as_mut_view ( ) . drain ( range)
246
+ // Memory safety
247
+ //
248
+ // The `String` version of `Drain` does not have the memory safety issues
249
+ // of the `Vec` version. The data is just plain bytes.
250
+ // Because the range removal happens in `Drop`, if the `Drain` iterator is leaked,
251
+ // the removal will not happen.
252
+ let Range { start, end } = crate :: slice:: range ( range, ..self . len ( ) ) ;
253
+ assert ! ( self . is_char_boundary( start) ) ;
254
+ assert ! ( self . is_char_boundary( end) ) ;
255
+
256
+ // Take out two simultaneous borrows. The &mut String won't be accessed
257
+ // until iteration is over, in Drop.
258
+ let self_ptr = self . as_mut_view ( ) as * mut _ ;
259
+ // SAFETY: `slice::range` and `is_char_boundary` do the appropriate bounds checks.
260
+ let chars_iter = unsafe { self . get_unchecked ( start..end) } . chars ( ) ;
261
+
262
+ Drain {
263
+ start,
264
+ end,
265
+ iter : chars_iter,
266
+ string : self_ptr,
267
+ }
310
268
}
311
- }
312
269
313
- impl < S : VecStorage < u8 > + ?Sized > StringInner < S > {
314
270
/// Get a reference to the `String`, erasing the `N` const-generic.
315
271
///
316
272
///
0 commit comments