use std::{borrow::Borrow, fmt};
use crate::buffer::{self, hold, resident, storage, write::consume};
pub trait Seal:
Sized + Borrow<storage::Shared<Self::Backing>> + From<storage::Shared<Self::Backing>>
{
type Backing: buffer::raw::Retain;
type Retention: Default + fmt::Debug;
const EMPTY: Self;
fn try_copy(
source: Result<buffer::raw::Slices<'_, 1>, consume::Error>,
domain: &Self::Retention,
) -> Result<storage::NonEmpty<Self>, hold::CopyError>;
}
impl Seal for storage::Shared {
type Backing = Option<storage::raw::Tag>;
type Retention = ();
const EMPTY: Self = Self::new();
fn try_copy(
source: Result<buffer::raw::Slices<'_, 1>, consume::Error>,
_: &Self::Retention,
) -> Result<storage::NonEmpty<Self>, hold::CopyError> {
let source = source
.map_err(storage::AllocError::Capacity)
.map_err(hold::CopyError::Storage)?;
let owner = source
.try_owned()
.map_err(storage::AllocError::Exhausted)
.map_err(hold::CopyError::Storage)?
.into_shared();
Ok(unsafe { buffer::raw::Source::nonempty(owner) })
}
}
impl<'d> Seal for storage::Shared<resident::State<'d>> {
type Backing = resident::State<'d>;
type Retention = Option<resident::Budget<'d>>;
const EMPTY: Self = Self::new();
fn try_copy(
source: Result<buffer::raw::Slices<'_, 1>, consume::Error>,
domain: &Self::Retention,
) -> Result<storage::NonEmpty<Self>, hold::CopyError> {
let source = source
.map_err(resident::AllocError::Capacity)
.map_err(hold::CopyError::Resident)?;
let budget = domain
.as_ref()
.ok_or(hold::CopyError::Authority(resident::BindError::Unbound))?;
let owner = source
.try_copy(budget)
.map_err(resident::AllocError::from)
.map_err(hold::CopyError::Resident)?
.into_shared();
Ok(unsafe { buffer::raw::Source::nonempty(owner) })
}
}