use std::{convert, num, ops};
use storage::{provided, range};
use write::consume;
pub use write::consume::scope::{Input, Prefix};
use crate::{buffer::pool::owners, mem::rc};
pub mod raw;
pub mod bytes;
pub mod hold;
pub mod pool;
pub mod resident;
pub mod segments;
pub mod storage;
pub mod write;
pub type View<'source, S> = Input<&'source S, &'source [u8]>;
impl<S: ?Sized> Copy for View<'_, S> {}
impl<S: ?Sized> Clone for View<'_, S> {
fn clone(&self) -> Self {
*self
}
}
impl<'source, S: ?Sized> View<'source, S> {
pub fn as_slice(&self) -> &'source [u8] {
self.proof
}
pub fn is_empty(&self) -> bool {
self.proof.is_empty()
}
pub fn len(&self) -> usize {
self.proof.len()
}
pub fn select(&self, bytes: &[u8]) -> Option<View<'source, S>> {
let range: ops::Range<usize> = self.proof.subslice_range(bytes)?.into();
Some(Self {
source: self.source,
proof: unsafe { self.proof.get_unchecked(range) },
})
}
pub fn get(&self, range: ops::Range<usize>) -> Option<View<'source, S>> {
let bytes = self.proof.get(range)?;
Some(View {
source: self.source,
proof: bytes,
})
}
pub fn try_get(&self, range: ops::Range<usize>) -> Result<Self, range::Error> {
self.get(range.clone())
.ok_or_else(|| range::Error::new(range, self.len()))
}
pub fn new(source: &'source S) -> Self
where
S: AsRef<[u8]>,
{
Self {
source,
proof: source.as_ref(),
}
}
}
impl<S: Residency + ?Sized> Residency for View<'_, S> {
fn resident(&self) -> usize {
if self.is_empty() {
0
} else {
self.source.resident()
}
}
}
impl View<'_, resident::Shared<'_>> {
pub fn try_own(self) -> Result<Option<storage::Shared>, rc::Error> {
if self.is_empty() {
return Ok(Some(storage::Shared::new()));
}
let owner = match &self.source.owner().0 {
resident::Kind::Owned(owner) => Some(owner.try_clone()?),
resident::Kind::Static => None,
resident::Kind::Slice
| resident::Kind::Resident(_)
| resident::Kind::Accounted(_)
| resident::Kind::Provided(_)
| resident::Kind::Fixed(_) => return Ok(None),
};
Ok(Some(unsafe {
storage::Shared::assume(self.as_slice(), owner)
}))
}
}
const _: () = assert!(size_of::<View<'static, storage::Shared>>() == 3 * size_of::<usize>());
impl<S: ?Sized> consume::Consume for View<'_, S> {
fn remaining(&self) -> usize {
self.proof.len()
}
fn try_consume(&mut self, amount: usize) -> Result<(), consume::Error> {
bytes::scope::Range::try_consume(self, amount)
}
fn consume_max(&mut self, max: usize) -> usize {
raw::Store::advance_max(self, max)
}
}
impl<S: ?Sized> AsRef<[u8]> for View<'_, S> {
fn as_ref(&self) -> &[u8] {
self.proof
}
}
impl<'input, S: ?Sized> View<'input, S> {
pub fn try_split<const N: usize>(mut self) -> Result<(&'input [u8; N], Self), consume::Error> {
let (head, tail) = self.proof.split_first_chunk::<N>().ok_or_else(|| {
consume::Error::new(
num::NonZero::<usize>::MIN.saturating_add(N.saturating_sub(1)),
self.proof.len(),
)
})?;
self.proof = tail;
Ok((head, self))
}
}
pub trait Residency {
fn resident(&self) -> usize;
}
impl<L> Residency for pool::Span<owners::Plain<L>, pool::Owned, pool::Frozen>
where
owners::Plain<L>: crate::buffer::raw::Backing,
{
fn resident(&self) -> usize {
self.capacity().get() as usize
}
}
impl<L> Residency for pool::Cursor<L>
where
owners::Plain<L>: crate::buffer::raw::Backing,
{
fn resident(&self) -> usize {
self.len() + self.spare_len()
}
}
impl Residency for Vec<u8> {
fn resident(&self) -> usize {
self.capacity()
}
}
impl<const CAP: usize, L> Residency for storage::inline::Bytes<CAP, L> {
fn resident(&self) -> usize {
CAP
}
}
impl Residency for [u8] {
fn resident(&self) -> usize {
0
}
}
impl Residency for &[u8] {
fn resident(&self) -> usize {
0
}
}
impl Residency for convert::Infallible {
fn resident(&self) -> usize {
match *self {}
}
}
pub const BLOCK_CAPACITY: u32 = 64 * 1024;
fn is_within(range: &ops::Range<usize>, len: usize) -> bool {
range.start <= range.end && range.end <= len
}
impl<O> storage::Shared<O> {
pub fn view(&self) -> View<'_, Self> {
View::new(self)
}
}
impl<'d, L: provided::Layout> View<'_, storage::Shared<Option<provided::Account<'d, L>>>> {
pub fn try_retain(
self,
) -> Result<storage::Shared<Option<provided::Account<'d, L>>>, rc::Error> {
if self.is_empty() {
return Ok(storage::Shared::<Option<provided::Account<'d, L>>>::new());
}
self.try_clone()
}
}
const _: () = {
assert!(
size_of::<storage::Shared<Option<provided::Account<'static>>>>() == 3 * size_of::<usize>()
);
assert!(
size_of::<Option<storage::Shared<Option<provided::Account<'static>>>>>()
== 3 * size_of::<usize>()
);
};