forked from frankmcsherry/columnar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharc.rs
More file actions
69 lines (61 loc) · 2.25 KB
/
arc.rs
File metadata and controls
69 lines (61 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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()));
}
}