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::IOResult;
use super::IOValue;
use super::Reader;
use super::packed::PackedReader;
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;
impl DomainDecode<IOValue> for IOValueDomainDecode {
fn decode_embedded_iovalue(&mut self, v: IOValue) -> IOResult<IOValue> {
Ok(v)
fn decode_embedded<'de, 'src, S: BinarySource<'de>>(
&mut self,
src: &'src mut S,
read_annotations: bool,
) -> IOResult<IOValue> {
PackedReader::new(src, IOValueDomainDecode).demand_next(read_annotations)
}
}
pub struct NoEmbeddedDomainDecode;
impl<D: Embeddable> DomainDecode<D> for NoEmbeddedDomainDecode {
fn decode_embedded_iovalue(&mut self, _v: IOValue) -> IOResult<D> {
Err(std::io::Error::new(std::io::ErrorKind::Unsupported,
"Embedded values not supported here"))
fn decode_embedded<'de, 'src, S: BinarySource<'de>>(
&mut self,
_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,
Embeddable,
IOResult,
IOValueDomainDecode,
Map,
NestedValue,
Record,
@ -268,8 +267,7 @@ impl<'de, 'src, D: Embeddable, N: NestedValue<D>, Dec: DomainDecode<D>, S: Binar
}
}
Tag::Embedded => {
let v = PackedReader::new(self.source, IOValueDomainDecode).demand_next(read_annotations)?;
Value::Embedded(self.decode_embedded.decode_embedded_iovalue(v)?).wrap()
Value::Embedded(self.decode_embedded.decode_embedded(self.source, read_annotations)?).wrap()
}
Tag::SmallInteger(v) => {
// TODO: prebuild these in value.rs