diff --git a/src/codec.rs b/src/codec.rs index cfba3d2..6c11a9f 100644 --- a/src/codec.rs +++ b/src/codec.rs @@ -70,14 +70,15 @@ pub fn consolidate_slice(slice: &mut [(T, usize)]) -> usize { offset } +/// A region that encodes its data in a codec `C`. #[derive(Default, Debug)] -struct CodecRegion { +pub struct CodecRegion { inner: CopyRegion, - codec: DictionaryCodec, + codec: C, staging: Vec, } -impl Region for CodecRegion { +impl Region for CodecRegion { type ReadItem<'a> = &'a [u8] where Self: 'a; @@ -92,8 +93,8 @@ impl Region for CodecRegion { let codecs = regions.clone().map(|r| &r.codec).collect::>(); let codecs: Result<[_; 2], _> = codecs.try_into(); let codec = match codecs { - Ok(codecs) => DictionaryCodec::new_from(codecs), - Err(_) => DictionaryCodec::default(), + Ok(codecs) => C::new_from(codecs), + Err(_) => C::default(), }; Self { inner: CopyRegion::merge_regions(regions.map(|r| &r.inner)), @@ -120,20 +121,14 @@ impl Region for CodecRegion { } } -impl CopyOnto for &[u8] { - fn copy_onto(self, target: &mut CodecRegion) -> ::Index { +impl CopyOnto> for &[u8] { + fn copy_onto(self, target: &mut CodecRegion) -> as Region>::Index { target.staging.clear(); target.codec.encode(self, &mut target.staging); target.staging.as_slice().copy_onto(&mut target.inner) } } -impl<'a> CopyOnto for std::iter::Once<&'a [u8]> { - fn copy_onto(mut self, target: &mut CodecRegion) -> ::Index { - self.next().unwrap_or(&[]).copy_onto(target) - } -} - /// TODO pub trait Codec: Default + 'static { /// Decodes an input byte slice into a sequence of byte slices. @@ -370,12 +365,12 @@ mod misra_gries { #[cfg(test)] mod tests { - use super::{Codec, CodecRegion}; + use super::{Codec, CodecRegion, DictionaryCodec}; use crate::*; #[test] fn test_simple() { - let mut r = CodecRegion::default(); + let mut r = CodecRegion::::default(); for _ in 0..1000 { let index = "abc".as_bytes().copy_onto(&mut r);