#[cfg(feature = "alloc")]
use alloc::borrow::ToOwned;
#[cfg(feature = "alloc")]
use core::{borrow, borrow::Borrow as _, ops::Deref};
#[cfg(feature = "alloc")]
pub struct OwnedCodec<Codec> {
pub codec: Codec,
}
#[cfg(feature = "alloc")]
impl<Codec> OwnedCodec<Codec> {
pub const fn new(codec: Codec) -> Self {
Self { codec }
}
}
#[cfg(feature = "alloc")]
impl<'encoded, 'decoded, Codec> crate::Decoder<'encoded, 'decoded> for OwnedCodec<Codec>
where
Codec: crate::Decoder<'encoded, 'decoded>,
Codec::Decoded: Deref,
<Codec::Decoded as Deref>::Target: ToOwned,
{
type Decoded = <<Codec::Decoded as Deref>::Target as ToOwned>::Owned;
fn decode(
&self,
encoded: &'encoded [u8],
offset: &mut usize,
) -> Result<Self::Decoded, crate::DecodeError> {
let borrowed = self.codec.decode(encoded, offset)?;
Ok(borrowed.deref().to_owned())
}
}
#[cfg(feature = "alloc")]
impl<Codec> crate::Encoder for OwnedCodec<Codec>
where
Codec: crate::Encoder,
Codec::Decoded: ToOwned,
<Codec::Decoded as ToOwned>::Owned: borrow::Borrow<Codec::Decoded>,
{
type Decoded = <Codec::Decoded as ToOwned>::Owned;
fn encode(
&self,
decoded: &Self::Decoded,
encoded: &mut [u8],
offset: &mut usize,
) -> Result<(), crate::EncodeError> {
self.codec.encode(decoded.borrow(), encoded, offset)
}
}
#[cfg(feature = "alloc")]
impl<Codec> crate::Measurer for OwnedCodec<Codec>
where
Codec: crate::Measurer,
Codec::Decoded: ToOwned,
<Codec::Decoded as ToOwned>::Owned: borrow::Borrow<Codec::Decoded>,
{
type Decoded = <Codec::Decoded as ToOwned>::Owned;
fn measure(&self, decoded: &Self::Decoded) -> Result<usize, crate::EncodeError> {
self.codec.measure(decoded.borrow())
}
}
#[cfg(feature = "alloc")]
impl<Codec> crate::FixedMeasurer for OwnedCodec<Codec>
where
Codec: crate::FixedMeasurer,
Codec::Decoded: ToOwned,
<Codec::Decoded as ToOwned>::Owned: borrow::Borrow<Codec::Decoded>,
{
fn measure_fixed(&self) -> usize {
self.codec.measure_fixed()
}
}
pub trait ConstantCoder {
type Decoded;
fn constant(&self) -> &Self::Decoded;
}
pub struct PhantomCodec<T>(pub T);
impl<T> PhantomCodec<T>
where
T: Clone,
{
pub const fn new(value: T) -> Self {
Self(value)
}
}
impl<T> ConstantCoder for PhantomCodec<T> {
type Decoded = T;
fn constant(&self) -> &Self::Decoded {
&self.0
}
}
impl<'encoded, 'decoded, T> crate::Decoder<'encoded, 'decoded> for PhantomCodec<T>
where
T: Clone + 'decoded,
{
type Decoded = T;
fn decode(
&self,
_encoded: &'encoded [u8],
_offset: &mut usize,
) -> Result<Self::Decoded, crate::DecodeError> {
Ok(self.0.clone())
}
}
impl<T> crate::Encoder for PhantomCodec<T>
where
T: Clone + PartialEq,
{
type Decoded = T;
fn encode(
&self,
_decoded: &Self::Decoded,
_encoded: &mut [u8],
_offset: &mut usize,
) -> Result<(), crate::EncodeError> {
if _decoded.ne(&self.0) {
return Err(crate::EncodeError::InvalidData);
}
Ok(())
}
}
impl<T> crate::Measurer for PhantomCodec<T>
where
T: Clone + PartialEq,
{
type Decoded = T;
fn measure(&self, _decoded: &Self::Decoded) -> Result<usize, crate::EncodeError> {
if _decoded.ne(&self.0) {
return Err(crate::EncodeError::InvalidData);
}
Ok(0)
}
}
impl<T> crate::FixedMeasurer for PhantomCodec<T>
where
T: Clone + PartialEq,
{
fn measure_fixed(&self) -> usize {
0
}
}