use alloc::string::String;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct AccessPolicy {
allow_copy: bool,
}
impl Default for AccessPolicy {
fn default() -> Self {
Self::allow_copy()
}
}
impl AccessPolicy {
pub fn allow_copy() -> Self {
Self { allow_copy: true }
}
pub fn zero_copy() -> Self {
Self { allow_copy: false }
}
pub fn copy_allowed(&self) -> bool {
self.allow_copy
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AccessError {
WouldCopy,
Read(String),
}
impl core::fmt::Display for AccessError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::WouldCopy => f.write_str("access would require a copy, which the policy forbids"),
Self::Read(msg) => write!(f, "failed to materialize bytes: {msg}"),
}
}
}
impl core::error::Error for AccessError {}
#[derive(Clone, Copy, Debug, Default)]
pub struct Reader {
pub(crate) policy: AccessPolicy,
}
impl Reader {
pub fn new() -> Self {
Self::default()
}
pub fn no_copy(mut self) -> Self {
self.policy = AccessPolicy::zero_copy();
self
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct Writer {
pub(crate) policy: AccessPolicy,
}
impl Writer {
pub fn new() -> Self {
Self::default()
}
pub fn no_copy(mut self) -> Self {
self.policy = AccessPolicy::zero_copy();
self
}
}