Skip to main content

columnar/
arc.rs

1//! Implementations of traits for `Arc<T>`
2use alloc::sync::Arc;
3
4use crate::{Len, Borrow, AsBytes, FromBytes};
5
6impl<T: Borrow> Borrow for Arc<T> {
7    type Ref<'a> = T::Ref<'a> where T: 'a;
8    type Borrowed<'a> = T::Borrowed<'a>;
9    #[inline(always)] fn borrow<'a>(&'a self) -> Self::Borrowed<'a> { self.as_ref().borrow() }
10    #[inline(always)] fn reborrow<'b, 'a: 'b>(item: Self::Borrowed<'a>) -> Self::Borrowed<'b> where Self: 'a { T::reborrow(item) }
11    #[inline(always)] fn reborrow_ref<'b, 'a: 'b>(item: Self::Ref<'a>) -> Self::Ref<'b> where Self: 'a { T::reborrow_ref(item) }
12}
13impl<T: Len> Len for Arc<T> {
14    #[inline(always)] fn len(&self) -> usize { self.as_ref().len() }
15}
16impl<'a, T: AsBytes<'a>> AsBytes<'a> for Arc<T> {
17    #[inline(always)] fn as_bytes(&self) -> impl Iterator<Item=(u64, &'a [u8])> { self.as_ref().as_bytes() }
18}
19impl<'a, T: FromBytes<'a>> FromBytes<'a> for Arc<T> {
20    const SLICE_COUNT: usize = T::SLICE_COUNT;
21    #[inline(always)] fn from_bytes(bytes: &mut impl Iterator<Item=&'a [u8]>) -> Self { Arc::new(T::from_bytes(bytes)) }
22    #[inline(always)] fn from_store(store: &crate::bytes::indexed::DecodedStore<'a>, offset: &mut usize) -> Self { Arc::new(T::from_store(store, offset)) }
23}
24
25#[cfg(test)]
26mod tests {
27    use alloc::sync::Arc;
28    use alloc::{vec, vec::Vec};
29    use crate::{Borrow, Len, AsBytes, FromBytes};
30
31    #[test]
32    fn test_borrow() {
33        let x = Arc::new(vec![1, 2, 3]);
34        let y: &[i32] = x.borrow();
35        assert_eq!(y, &[1, 2, 3]);
36    }
37
38    #[test]
39    fn test_len() {
40        let x = Arc::new(vec![1, 2, 3]);
41        assert_eq!(x.len(), 3);
42    }
43
44    #[test]
45    fn test_as_from_bytes() {
46        let x = Arc::new(vec![1u8, 2, 3, 4, 5]);
47        let bytes: Vec<_> = x.borrow().as_bytes().map(|(_, b)| b).collect();
48        let y: Arc<&[u8]> = FromBytes::from_bytes(&mut bytes.into_iter());
49        assert_eq!(*x, *y);
50    }
51
52    #[test]
53    fn test_borrow_tuple() {
54        let x = (vec![4,5,6,7,], Arc::new(vec![1, 2, 3]));
55        let y: (&[i32], &[i32]) = x.borrow();
56        assert_eq!(y, ([4,5,6,7].as_ref(), [1, 2, 3].as_ref()));
57    }
58}