Avoidance of hardcoded use of IOValue in Embedded reading

This commit is contained in:
Tony Garnock-Jones 2021-07-05 11:50:40 +02:00
parent 41fe3c3440
commit 8f1a83e548
2 changed files with 21 additions and 9 deletions

View File

@ -1,24 +1,38 @@
use super::BinarySource;
use super::Embeddable; use super::Embeddable;
use super::IOResult; use super::IOResult;
use super::IOValue; use super::IOValue;
use super::Reader;
use super::packed::PackedReader;
pub trait DomainDecode<D: Embeddable> { pub trait DomainDecode<D: Embeddable> {
fn decode_embedded_iovalue(&mut self, v: IOValue) -> IOResult<D>; fn decode_embedded<'de, 'src, S: BinarySource<'de>>(
&mut self,
src: &'src mut S,
read_annotations: bool,
) -> IOResult<D>;
} }
pub struct IOValueDomainDecode; pub struct IOValueDomainDecode;
impl DomainDecode<IOValue> for IOValueDomainDecode { impl DomainDecode<IOValue> for IOValueDomainDecode {
fn decode_embedded_iovalue(&mut self, v: IOValue) -> IOResult<IOValue> { fn decode_embedded<'de, 'src, S: BinarySource<'de>>(
Ok(v) &mut self,
src: &'src mut S,
read_annotations: bool,
) -> IOResult<IOValue> {
PackedReader::new(src, IOValueDomainDecode).demand_next(read_annotations)
} }
} }
pub struct NoEmbeddedDomainDecode; pub struct NoEmbeddedDomainDecode;
impl<D: Embeddable> DomainDecode<D> for NoEmbeddedDomainDecode { impl<D: Embeddable> DomainDecode<D> for NoEmbeddedDomainDecode {
fn decode_embedded_iovalue(&mut self, _v: IOValue) -> IOResult<D> { fn decode_embedded<'de, 'src, S: BinarySource<'de>>(
Err(std::io::Error::new(std::io::ErrorKind::Unsupported, &mut self,
"Embedded values not supported here")) _src: &'src mut S,
_read_annotations: bool,
) -> IOResult<D> {
Err(std::io::Error::new(std::io::ErrorKind::Unsupported, "Embedded values not supported here"))
} }
} }

View File

@ -13,7 +13,6 @@ use super::super::{
DomainDecode, DomainDecode,
Embeddable, Embeddable,
IOResult, IOResult,
IOValueDomainDecode,
Map, Map,
NestedValue, NestedValue,
Record, Record,
@ -268,8 +267,7 @@ impl<'de, 'src, D: Embeddable, N: NestedValue<D>, Dec: DomainDecode<D>, S: Binar
} }
} }
Tag::Embedded => { Tag::Embedded => {
let v = PackedReader::new(self.source, IOValueDomainDecode).demand_next(read_annotations)?; Value::Embedded(self.decode_embedded.decode_embedded(self.source, read_annotations)?).wrap()
Value::Embedded(self.decode_embedded.decode_embedded_iovalue(v)?).wrap()
} }
Tag::SmallInteger(v) => { Tag::SmallInteger(v) => {
// TODO: prebuild these in value.rs // TODO: prebuild these in value.rs