pub(in crate::buffer::resident) mod exact;
pub(in crate::buffer::resident) mod state;
use std::ops;
use crate::buffer::{
self,
bytes::bounded,
resident, storage,
storage::{provided, regions},
};
pub type Shared<'d> = regions::Shared<state::State<'d>>;
impl<'d> resident::Budget<'d> {
pub fn try_copy(
&self,
source: &impl bounded::Source,
) -> Result<Shared<'d>, resident::copy::Error> {
let mut owner = std::mem::MaybeUninit::<Shared<'d>>::uninit();
storage::Owned::try_copy(source, self, &mut owner)?;
Ok(unsafe { owner.assume_init() })
}
}
impl<'d> From<storage::Owned<resident::Budget<'d>>> for Shared<'d> {
#[inline]
fn from(owner: storage::Owned<resident::Budget<'d>>) -> Self {
owner.freeze()
}
}
impl<'d> From<buffer::hold::Retained<'d>> for Shared<'d> {
#[inline]
fn from(owner: buffer::hold::Retained<'d>) -> Self {
owner.into_shared()
}
}
impl<'d> Shared<'d> {
pub fn get(&self, range: ops::Range<usize>) -> Option<buffer::View<'_, Self>> {
buffer::View::new(self).get(range)
}
pub fn scope(&self) -> Option<&Self> {
use resident::Kind;
match &self.owner().0 {
Kind::Owned(_) | Kind::Static => None,
Kind::Slice if self.is_empty() => None,
Kind::Slice
| Kind::Resident(_)
| Kind::Accounted(_)
| Kind::Provided(_)
| Kind::Fixed(_) => Some(self),
}
}
}
impl<'d, L: provided::Layout>
From<bounded::Value<storage::Shared<Option<provided::Account<'d, L>>>>> for Shared<'d>
where
resident::Kind<'d>: From<provided::Account<'d, L>>,
{
fn from(bytes: bounded::Value<storage::Shared<Option<provided::Account<'d, L>>>>) -> Self {
bytes.0.into()
}
}
const _: () = {
assert!(size_of::<Shared<'static>>() == 4 * size_of::<usize>());
assert!(size_of::<Option<Shared<'static>>>() == 4 * size_of::<usize>());
};
const _: () = {
assert!(
size_of::<crate::buffer::raw::Consumed<'static, Shared<'static>, 8>>()
== 2 * size_of::<usize>()
);
assert!(
size_of::<Option<crate::buffer::raw::Consumed<'static, Shared<'static>, 8>>>()
== 2 * size_of::<usize>()
);
};