use calandria::RetainedBytes;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct FrameMeasure {
wire_bytes: usize,
retained_bytes: RetainedBytes,
}
impl FrameMeasure {
pub fn capture<F>(frame: &F) -> Self
where
F: WriteFrame + ?Sized,
{
Self {
wire_bytes: frame.bytes().len(),
retained_bytes: frame.retained_bytes(),
}
}
pub const fn wire_bytes(self) -> usize {
self.wire_bytes
}
pub const fn retained_bytes(self) -> RetainedBytes {
self.retained_bytes
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum FrameContractViolation {
WireLengthChanged {
measured: usize,
observed: usize,
},
WriteRangeUnavailable {
measured: usize,
written: usize,
observed: usize,
},
}
pub trait WriteFrame {
fn bytes(&self) -> &[u8];
fn retained_bytes(&self) -> RetainedBytes;
}
impl core::fmt::Display for FrameContractViolation {
fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
formatter.write_str(match self {
Self::WireLengthChanged { .. } => "frame wire length changed after commit",
Self::WriteRangeUnavailable { .. } => {
"frame byte view no longer contains the committed write range"
}
})
}
}
impl core::error::Error for FrameContractViolation {}