use crate::{
error::{Error, Result},
segment::Segment,
Endidness,
};
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
#[cfg(feature = "std")]
use std::path::Path;
mod vec_source;
pub use vec_source::VecSource;
#[cfg(feature = "with_bytes")]
use bytes::Bytes;
#[cfg(feature = "with_bytes")]
mod bytes_source;
#[cfg(feature = "with_bytes")]
pub use bytes_source::BytesSource;
#[cfg(feature = "memmap")]
mod mmap;
#[cfg(feature = "memmap")]
pub use mmap::MappedFileSource;
#[cfg(feature = "async")]
use async_trait::async_trait;
pub trait Source: Sized + Sync + Send {
type Item;
fn from_vec(items: Vec<Self::Item>) -> Result<Self> {
Self::from_vec_with_offset(items, 0)
}
fn from_vec_with_offset(items: Vec<Self::Item>, initial_offset: usize) -> Result<Self>;
fn validate_offset(&self, offset: usize) -> Result<()> {
if offset < self.lower_offset_limit() {
Err(Error::OffsetTooSmall { offset })
} else if offset > self.upper_offset_limit() {
Err(Error::OffsetTooLarge { offset })
} else {
Ok(())
}
}
fn size(&self) -> usize;
fn initial_offset(&self) -> usize;
fn all(&self) -> Result<Segment<Self::Item>> {
self.segment(self.lower_offset_limit(), self.upper_offset_limit())
}
fn segment(&self, start: usize, end: usize) -> Result<Segment<Self::Item>>;
fn get_n(&self, offset: usize, num_items: usize) -> Result<Segment<Self::Item>> {
self.validate_offset(offset)?;
self.validate_offset(offset + num_items)?;
self.segment(offset, offset + num_items)
}
fn all_before(&self, offset: usize) -> Result<Segment<Self::Item>> {
self.validate_offset(offset)?;
self.get_n(self.lower_offset_limit(), offset)
}
fn all_after(&self, offset: usize) -> Result<Segment<Self::Item>> {
self.validate_offset(offset)?;
self.get_n(offset, self.upper_offset_limit())
}
#[inline]
fn lower_offset_limit(&self) -> usize {
self.initial_offset()
}
#[inline]
fn upper_offset_limit(&self) -> usize {
self.size() + self.initial_offset()
}
}
#[cfg_attr(feature = "async", async_trait)]
pub trait U8Source: Source<Item = u8> {
fn endidness(&self) -> Endidness;
fn change_endidness(&mut self, endidness: Endidness);
#[inline]
fn from_u8_slice(slice: &[u8], endidness: Endidness) -> Result<Self> {
Self::from_u8_slice_with_offset(slice, 0, endidness)
}
fn from_u8_slice_with_offset(
slice: &[u8],
initial_offset: usize,
endidness: Endidness,
) -> Result<Self>;
#[inline]
fn from_u8_vec(items: Vec<u8>, endidness: Endidness) -> Result<Self> {
Self::from_u8_vec_with_offset(items, 0, endidness)
}
#[inline]
fn from_u8_vec_with_offset(
items: Vec<u8>,
initial_offset: usize,
endidness: Endidness,
) -> Result<Self> {
Self::from_u8_slice_with_offset(&items, initial_offset, endidness)
}
#[cfg(feature = "std")]
#[inline]
fn from_file<P: AsRef<Path>>(path: P, endidness: Endidness) -> Result<Self> {
Self::from_file_with_offset(path, 0, endidness)
}
#[cfg(feature = "std")]
fn from_file_with_offset<P: AsRef<Path>>(
path: P,
initial_offset: usize,
endidness: Endidness,
) -> Result<Self>;
#[cfg(feature = "async")]
#[inline]
async fn from_file_async<P>(path: P, endidness: Endidness) -> Result<Self>
where
P: AsRef<Path> + Sync + Send,
{
Self::from_file_with_offset_async(path, 0, endidness).await
}
#[cfg(feature = "async")]
async fn from_file_with_offset_async<P>(
path: P,
initial_offset: usize,
endidness: Endidness,
) -> Result<Self>
where
P: AsRef<Path> + Sync + Send;
#[cfg(feature = "with_bytes")]
#[inline]
fn from_bytes(bytes: Bytes, endidness: Endidness) -> Result<Self> {
Self::from_bytes_with_offset(bytes, 0, endidness)
}
#[cfg(feature = "with_bytes")]
fn from_bytes_with_offset(
bytes: Bytes,
initial_offset: usize,
endidness: Endidness,
) -> Result<Self>;
}