Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn _bench_copy<T: Columnar+Eq>(bencher: &mut Bencher, record: T) where T::Contai
for _ in 0 .. 1024 {
arena.push(&record);
}
use columnar::Container;
use columnar::Borrow;
bencher.bytes = Sequence::length_in_bytes(&arena.borrow()) as u64;
arena.clear();

Expand All @@ -82,7 +82,7 @@ fn _bench_extend<T: Columnar+Eq>(bencher: &mut Bencher, record: T) where T::Cont
for _ in 0 .. 1024 {
arena.push(&record);
}
use columnar::Container;
use columnar::{Borrow, Container};
bencher.bytes = Sequence::length_in_bytes(&arena.borrow()) as u64;

let arena2 = arena.clone();
Expand Down
40 changes: 24 additions & 16 deletions columnar_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,28 +361,30 @@ fn derive_struct(name: &syn::Ident, generics: &syn::Generics, data_struct: syn::
type Container = #c_ident < #(<#types as ::columnar::Columnar>::Container ),* >;
}

impl < #( #container_types: ::columnar::Container ),* > ::columnar::Container for #c_ident < #( #container_types ),* > {
type Ref<'a> = #r_ident < #(<#container_types as ::columnar::Container>::Ref<'a>,)* > where #(#container_types: 'a,)*;
type Borrowed<'a> = #c_ident < #(<#container_types as ::columnar::Container>::Borrowed<'a> ),* > where #(#container_types: 'a,)*;
impl < #( #container_types: ::columnar::Borrow ),* > ::columnar::Borrow for #c_ident < #( #container_types ),* > {
type Ref<'a> = #r_ident < #(<#container_types as ::columnar::Borrow>::Ref<'a>,)* > where #(#container_types: 'a,)*;
type Borrowed<'a> = #c_ident < #(<#container_types as ::columnar::Borrow>::Borrowed<'a> ),* > where #(#container_types: 'a,)*;
#[inline(always)]
fn borrow<'a>(&'a self) -> Self::Borrowed<'a> {
#c_ident {
#( #names: <#container_types as ::columnar::Container>::borrow(&self.#names), )*
#( #names: <#container_types as ::columnar::Borrow>::borrow(&self.#names), )*
}
}
#[inline(always)]
fn reborrow<'b, 'a: 'b>(thing: Self::Borrowed<'a>) -> Self::Borrowed<'b> {
#c_ident {
#( #names: <#container_types as ::columnar::Container>::reborrow(thing.#names), )*
#( #names: <#container_types as ::columnar::Borrow>::reborrow(thing.#names), )*
}
}
#[inline(always)]
fn reborrow_ref<'b, 'a: 'b>(thing: Self::Ref<'a>) -> Self::Ref<'b> {
#r_ident {
#( #names: <#container_types as ::columnar::Container>::reborrow_ref(thing.#names), )*
#( #names: <#container_types as ::columnar::Borrow>::reborrow_ref(thing.#names), )*
}
}
}

impl < #( #container_types: ::columnar::Container ),* > ::columnar::Container for #c_ident < #( #container_types ),* > {
#[inline(always)]
fn extend_from_self(&mut self, other: Self::Borrowed<'_>, range: std::ops::Range<usize>) {
#( self.#names.extend_from_self(other.#names, range.clone()); )*
Expand Down Expand Up @@ -512,7 +514,7 @@ fn derive_unit_struct(name: &syn::Ident, _generics: &syn::Generics, vis: syn::Vi
type Container = #c_ident;
}

impl ::columnar::Container for #c_ident {
impl ::columnar::Borrow for #c_ident {
type Ref<'a> = #name;
type Borrowed<'a> = #c_ident < &'a u64 >;
#[inline(always)]
Expand All @@ -525,7 +527,9 @@ fn derive_unit_struct(name: &syn::Ident, _generics: &syn::Generics, vis: syn::Vi
}
#[inline(always)]
fn reborrow_ref<'b, 'a: 'b>(thing: Self::Ref<'a>) -> Self::Ref<'b> { thing }
}

