use o3::buffer::{CapacityError, FixedPoolCapacity, Lease, Pooled, RuntimePoolCapacity, Shared};
pub(crate) mod private {
pub unsafe trait Sealed {}
}
#[allow(private_bounds)]
pub trait StableBytes: private::Sealed + AsRef<[u8]> {}
impl<T> StableBytes for T where T: private::Sealed + AsRef<[u8]> {}
unsafe impl private::Sealed for Shared {}
unsafe impl private::Sealed for Pooled {}
unsafe impl private::Sealed for Lease<'_, RuntimePoolCapacity> {}
unsafe impl<const CAP: u32> private::Sealed for Lease<'_, FixedPoolCapacity<CAP>> {}
unsafe impl private::Sealed for &'static [u8] {}
unsafe impl<const N: usize> private::Sealed for &'static [u8; N] {}
pub struct StaticBytes<T> {
bytes: &'static [u8],
owner: T,
}
impl<T> StaticBytes<T> {
pub const fn new(bytes: &'static [u8], owner: T) -> Self {
Self { bytes, owner }
}
pub const fn owner(&self) -> &T {
&self.owner
}
pub fn into_owner(self) -> T {
self.owner
}
}
impl<T> AsRef<[u8]> for StaticBytes<T> {
fn as_ref(&self) -> &[u8] {
self.bytes
}
}
unsafe impl<T> private::Sealed for StaticBytes<T> {}
pub struct LeaseBuffer<'d> {
lease: Lease<'d>,
overflowed: bool,
}
impl<'d> LeaseBuffer<'d> {
pub const fn new(lease: Lease<'d>) -> Self {
Self {
lease,
overflowed: false,
}
}
pub const fn overflowed(&self) -> bool {
self.overflowed
}
pub fn try_push(&mut self, byte: u8) -> Result<(), CapacityError> {
let result = self.lease.try_push(byte);
self.overflowed |= result.is_err();
result
}
pub fn try_extend_from_slice(&mut self, src: &[u8]) -> Result<(), CapacityError> {
let result = self.lease.try_extend_from_slice(src);
self.overflowed |= result.is_err();
result
}
pub fn len(&self) -> usize {
self.lease.len()
}
pub fn is_empty(&self) -> bool {
self.lease.is_empty()
}
pub fn as_mut_slice(&mut self) -> &mut [u8] {
self.lease.as_mut_slice()
}
pub fn into_lease(self) -> Lease<'d> {
self.lease
}
}
impl AsRef<[u8]> for LeaseBuffer<'_> {
fn as_ref(&self) -> &[u8] {
self.lease.as_ref()
}
}
unsafe impl private::Sealed for LeaseBuffer<'_> {}