columnar/
boxed.rs

1//! Support for `Box<T>` where `T: Columnar`.
2//!
3//! The implementation defers to `T`'s implementation to store data. It reveals
4//! the same reference type as `T`, wrapped in [`Boxed`].
5//!
6//! We need this wrapper to distinguish which [`Push`] implementation to use, otherwise
7//! the implementations would conflict.
8
9use crate::{AsBytes, Clear, Columnar, Container, FromBytes, HeapSize, Index, IndexMut, Len, Push, Ref};
10
11impl<T: Columnar> Columnar for Box<T> {
12    type Container = Boxed<T::Container>;
13    #[inline(always)] fn copy_from<'a>(&mut self, other: Ref<'a, Self>) { self.as_mut().copy_from(other.0); }
14    #[inline(always)] fn into_owned<'a>(other: Ref<'a, Self>) -> Self { T::into_owned(other.0).into() }
15}
16
17/// A newtype wrapper around `T` that implements `Deref` and `DerefMut`.
18#[derive(Copy, Clone, Default)]
19pub struct Boxed<T>(pub T);
20
21impl<T> std::ops::Deref for Boxed<T> {
22    type Target = T;
23    #[inline(always)] fn deref(&self) -> &T { &self.0 }
24}
25impl<T> std::ops::DerefMut for Boxed<T> {
26    #[inline(always)] fn deref_mut(&mut self) -> &mut T { &mut self.0 }
27}
28impl<C: Container> Container for Boxed<C> {
29    type Ref<'a> = Boxed<C::Ref<'a>>;
30    type Borrowed<'a> = Boxed<C::Borrowed<'a>>;
31    #[inline(always)] fn borrow<'a>(&'a self) -> Self::Borrowed<'a> { Boxed(self.0.borrow()) }
32    #[inline(always)] fn reborrow<'b, 'a: 'b>(item: Self::Borrowed<'a>) -> Self::Borrowed<'b> where Self: 'a { Boxed(C::reborrow(item.0)) }
33    #[inline(always)] fn reborrow_ref<'b, 'a: 'b>(item: Self::Ref<'a>) -> Self::Ref<'b> where Self: 'a { Boxed(C::reborrow_ref(item.0)) }
34    #[inline(always)] fn extend_from_self(&mut self, other: Self::Borrowed<'_>, range: std::ops::Range<usize>) { self.0.extend_from_self(other.0, range) }
35    #[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)) }
36}
37impl<C: Len> Len for Boxed<C> {
38    #[inline(always)] fn len(&self) -> usize { self.0.len() }
39    #[inline(always)] fn is_empty(&self) -> bool { self.0.is_empty() }
40}
41impl<C: Clear> Clear for Boxed<C> {
42    #[inline(always)] fn clear(&mut self) { self.0.clear() }
43}
44impl<'a, T: ?Sized, C: Container + Push<&'a T>> Push<&'a Box<T>> for Boxed<C> {
45    #[inline(always)] fn push(&mut self, item: &'a Box<T>) { self.0.push(item.as_ref()) }
46    #[inline(always)] fn extend(&mut self, iter: impl IntoIterator<Item=&'a Box<T>>) {
47        self.0.extend(iter.into_iter().map(|x| x.as_ref()))
48    }
49}
50impl<'a, C: Container> Push<Boxed<C::Ref<'a>>> for Boxed<C> {
51    #[inline(always)] fn push(&mut self, item: Boxed<C::Ref<'_>>) { self.0.push(item.0) }
52    #[inline(always)] fn extend(&mut self, iter: impl IntoIterator<Item=Boxed<C::Ref<'a>>>) {
53        self.0.extend(iter.into_iter().map(|x| x.0))
54    }
55}
56impl<'a, C: AsBytes<'a>> AsBytes<'a> for Boxed<C> {
57    #[inline(always)] fn as_bytes(&self) -> impl Iterator<Item=(u64, &'a [u8])> { self.0.as_bytes() }
58}
59impl<'a, C: FromBytes<'a>> FromBytes<'a> for Boxed<C> {
60    #[inline(always)] fn from_bytes(bytes: &mut impl Iterator<Item=&'a [u8]>) -> Self { Self(C::from_bytes(bytes)) }
61}
62impl<C: Index> Index for Boxed<C> {
63    type Ref = Boxed<C::Ref>;
64    #[inline(always)] fn get(&self, index: usize) -> Self::Ref { Boxed(self.0.get(index)) }
65}
66impl<C: IndexMut> IndexMut for Boxed<C> {
67    type IndexMut<'a> = Boxed<C::IndexMut<'a>> where Self: 'a;
68    #[inline(always)] fn get_mut(&mut self, index: usize) -> Self::IndexMut<'_> { Boxed(self.0.get_mut(index)) }
69}
70impl<C: HeapSize> HeapSize for Boxed<C> {
71    #[inline(always)] fn heap_size(&self) -> (usize, usize) { self.0.heap_size() }
72}