From bddb37e44fc5f4046280e8eadcfac4ff4a2ab131 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Sat, 16 Feb 2019 18:56:26 +0100 Subject: [PATCH] feat: Move SharedData to its own module --- src/data/mod.rs | 44 ++----------------------------------- src/data/shared_data.rs | 48 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 42 deletions(-) create mode 100644 src/data/shared_data.rs diff --git a/src/data/mod.rs b/src/data/mod.rs index 328d66604..f076b08e9 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -1,5 +1,6 @@ mod doc_ids; mod doc_indexes; +mod shared_data; use std::slice::from_raw_parts; use std::mem::size_of; @@ -8,48 +9,7 @@ use std::sync::Arc; pub use self::doc_ids::DocIds; pub use self::doc_indexes::{DocIndexes, DocIndexesBuilder}; - -#[derive(Clone, Default)] -pub struct SharedData { - pub bytes: Arc>, - pub offset: usize, - pub len: usize, -} - -impl SharedData { - pub fn from_bytes(vec: Vec) -> SharedData { - let len = vec.len(); - let bytes = Arc::from(vec); - SharedData::new(bytes, 0, len) - } - - pub fn new(bytes: Arc>, offset: usize, len: usize) -> SharedData { - SharedData { bytes, offset, len } - } - - pub fn range(&self, offset: usize, len: usize) -> SharedData { - assert!(offset + len <= self.len); - SharedData { - bytes: self.bytes.clone(), - offset: self.offset + offset, - len: len, - } - } -} - -impl Deref for SharedData { - type Target = [u8]; - - fn deref(&self) -> &Self::Target { - self.as_ref() - } -} - -impl AsRef<[u8]> for SharedData { - fn as_ref(&self) -> &[u8] { - &self.bytes[self.offset..self.offset + self.len] - } -} +pub use self::shared_data::SharedData; unsafe fn into_u8_slice(slice: &[T]) -> &[u8] { let ptr = slice.as_ptr() as *const u8; diff --git a/src/data/shared_data.rs b/src/data/shared_data.rs new file mode 100644 index 000000000..100f837f7 --- /dev/null +++ b/src/data/shared_data.rs @@ -0,0 +1,48 @@ +use std::sync::Arc; +use std::ops::Deref; + +#[derive(Default, Clone)] +pub struct SharedData { + pub bytes: Arc>, + pub offset: usize, + pub len: usize, +} + +impl SharedData { + pub fn from_bytes(vec: Vec) -> SharedData { + let len = vec.len(); + let bytes = Arc::from(vec); + SharedData::new(bytes, 0, len) + } + + pub fn new(bytes: Arc>, offset: usize, len: usize) -> SharedData { + SharedData { bytes, offset, len } + } + + pub fn as_slice(&self) -> &[u8] { + &self.bytes[self.offset..self.offset + self.len] + } + + pub fn range(&self, offset: usize, len: usize) -> SharedData { + assert!(offset + len <= self.len); + SharedData { + bytes: self.bytes.clone(), + offset: self.offset + offset, + len: len, + } + } +} + +impl Deref for SharedData { + type Target = [u8]; + + fn deref(&self) -> &Self::Target { + self.as_slice() + } +} + +impl AsRef<[u8]> for SharedData { + fn as_ref(&self) -> &[u8] { + self.as_slice() + } +}