impl ::columnar::Container for #c_ident {
#[inline(always)]
fn extend_from_self(&mut self, _other: Self::Borrowed<'_>, range: std::ops::Range<usize>) {
self.count += range.len() as u64;
Expand Down Expand Up @@ -970,7 +974,7 @@ fn derive_enum(name: &syn::Ident, generics: &syn:: Generics, data_enum: syn::Dat
let reborrow_ref = variants.iter().enumerate().zip(container_names.iter()).map(|((index, (variant, types)), cname)| {
quote! {
#r_ident::#variant(( potato )) => {
#r_ident::#variant(( < (#cname) as ::columnar::Container >::reborrow_ref::<'b, 'a>( potato ) ))
#r_ident::#variant(( < (#cname) as ::columnar::Borrow >::reborrow_ref::<'b, 'a>( potato ) ))
},
}
}).collect::<Vec<_>>();
Expand All @@ -993,9 +997,9 @@ fn derive_enum(name: &syn::Ident, generics: &syn:: Generics, data_enum: syn::Dat
type Container = #c_ident < #(#container_types),* >;
}

impl < #(#container_names : ::columnar::Container ),* > ::columnar::Container for #c_ident < #(#container_names),* > {
type Ref<'a> = #r_ident < #( <#container_names as ::columnar::Container>::Ref<'a> ,)* > where Self: 'a, #(#container_names: 'a,)*;
type Borrowed<'a> = #c_ident < #( < #container_names as ::columnar::Container >::Borrowed<'a>, )* &'a [u8], &'a [u64] > where #(#container_names: 'a,)*;
impl < #(#container_names : ::columnar::Borrow ),* > ::columnar::Borrow for #c_ident < #(#container_names),* > {
type Ref<'a> = #r_ident < #( <#container_names as ::columnar::Borrow>::Ref<'a> ,)* > where Self: 'a, #(#container_names: 'a,)*;
type Borrowed<'a> = #c_ident < #( < #container_names as ::columnar::Borrow >::Borrowed<'a>, )* &'a [u8], &'a [u64] > where #(#container_names: 'a,)*;
#[inline(always)]
fn borrow<'a>(&'a self) -> Self::Borrowed<'a> {
#c_ident {
Expand All @@ -1007,9 +1011,9 @@ fn derive_enum(name: &syn::Ident, generics: &syn:: Generics, data_enum: syn::Dat
#[inline(always)]
fn reborrow<'b, 'a: 'b>(thing: Self::Borrowed<'a>) -> Self::Borrowed<'b> {
#c_ident {
#(#names: <#container_names as ::columnar::Container>::reborrow(thing.#names),)*
variant: <Vec<u8> as ::columnar::Container>::reborrow(thing.variant),
offset: <Vec<u64> as ::columnar::Container>::reborrow(thing.offset),
#(#names: <#container_names as ::columnar::Borrow>::reborrow(thing.#names),)*
variant: <Vec<u8> as ::columnar::Borrow>::reborrow(thing.variant),
offset: <Vec<u64> as ::columnar::Borrow>::reborrow(thing.offset),
}
}
#[inline(always)]
Expand All @@ -1018,7 +1022,9 @@ fn derive_enum(name: &syn::Ident, generics: &syn:: Generics, data_enum: syn::Dat
#( #reborrow_ref )*
}
}
}

impl < #(#container_names : ::columnar::Container ),* > ::columnar::Container for #c_ident < #(#container_names),* > {
// TODO: implement `extend_from_self`.

fn reserve_for<'a, I>(&mut self, selves: I) where Self: 'a, I: Iterator<Item = Self::Borrowed<'a>> + Clone {
Expand Down Expand Up @@ -1161,7 +1167,7 @@ fn derive_tags(name: &syn::Ident, _generics: &syn:: Generics, data_enum: syn::Da
type Container = #c_ident;
}

impl<CV: ::columnar::common::PushIndexAs<u8>> ::columnar::Container for #c_ident <CV> {
impl<CV: ::columnar::common::BorrowIndexAs<u8>> ::columnar::Borrow for #c_ident <CV> {
type Ref<'a> = #name;
type Borrowed<'a> = #c_ident < CV::Borrowed<'a> > where CV: 'a;
#[inline(always)]
Expand All @@ -1173,12 +1179,14 @@ fn derive_tags(name: &syn::Ident, _generics: &syn:: Generics, data_enum: syn::Da
#[inline(always)]
fn reborrow<'b, 'a: 'b>(thing: Self::Borrowed<'a>) -> Self::Borrowed<'b> {
#c_ident {
variant: <CV as ::columnar::Container>::reborrow(thing.variant),
variant: <CV as ::columnar::Borrow>::reborrow(thing.variant),
}
}
#[inline(always)]
fn reborrow_ref<'b, 'a: 'b>(thing: Self::Ref<'a>) -> Self::Ref<'b> { thing }
}

impl<CV: ::columnar::common::PushIndexAs<u8>> ::columnar::Container for #c_ident <CV> {
// TODO: implement `extend_from_self`.

fn reserve_for<'a, I>(&mut self, selves: I) where Self: 'a, I: Iterator<Item = Self::Borrowed<'a>> + Clone {
Expand Down
69 changes: 69 additions & 0 deletions src/arc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//! Implementations of traits for `Arc<T>`
use std::sync::Arc;

use crate::{Len, Borrow, HeapSize, AsBytes, FromBytes};

