2018-11-08 12:05:59 +01:00
|
|
|
use std::slice::from_raw_parts;
|
2018-12-31 18:33:59 +01:00
|
|
|
use std::mem::size_of;
|
2019-02-17 16:33:42 +01:00
|
|
|
use std::error::Error;
|
2018-11-08 12:05:59 +01:00
|
|
|
|
2018-12-30 13:22:02 +01:00
|
|
|
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
|
2018-12-09 14:18:23 +01:00
|
|
|
use sdset::Set;
|
2018-11-08 12:05:59 +01:00
|
|
|
|
2019-02-17 16:33:42 +01:00
|
|
|
use crate::shared_data_cursor::{SharedDataCursor, FromSharedDataCursor};
|
|
|
|
use crate::write_to_bytes::WriteToBytes;
|
2018-12-30 13:22:02 +01:00
|
|
|
use crate::data::SharedData;
|
2019-02-17 16:33:42 +01:00
|
|
|
use crate::DocumentId;
|
|
|
|
|
2018-12-31 18:33:59 +01:00
|
|
|
use super::into_u8_slice;
|
2018-11-08 12:05:59 +01:00
|
|
|
|
2018-12-01 18:37:21 +01:00
|
|
|
#[derive(Default, Clone)]
|
2018-12-31 18:33:59 +01:00
|
|
|
pub struct DocIds(SharedData);
|
2018-11-08 12:05:59 +01:00
|
|
|
|
|
|
|
impl DocIds {
|
2018-12-31 18:33:59 +01:00
|
|
|
pub fn new(ids: &Set<DocumentId>) -> DocIds {
|
|
|
|
let bytes = unsafe { into_u8_slice(ids.as_slice()) };
|
|
|
|
let data = SharedData::from_bytes(bytes.to_vec());
|
|
|
|
DocIds(data)
|
2018-11-08 12:05:59 +01:00
|
|
|
}
|
|
|
|
|
2018-12-31 18:33:59 +01:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.0.is_empty()
|
2018-12-30 13:22:02 +01:00
|
|
|
}
|
|
|
|
|
2018-12-31 18:33:59 +01:00
|
|
|
pub fn as_bytes(&self) -> &[u8] {
|
|
|
|
&self.0
|
2018-11-08 12:05:59 +01:00
|
|
|
}
|
2018-12-31 18:33:59 +01:00
|
|
|
}
|
2018-11-08 12:05:59 +01:00
|
|
|
|
2018-12-31 18:33:59 +01:00
|
|
|
impl AsRef<Set<DocumentId>> for DocIds {
|
|
|
|
fn as_ref(&self) -> &Set<DocumentId> {
|
|
|
|
let slice = &self.0;
|
2018-11-08 12:05:59 +01:00
|
|
|
let ptr = slice.as_ptr() as *const DocumentId;
|
2018-12-31 18:33:59 +01:00
|
|
|
let len = slice.len() / size_of::<DocumentId>();
|
2018-12-09 14:18:23 +01:00
|
|
|
let slice = unsafe { from_raw_parts(ptr, len) };
|
|
|
|
Set::new_unchecked(slice)
|
2018-11-08 12:05:59 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-17 16:33:42 +01:00
|
|
|
|
|
|
|
impl FromSharedDataCursor for DocIds {
|
|
|
|
type Error = Box<Error>;
|
|
|
|
|
|
|
|
fn from_shared_data_cursor(cursor: &mut SharedDataCursor) -> Result<DocIds, Self::Error> {
|
|
|
|
let len = cursor.read_u64::<LittleEndian>()? as usize;
|
|
|
|
let data = cursor.extract(len);
|
|
|
|
|
|
|
|
Ok(DocIds(data))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WriteToBytes for DocIds {
|
|
|
|
fn write_to_bytes(&self, bytes: &mut Vec<u8>) {
|
|
|
|
let len = self.0.len() as u64;
|
|
|
|
bytes.write_u64::<LittleEndian>(len).unwrap();
|
|
|
|
bytes.extend_from_slice(&self.0);
|
|
|
|
}
|
|
|
|
}
|