Require that domain toing and froing be total

This commit is contained in:
Tony Garnock-Jones 2021-01-29 13:42:17 +01:00
parent 532e811894
commit 6fd06cec98
4 changed files with 17 additions and 22 deletions

View File

@ -22,11 +22,15 @@ mod dom {
}
impl Domain for Dom {
fn as_preserves(&self) -> Result<IOValue, std::io::Error> {
Ok(match self {
fn from_preserves(v: IOValue) -> Self {
panic!("Cannot decode IOValue to Dom: {:?}", v);
}
fn as_preserves(&self) -> IOValue {
match self {
Dom::One => Value::ByteString(vec![255, 255, 255, 255]).wrap(),
Dom::Two => Value::symbol(&format!("Dom::{:?}", self)).wrap(),
})
}
}
}

View File

@ -264,7 +264,7 @@ impl<'de, S: BinarySource<'de>> Reader<'de> for PackedReader<'de, S> {
}
Tag::Pointer => {
let v = self.demand_next(read_annotations)?;
Value::Domain(IOValue::from_preserves(v)?).wrap()
Value::Domain(IOValue::from_preserves(v)).wrap()
}
Tag::SmallInteger(v) => {
// TODO: prebuild these in value.rs

View File

@ -19,17 +19,8 @@ use super::signed_integer::SignedInteger;
use crate::error::{Error, ExpectedKind, Received};
pub trait Domain: Sized + Debug + Clone + Eq + Hash + Ord {
fn from_preserves(v: IOValue) -> Result<Self, std::io::Error> {
Err(std::io::Error::new(std::io::ErrorKind::InvalidData,
format!("Cannot Preserves-decode domain-specific value {:?}",
v)))
}
fn as_preserves(&self) -> Result<IOValue, std::io::Error> {
Err(std::io::Error::new(std::io::ErrorKind::InvalidData,
format!("Cannot Preserves-encode domain-specific value {:?}",
self)))
}
fn from_preserves(v: IOValue) -> Self;
fn as_preserves(&self) -> IOValue;
}
pub trait NestedValue<D: Domain>: Sized + Debug + Clone + Eq + Hash + Ord {
@ -55,11 +46,11 @@ pub trait NestedValue<D: Domain>: Sized + Debug + Clone + Eq + Hash + Ord {
}
fn to_io_value(&self) -> IOValue {
self.copy_via(&|d| d.as_preserves().unwrap().value().clone())
self.copy_via(&|d| d.as_preserves().value().clone())
}
fn from_io_value(v: &IOValue) -> Self {
v.copy_via(&|_| unreachable!())
v.copy_via(&|d| Value::Domain(D::from_preserves(d.clone())))
}
}
@ -1055,12 +1046,12 @@ pub struct IOValue(Arc<AnnotatedValue<IOValue, IOValue>>);
pub type UnwrappedIOValue = Value<IOValue, IOValue>;
impl Domain for IOValue {
fn from_preserves(v: IOValue) -> Result<Self, std::io::Error> {
Ok(v)
fn from_preserves(v: IOValue) -> Self {
v
}
fn as_preserves(&self) -> Result<IOValue, std::io::Error> {
Ok(self.clone())
fn as_preserves(&self) -> IOValue {
self.clone()
}
}

View File

@ -135,7 +135,7 @@ pub trait Writer: Sized {
}
Value::Domain(ref d) => {
let mut c = self.start_pointer()?;
c.write(&d.as_preserves()?)?;
c.write(&d.as_preserves())?;
self.end_pointer(c)
}
}