Skip to content

Commit ed9c39e

Browse files
committed
Impl container traits on Cow
1 parent 3359f60 commit ed9c39e

1 file changed

Lines changed: 50 additions & 46 deletions

File tree

micropb/src/container.rs

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ mod impl_heapless {
353353
mod impl_alloc {
354354
use super::*;
355355

356-
use alloc::{collections::BTreeMap, string::String, vec::Vec};
356+
use alloc::{borrow::Cow, collections::BTreeMap, string::String, vec::Vec};
357357

358358
impl PbBytes for Vec<u8> {}
359359
impl PbString for Vec<u8> {
@@ -378,22 +378,28 @@ mod impl_alloc {
378378
}
379379
}
380380

381-
//impl<'a, T> PbContainer for alloc::borrow::Cow<'a, [T]>
382-
//where
383-
//[T]: ToOwned<Owned = Vec<T>>,
384-
//{
385-
//unsafe fn pb_set_len(&mut self, len: usize) {
386-
//self.to_mut().set_len(len);
387-
//}
381+
impl PbBytes for Cow<'_, [u8]> {}
382+
impl PbString for Cow<'_, [u8]> {
383+
#[inline]
384+
unsafe fn pb_set_len(&mut self, len: usize) {
385+
self.to_mut().set_len(len);
386+
}
388387

389-
//fn pb_clear(&mut self) {
390-
//self.to_mut().clear()
391-
//}
388+
#[inline]
389+
fn pb_clear(&mut self) {
390+
self.to_mut().clear()
391+
}
392392

393-
//fn pb_reserve(&mut self, additional: usize) {
394-
//self.to_mut().reserve(additional)
395-
//}
396-
//}
393+
#[inline]
394+
fn pb_reserve(&mut self, additional: usize) {
395+
self.to_mut().reserve(additional)
396+
}
397+
398+
#[inline]
399+
fn pb_spare_cap(&mut self) -> &mut [MaybeUninit<u8>] {
400+
self.to_mut().spare_capacity_mut()
401+
}
402+
}
397403

398404
impl PbString for String {
399405
#[inline]
@@ -418,19 +424,28 @@ mod impl_alloc {
418424
}
419425
}
420426

421-
//impl<'a> PbContainer for alloc::borrow::Cow<'a, str> {
422-
//unsafe fn pb_set_len(&mut self, len: usize) {
423-
//self.to_mut().as_mut_vec().set_len(len);
424-
//}
427+
impl PbString for Cow<'_, str> {
428+
#[inline]
429+
unsafe fn pb_set_len(&mut self, len: usize) {
430+
self.to_mut().as_mut_vec().set_len(len);
431+
}
425432

426-
//fn pb_clear(&mut self) {
427-
//self.to_mut().clear()
428-
//}
433+
#[inline]
434+
fn pb_clear(&mut self) {
435+
self.to_mut().clear()
436+
}
437+
438+
#[inline]
439+
fn pb_reserve(&mut self, additional: usize) {
440+
self.to_mut().reserve(additional)
441+
}
429442

430-
//fn pb_reserve(&mut self, additional: usize) {
431-
//self.to_mut().reserve(additional)
432-
//}
433-
//}
443+
#[inline]
444+
fn pb_spare_cap(&mut self) -> &mut [MaybeUninit<u8>] {
445+
// SAFETY: spare_capacity_mut() is a safe call, since it doesn't change any bytes
446+
unsafe { self.to_mut().as_mut_vec() }.spare_capacity_mut()
447+
}
448+
}
434449

435450
impl<T> PbVec<T> for Vec<T> {
436451
#[inline]
@@ -440,26 +455,15 @@ mod impl_alloc {
440455
}
441456
}
442457

443-
//impl<'a, T> PbVec<T> for alloc::borrow::Cow<'a, [T]>
444-
//where
445-
//[T]: ToOwned<Owned = Vec<T>>,
446-
//{
447-
//fn pb_push(&mut self, elem: T) -> Result<(), ()> {
448-
//self.to_mut().push(elem);
449-
//Ok(())
450-
//}
451-
452-
//fn pb_spare_cap(&mut self) -> &mut [MaybeUninit<T>] {
453-
//self.to_mut().spare_capacity_mut()
454-
//}
455-
//}
456-
457-
//impl<'a> PbString for alloc::borrow::Cow<'a, str> {
458-
//fn pb_spare_cap(&mut self) -> &mut [MaybeUninit<u8>] {
459-
// // SAFETY: spare_capacity_mut() is a safe call, since it doesn't change any bytes
460-
//unsafe { self.to_mut().as_mut_vec().spare_capacity_mut() }
461-
//}
462-
//}
458+
impl<T> PbVec<T> for Cow<'_, [T]>
459+
where
460+
[T]: ToOwned<Owned = Vec<T>>,
461+
{
462+
fn pb_push(&mut self, elem: T) -> Result<(), ()> {
463+
self.to_mut().push(elem);
464+
Ok(())
465+
}
466+
}
463467

464468
impl<K: Ord, V> PbMap<K, V> for BTreeMap<K, V> {
465469
#[inline]

0 commit comments

Comments
 (0)