diff --git a/implementations/rust/src/lib.rs b/implementations/rust/src/lib.rs index bba7940..635680b 100644 --- a/implementations/rust/src/lib.rs +++ b/implementations/rust/src/lib.rs @@ -22,11 +22,15 @@ mod dom { } impl Domain for Dom { - fn as_preserves(&self) -> Result { - 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(), - }) + } } } diff --git a/implementations/rust/src/value/packed/reader.rs b/implementations/rust/src/value/packed/reader.rs index e838381..3da0cd4 100644 --- a/implementations/rust/src/value/packed/reader.rs +++ b/implementations/rust/src/value/packed/reader.rs @@ -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 diff --git a/implementations/rust/src/value/value.rs b/implementations/rust/src/value/value.rs index 32213e0..c0613b7 100644 --- a/implementations/rust/src/value/value.rs +++ b/implementations/rust/src/value/value.rs @@ -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 { - Err(std::io::Error::new(std::io::ErrorKind::InvalidData, - format!("Cannot Preserves-decode domain-specific value {:?}", - v))) - } - - fn as_preserves(&self) -> Result { - 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: Sized + Debug + Clone + Eq + Hash + Ord { @@ -55,11 +46,11 @@ pub trait NestedValue: 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>); pub type UnwrappedIOValue = Value; impl Domain for IOValue { - fn from_preserves(v: IOValue) -> Result { - Ok(v) + fn from_preserves(v: IOValue) -> Self { + v } - fn as_preserves(&self) -> Result { - Ok(self.clone()) + fn as_preserves(&self) -> IOValue { + self.clone() } } diff --git a/implementations/rust/src/value/writer.rs b/implementations/rust/src/value/writer.rs index f74dd43..5a05560 100644 --- a/implementations/rust/src/value/writer.rs +++ b/implementations/rust/src/value/writer.rs @@ -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) } }