impl<T: Borrow> Borrow for Arc<T> {
type Ref<'a> = T::Ref<'a> where T: 'a;
type Borrowed<'a> = T::Borrowed<'a>;
#[inline(always)] fn borrow<'a>(&'a self) -> Self::Borrowed<'a> { self.as_ref().borrow() }
#[inline(always)] fn reborrow<'b, 'a: 'b>(item: Self::Borrowed<'a>) -> Self::Borrowed<'b> where Self: 'a { T::reborrow(item) }
#[inline(always)] fn reborrow_ref<'b, 'a: 'b>(item: Self::Ref<'a>) -> Self::Ref<'b> where Self: 'a { T::reborrow_ref(item) }
}
impl<T: Len> Len for Arc<T> {
#[inline(always)] fn len(&self) -> usize { self.as_ref().len() }
}
impl<T: HeapSize> HeapSize for Arc<T> {
fn heap_size(&self) -> (usize, usize) {
let (l, c) = self.as_ref().heap_size();
(l + std::mem::size_of::<Arc<T>>(), c + std::mem::size_of::<Arc<T>>())
}
}
impl<'a, T: AsBytes<'a>> AsBytes<'a> for Arc<T> {
#[inline(always)] fn as_bytes(&self) -> impl Iterator<Item=(u64, &'a [u8])> { self.as_ref().as_bytes() }
}
impl<'a, T: FromBytes<'a>> FromBytes<'a> for Arc<T> {
#[inline(always)] fn from_bytes(bytes: &mut impl Iterator<Item=&'a [u8]>) -> Self { Arc::new(T::from_bytes(bytes)) }
}

#[cfg(test)]
mod tests {
use std::sync::Arc;
use crate::{Borrow, Len, HeapSize, AsBytes, FromBytes};

#[test]
fn test_borrow() {
let x = Arc::new(vec![1, 2, 3]);
let y: &[i32] = x.borrow();
assert_eq!(y, &[1, 2, 3]);
}

#[test]
fn test_len() {
let x = Arc::new(vec![1, 2, 3]);
assert_eq!(x.len(), 3);
}

#[test]
fn test_heap_size() {
let x = Arc::new(vec![1, 2, 3]);
let (l, c) = x.heap_size();
assert!(l > 0);
assert!(c > 0);
}

#[test]
fn test_as_from_bytes() {
let x = Arc::new(vec![1u8, 2, 3, 4, 5]);
let bytes: Vec<_> = x.borrow().as_bytes().map(|(_, b)| b).collect();
let y: Arc<&[u8]> = FromBytes::from_bytes(&mut bytes.into_iter());
assert_eq!(*x, *y);
}

#[test]
fn test_borrow_tuple() {
let x = (vec![4,5,6,7,], Arc::new(vec![1, 2, 3]));
let y: (&[i32], &[i32]) = x.borrow();
assert_eq!(y, ([4,5,6,7].as_ref(), [1, 2, 3].as_ref()));
}
}
6 changes: 4 additions & 2 deletions src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! We need this wrapper to distinguish which [`Push`] implementation to use, otherwise
//! the implementations would conflict.

use crate::{AsBytes, Clear, Columnar, Container, FromBytes, HeapSize, Index, IndexMut, Len, Push, Ref};
use crate::{AsBytes, Borrow, Clear, Columnar, Container, FromBytes, HeapSize, Index, IndexMut, Len, Push, Ref};

impl<T: Columnar> Columnar for Box<T> {
type Container = Boxed<T::Container>;
Expand All @@ -25,12 +25,14 @@ impl<T> std::ops::Deref for Boxed<T> {
impl<T> std::ops::DerefMut for Boxed<T> {
#[inline(always)] fn deref_mut(&mut self) -> &mut T { &mut self.0 }
}
impl<C: Container> Container for Boxed<C> {
impl<C: Borrow> Borrow for Boxed<C> {
type Ref<'a> = Boxed<C::Ref<'a>>;
type Borrowed<'a> = Boxed<C::Borrowed<'a>>;
#[inline(always)] fn borrow<'a>(&'a self) -> Self::Borrowed<'a> { Boxed(self.0.borrow()) }
#[inline(always)] fn reborrow<'b, 'a: 'b>(item: Self::Borrowed<'a>) -> Self::Borrowed<'b> where Self: 'a { Boxed(C::reborrow(item.0)) }
#[inline(always)] fn reborrow_ref<'b, 'a: 'b>(item: Self::Ref<'a>) -> Self::Ref<'b> where Self: 'a { Boxed(C::reborrow_ref(item.0)) }
}
impl<C: Container> Container for Boxed<C> {
#[inline(always)] fn extend_from_self(&mut self, other: Self::Borrowed<'_>, range: std::ops::Range<usize>) { self.0.extend_from_self(other.0, range) }
#[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)) }
}
Expand Down
Loading
Loading