@@ -56,7 +56,7 @@ impl<const N: usize> TryFrom<&[u8]> for Bytes<N> {
56
56
impl < const N : usize > Bytes < N > {
57
57
/// Construct a new, empty `Bytes<N>`.
58
58
pub fn new ( ) -> Self {
59
- Self { bytes : Vec :: new ( ) }
59
+ Bytes :: from ( Vec :: new ( ) )
60
60
}
61
61
62
62
pub fn as_ptr ( & self ) -> * const u8 {
@@ -282,78 +282,6 @@ impl<const N: usize> Bytes<N> {
282
282
pub fn retain_mut ( & mut self , f : impl FnMut ( & mut u8 ) -> bool ) {
283
283
self . bytes . retain_mut ( f)
284
284
}
285
- }
286
-
287
- impl < const N : usize > Bytes < N > {
288
- // /// Some APIs offer an interface of the form `f(&mut [u8]) -> Result<usize, E>`,
289
- // /// with the contract that the Ok-value signals how many bytes were written.
290
- // ///
291
- // /// This constructor allows wrapping such interfaces in a more ergonomic way,
292
- // /// returning a Bytes willed using `f`.
293
- // ///
294
- // /// It seems it's not possible to do this as an actual `TryFrom` implementation.
295
- // pub fn from_constructor<E>(
296
- // f: impl FnOnce(&mut [u8]) -> core::result::Result<usize, E>,
297
- // ) -> core::result::Result<Self, E> {
298
- // let mut data = Self::new();
299
- // data.resize_to_capacity();
300
- // let result = f(&mut data);
301
-
302
- // result.map(|count| {
303
- // data.resize_default(count)
304
- // .expect("Contructor returned size larger than capacity");
305
- // data
306
- // })
307
- // }
308
-
309
- // cf. https://internals.rust-lang.org/t/add-vec-insert-slice-at-to-insert-the-content-of-a-slice-at-an-arbitrary-index/11008/3
310
- pub fn insert_slice_at ( & mut self , slice : & [ u8 ] , at : usize ) -> core:: result:: Result < ( ) , ( ) > {
311
- let l = slice. len ( ) ;
312
- let before = self . len ( ) ;
313
-
314
- // make space
315
- self . bytes . resize_default ( before + l) ?;
316
-
317
- // move back existing
318
- let raw: & mut [ u8 ] = & mut self . bytes ;
319
- raw. copy_within ( at..before, at + l) ;
320
-
321
- // insert slice
322
- raw[ at..] [ ..l] . copy_from_slice ( slice) ;
323
-
324
- Ok ( ( ) )
325
- }
326
-
327
- // pub fn insert(&mut self, index: usize, item: u8) -> Result<(), u8> {
328
- // self.insert_slice_at(&[item], index).map_err(|_| item)
329
- // }
330
-
331
- // pub fn remove(&mut self, index: usize) -> Result<u8, ()> {
332
- // if index < self.len() {
333
- // unsafe { Ok(self.remove_unchecked(index)) }
334
- // } else {
335
- // Err(())
336
- // }
337
- // }
338
-
339
- // pub(crate) unsafe fn remove_unchecked(&mut self, index: usize) -> u8 {
340
- // // the place we are taking from.
341
- // let p = self.bytes.as_mut_ptr().add(index);
342
-
343
- // // copy it out, unsafely having a copy of the value on
344
- // // the stack and in the vector at the same time.
345
- // let ret = ptr::read(p);
346
-
347
- // // shift everything down to fill in that spot.
348
- // ptr::copy(p.offset(1), p, self.len() - index - 1);
349
-
350
- // self.resize_default(self.len() - 1).unwrap();
351
- // ret
352
- // }
353
-
354
- pub fn resize_default ( & mut self , new_len : usize ) -> core:: result:: Result < ( ) , ( ) > {
355
- self . bytes . resize_default ( new_len)
356
- }
357
285
358
286
pub fn resize_to_capacity ( & mut self ) {
359
287
self . bytes . resize_default ( self . bytes . capacity ( ) ) . ok ( ) ;
0 commit comments