use crate::MemVec;
#[allow(clippy::len_without_is_empty)]
pub trait Memory
where
Self: core::ops::Deref<Target = [u8]> + core::ops::DerefMut<Target = [u8]>,
{
type Error: core::fmt::Debug;
fn as_ptr(&self) -> *const u8;
fn as_mut_ptr(&mut self) -> *mut u8;
fn len(&self) -> usize;
fn len_mut(&mut self) -> &mut usize;
fn reserve(&mut self, capacity: usize) -> Result<(), Self::Error>;
fn shrink_to(&mut self, capacity: usize) -> Result<(), Self::Error>;
unsafe fn try_into_memvec<'a, T: Copy>(
self,
) -> Result<MemVec<'a, T, Self>, (Self, MemoryLayoutError)>
where
Self: Sized,
{
MemVec::try_from_memory(self)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MemoryLayoutError {
MisalignedMemory,
CapacityExceeded,
}
impl core::fmt::Display for MemoryLayoutError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
MemoryLayoutError::MisalignedMemory => {
write!(f, "memory is not properly aligned for the target type")
}
MemoryLayoutError::CapacityExceeded => {
write!(f, "logical length exceeds available memory capacity")
}
}
}
}
impl core::error::Error for MemoryLayoutError {}