use std::{cell::Cell, marker, ops};
use o3::buffer::{
self,
bytes::scope,
resident::{
self,
staging::{self, reserve},
},
storage::{self, range},
write::{
Sink as _,
consume::{self, Consume},
},
};
pub mod domain;
pub mod pool;
pub mod provided;
pub mod scatter;
pub struct Bounds<'a> {
pub range: (ops::Bound<usize>, ops::Bound<usize>),
pub calls: &'a Cell<u16>,
}
impl ops::RangeBounds<usize> for Bounds<'_> {
fn start_bound(&self) -> ops::Bound<&usize> {
self.calls.set(self.calls.get() * 10 + 1);
self.range.0.as_ref()
}
fn end_bound(&self) -> ops::Bound<&usize> {
self.calls.set(self.calls.get() * 10 + 2);
self.range.1.as_ref()
}
}
impl Drop for Bounds<'_> {
fn drop(&mut self) {
self.calls.set(self.calls.get() * 10 + 3);
}
}
pub struct Selected<'id> {
range: ops::Range<usize>,
brand: marker::PhantomData<*mut &'id ()>,
}
impl<'id> Selected<'id> {
pub fn try_new(
source: &scope::Owner<'id, storage::Shared<resident::State<'_>>>,
range: ops::Range<usize>,
) -> Result<Self, range::Error> {
source.view().try_get(range.clone())?;
Ok(Self {
range,
brand: marker::PhantomData,
})
}
}
unsafe impl<'id> scope::raw::Select<'id> for Selected<'id> {
fn bounds(self) -> ops::Range<usize> {
self.range
}
}
#[derive(PartialEq, Eq)]
pub enum Reject {
Requested,
}
pub struct Redirect {
pub source: [u8; 3],
pub target: storage::Shared,
}
impl Consume for Redirect {
fn remaining(&self) -> usize {
self.source.len()
}
fn try_consume(&mut self, amount: usize) -> Result<(), consume::Error> {
self.target.try_consume(amount)
}
fn consume_max(&mut self, max: usize) -> usize {
self.target.consume_max(max)
}
}
pub struct Counted {
pub bytes: [u8; 3],
pub reads: Cell<Option<usize>>,
}
unsafe impl buffer::raw::Source for Counted {}
pub struct Resident<'a> {
pub bytes: usize,
pub reads: &'a Cell<usize>,
}
impl buffer::Residency for Resident<'_> {
fn resident(&self) -> usize {
self.reads.set(self.reads.get() + 1);
self.bytes
}
}
impl AsRef<[u8]> for Counted {
fn as_ref(&self) -> &[u8] {
crate::common::bump(&self.reads);
&self.bytes
}
}
pub struct Local(pub staging::Layout);
impl Local {
pub fn try_new() -> Result<Self, staging::Error> {
let plan = staging::Plan::new(1, 8)?;
staging::Layout::new(1, plan, 0, 12).map(Self)
}
pub fn try_buf<'d>(
&self,
budget: &resident::Budget<'d>,
) -> Result<staging::Buf<'d>, reserve::Error> {
let pool = staging::Pool::try_new(budget, self.0).map_err(reserve::Error::Resident)?;
let mut buf = staging::Buf::try_from(&pool)
.map_err(resident::AllocError::Retain)
.map_err(reserve::Error::Resident)?;
buf.try_reserve(4)?;
buf.write_slice(b"abcd")?;
Ok(buf)
}
}