1use alloc::boxed::Box;
9
10use crate::{AsBytes, Borrow, Clear, Columnar, Container, FromBytes, Index, IndexMut, Len, Push, Ref};
11
12impl<T: Columnar> Columnar for Box<T> {
13 type Container = Boxed<T::Container>;
14 #[inline(always)] fn copy_from<'a>(&mut self, other: Ref<'a, Self>) { self.as_mut().copy_from(other.0); }
15 #[inline(always)] fn into_owned<'a>(other: Ref<'a, Self>) -> Self { T::into_owned(other.0).into() }
16}
17
18#[derive(Copy, Clone, Default)]
20pub struct Boxed<T>(pub T);
21
22impl<T> core::ops::Deref for Boxed<T> {
23 type Target = T;
24 #[inline(always)] fn deref(&self) -> &T { &self.0 }
25}
26impl<T> core::ops::DerefMut for Boxed<T> {
27 #[inline(always)] fn deref_mut(&mut self) -> &mut T { &mut self.0 }
28}
29impl<C: Borrow> Borrow for Boxed<C> {
30 type Ref<'a> = Boxed<C::Ref<'a>>;
31 type Borrowed<'a> = Boxed<C::Borrowed<'a>>;
32 #[inline(always)] fn borrow<'a>(&'a self) -> Self::Borrowed<'a> { Boxed(self.0.borrow()) }
33 #[inline(always)] fn reborrow<'b, 'a: 'b>(item: Self::Borrowed<'a>) -> Self::Borrowed<'b> where Self: 'a { Boxed(C::reborrow(item.0)) }
34 #[inline(always)] fn reborrow_ref<'b, 'a: 'b>(item: Self::Ref<'a>) -> Self::Ref<'b> where Self: 'a { Boxed(C::reborrow_ref(item.0)) }
35}
36impl<C: Container> Container for Boxed<C> {
37 #[inline(always)] fn extend_from_self(&mut self, other: Self::Borrowed<'_>, range: core::ops::Range<usize>) { self.0.extend_from_self(other.0, range) }
38 #[inline(always)] fn reserve_for<'a, I>(&mut self, selves: I) where Self: 'a, I: Iterator<Item = Self::Borrowed<'a>> + Clone { self.0.reserve_for(selves.map(|x| x.0)) }
39}
40impl<C: Len> Len for Boxed<C> {
41 #[inline(always)] fn len(&self) -> usize { self.0.len() }
42 #[inline(always)] fn is_empty(&self) -> bool { self.0.is_empty() }
43}
44impl<C: Clear> Clear for Boxed<C> {
45 #[inline(always)] fn clear(&mut self) { self.0.clear() }
46}
47impl<'a, T: ?Sized, C: Container + Push<&'a T>> Push<&'a Box<T>> for Boxed<C> {
48 #[inline(always)] fn push(&mut self, item: &'a Box<T>) { self.0.push(item.as_ref()) }
49 #[inline(always)] fn extend(&mut self, iter: impl IntoIterator<Item=&'a Box<T>>) {
50 self.0.extend(iter.into_iter().map(|x| x.as_ref()))
51 }
52}
53impl<'a, C: Container> Push<Boxed<C::Ref<'a>>> for Boxed<C> {
54 #[inline(always)] fn push(&mut self, item: Boxed<C::Ref<'_>>) { self.0.push(item.0) }
55 #[inline(always)] fn extend(&mut self, iter: impl IntoIterator<Item=Boxed<C::Ref<'a>>>) {
56 self.0.extend(iter.into_iter().map(|x| x.0))
57 }
58}
59impl<'a, C: AsBytes<'a>> AsBytes<'a> for Boxed<C> {
60 #[inline(always)] fn as_bytes(&self) -> impl Iterator<Item=(u64, &'a [u8])> { self.0.as_bytes() }
61}
62impl<'a, C: FromBytes<'a>> FromBytes<'a> for Boxed<C> {
63 const SLICE_COUNT: usize = C::SLICE_COUNT;
64 #[inline(always)] fn from_bytes(bytes: &mut impl Iterator<Item=&'a [u8]>) -> Self { Self(C::from_bytes(bytes)) }
65 #[inline(always)] fn from_store(store: &crate::bytes::indexed::DecodedStore<'a>, offset: &mut usize) -> Self { Self(C::from_store(store, offset)) }
66}
67impl<C: Index> Index for Boxed<C> {
68 type Ref = Boxed<C::Ref>;
69 #[inline(always)] fn get(&self, index: usize) -> Self::Ref { Boxed(self.0.get(index)) }
70}
71impl<C: IndexMut> IndexMut for Boxed<C> {
72 type IndexMut<'a> = Boxed<C::IndexMut<'a>> where Self: 'a;
73 #[inline(always)] fn get_mut(&mut self, index: usize) -> Self::IndexMut<'_> { Boxed(self.0.get_mut(index)) }
74}
75