use crate::HasValue; use crate::coders::{Decoder, Encoder}; use crate::data_result::DataResult; use crate::dynamic_ops::DynamicOps; use crate::map_codec::MapCodec; use crate::struct_builder::StructBuilder; use std::fmt::Display; /// A [`Codec`] implementation for a [`MapCodec`]. /// /// The `MapCodec` held by this `Codec` can either be *owned* or a static reference (*borrowed*). pub enum MapCodecCodec { Owned(C), Borrowed(&'static C), } impl MapCodecCodec { const fn codec(&self) -> &C { match self { Self::Owned(c) => c, Self::Borrowed(c) => c, } } } impl HasValue for MapCodecCodec { type Value = C::Value; } impl Encoder for MapCodecCodec { fn encode( &self, input: &Self::Value, ops: &'static impl DynamicOps, prefix: T, ) -> DataResult { self.codec() .encode(input, ops, self.codec().builder(ops)) .build(prefix) } } impl Decoder for MapCodecCodec { fn decode( &self, input: T, ops: &'static impl DynamicOps, ) -> DataResult<(Self::Value, T)> { self.codec() .compressed_decode(input.clone(), ops) .map(|a| (a, input)) } }