From 13f1f80b0125a3d10e93b5c78218673031233c9f Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Thu, 3 Nov 2022 17:40:30 +0100 Subject: [PATCH] More --- implementations/rust/oo/src/domain.rs | 12 ++-- implementations/rust/oo/src/reader.rs | 93 +++++++-------------------- implementations/rust/oo/src/repr.rs | 20 +++--- 3 files changed, 40 insertions(+), 85 deletions(-) diff --git a/implementations/rust/oo/src/domain.rs b/implementations/rust/oo/src/domain.rs index d3ada3a..b22e4a2 100644 --- a/implementations/rust/oo/src/domain.rs +++ b/implementations/rust/oo/src/domain.rs @@ -11,7 +11,7 @@ pub trait Domain: std::fmt::Debug + Eq + std::hash::Hash + Ord + Clone { } pub trait DomainDecode { - fn decode_embedded<'de, R: Reader<'de>>( + fn decode_embedded<'de, R: Reader<'de> + ?Sized>( &mut self, r: &mut R, read_annotations: bool, @@ -27,7 +27,7 @@ pub trait DomainEncode { } impl<'a, D: Domain, T: DomainDecode> DomainDecode for &'a mut T { - fn decode_embedded<'de, R: Reader<'de>>( + fn decode_embedded<'de, R: Reader<'de> + ?Sized>( &mut self, r: &mut R, read_annotations: bool, @@ -50,7 +50,7 @@ impl<'a, D: Domain, T: DomainEncode> DomainEncode for &'a mut T { pub struct DefaultDomainCodec; impl DomainDecode for DefaultDomainCodec { - fn decode_embedded<'de, R: Reader<'de>>( + fn decode_embedded<'de, R: Reader<'de> + ?Sized>( &mut self, r: &mut R, read_annotations: bool, @@ -73,7 +73,7 @@ impl DomainEncode for DefaultDomainCodec { pub struct DebugDomainCodec; impl, D: Domain + std::str::FromStr> DomainDecode for DebugDomainCodec { - fn decode_embedded<'de, R: Reader<'de>>( + fn decode_embedded<'de, R: Reader<'de> + ?Sized>( &mut self, r: &mut R, _read_annotations: bool, @@ -96,7 +96,7 @@ impl DomainEncode for DebugDomainCodec { pub struct NoEmbeddedDomainCodec; impl DomainDecode for NoEmbeddedDomainCodec { - fn decode_embedded<'de, R: Reader<'de>>( + fn decode_embedded<'de, R: Reader<'de> + ?Sized>( &mut self, _r: &mut R, _read_annotations: bool, @@ -124,7 +124,7 @@ impl Domain for IOValue { } impl DomainDecode for IOValueDomainCodec { - fn decode_embedded<'de, R: Reader<'de>>( + fn decode_embedded<'de, R: Reader<'de> + ?Sized>( &mut self, r: &mut R, read_annotations: bool, diff --git a/implementations/rust/oo/src/reader.rs b/implementations/rust/oo/src/reader.rs index 5528a06..a142f51 100644 --- a/implementations/rust/oo/src/reader.rs +++ b/implementations/rust/oo/src/reader.rs @@ -7,6 +7,9 @@ use crate::CompoundClass; use crate::SignedInteger; use crate::ValueClass; use crate::boundary as B; +use crate::domain::Domain; +use crate::domain::DomainDecode; +use crate::domain::IOValueDomainCodec; use crate::error::Error; use crate::error::ExpectedKind; use crate::error::io_eof; @@ -18,7 +21,7 @@ use crate::repr::Map; use crate::repr::Record; use crate::repr::Set; use crate::repr::Value; -use crate::repr::iovalue; +use crate::repr::owned; pub type ReaderResult = std::result::Result; @@ -139,7 +142,12 @@ pub trait Reader<'de> { } } - fn next_iovalue(&mut self, read_annotations: bool) -> io::Result { + fn next>( + &mut self, + read_annotations: bool, + dec: &mut Dec, + ) -> io::Result>> + { let (anns, v) = match read_annotations { true => self.gather_annotations()?.ok_or_else(io_eof)?, false => (Vec::new(), self.skip_annotations()?.ok_or_else(io_eof)?), @@ -149,7 +157,7 @@ pub trait Reader<'de> { self.next_atom()?.into_value(), ValueClass::Embedded => { self.open_embedded()?; - let v = self.next_iovalue(read_annotations)?; + let v = dec.decode_embedded(self, read_annotations)?; self.close_embedded()?; Box::new(Embedded::new(v)) } @@ -158,9 +166,9 @@ pub trait Reader<'de> { self.open_record()?; let mut b = B::start(B::Item::RecordLabel); self.boundary(&b)?; - vs.push(self.next_iovalue(read_annotations)?); + vs.push(self.next(read_annotations, dec)?); while !self.close_compound(&mut b, &B::Item::RecordField)? { - vs.push(self.next_iovalue(read_annotations)?); + vs.push(self.next(read_annotations, dec)?); } Box::new(Record::_from_vec(vs)) } @@ -169,7 +177,7 @@ pub trait Reader<'de> { self.open_sequence()?; let mut b = B::Type::default(); while !self.close_compound(&mut b, &B::Item::SequenceValue)? { - vs.push(self.next_iovalue(read_annotations)?); + vs.push(self.next(read_annotations, dec)?); } Box::new(vs) } @@ -178,7 +186,7 @@ pub trait Reader<'de> { self.open_set()?; let mut b = B::Type::default(); while !self.close_compound(&mut b, &B::Item::SetValue)? { - s.insert(self.next_iovalue(read_annotations)?); + s.insert(self.next(read_annotations, dec)?); } Box::new(s) } @@ -187,21 +195,26 @@ pub trait Reader<'de> { self.open_dictionary()?; let mut b = B::Type::default(); while !self.close_compound(&mut b, &B::Item::DictionaryKey)? { - let k = self.next_iovalue(read_annotations)?; + let k = self.next(read_annotations, dec)?; b.shift(Some(B::Item::DictionaryValue)); self.boundary(&b)?; - d.insert(k, self.next_iovalue(read_annotations)?); + d.insert(k, self.next(read_annotations, dec)?); } Box::new(d) } }; if anns.is_empty() { - Ok(value.into()) + Ok(value) } else { - Ok(iovalue(Annotations::new(value, anns))) + Ok(owned(Annotations::new(value, anns))) } } + fn next_iovalue(&mut self, read_annotations: bool) -> io::Result + { + Ok(self.next(read_annotations, &mut IOValueDomainCodec)?.into()) + } + fn next_boolean(&mut self) -> ReaderResult { self.next_iovalue(false)?.as_boolean().ok_or(Error::Expected(ExpectedKind::Boolean)) } @@ -271,64 +284,6 @@ pub trait Reader<'de> { } } -impl<'r, 'de, R: Reader<'de>> Reader<'de> for &'r mut R { - fn peek_class(&mut self) -> io::Result> { - (*self).peek_class() - } - - fn next_atom(&mut self) -> ReaderResult> { - (*self).next_atom() - } - - fn boundary(&mut self, b: &B::Type) -> ReaderResult<()> { - (*self).boundary(b) - } - - fn open_record(&mut self) -> ReaderResult<()> { - (*self).open_record() - } - - fn open_sequence(&mut self) -> ReaderResult<()> { - (*self).open_sequence() - } - - fn open_set(&mut self) -> ReaderResult<()> { - (*self).open_set() - } - - fn open_dictionary(&mut self) -> ReaderResult<()> { - (*self).open_dictionary() - } - - fn close_compound(&mut self, b: &mut B::Type, i: &B::Item) -> ReaderResult { - (*self).close_compound(b, i) - } - - fn open_embedded(&mut self) -> ReaderResult<()> { - (*self).open_embedded() - } - - fn close_embedded(&mut self) -> ReaderResult<()> { - (*self).close_embedded() - } - - fn open_annotation(&mut self) -> ReaderResult<()> { - (*self).open_annotation() - } - - fn close_annotation(&mut self) -> ReaderResult<()> { - (*self).close_annotation() - } - - fn mark(&mut self) -> io::Result { - (*self).mark() - } - - fn restore(&mut self, mark: usize) -> io::Result<()> { - (*self).restore(mark) - } -} - pub struct IOValues<'de, R: Reader<'de>> { pub reader: R, pub read_annotations: bool, diff --git a/implementations/rust/oo/src/repr.rs b/implementations/rust/oo/src/repr.rs index 8de22cb..a3b17be 100644 --- a/implementations/rust/oo/src/repr.rs +++ b/implementations/rust/oo/src/repr.rs @@ -322,7 +322,7 @@ impl<'a> Atom<'a> { } impl<'a, D: Domain> Value for Atom<'a> { - fn write(&self, w: &mut dyn Writer, enc: &mut dyn DomainEncode) -> io::Result<()> { + fn write(&self, w: &mut dyn Writer, _enc: &mut dyn DomainEncode) -> io::Result<()> { match self { Atom::Boolean(b) => w.write_bool(*b), Atom::Float(f) => w.write_f32(*f), @@ -395,14 +395,14 @@ impl Value for NoValue { } impl Value for bool { - fn write(&self, w: &mut dyn Writer, enc: &mut dyn DomainEncode) -> io::Result<()> { w.write_bool(*self) } + fn write(&self, w: &mut dyn Writer, __enc: &mut dyn DomainEncode) -> io::Result<()> { w.write_bool(*self) } fn value_clone(&self) -> Box> where D: 'static { Box::new(*self) } fn value_class(&self) -> ValueClass { ValueClass::Atomic(AtomClass::Boolean) } fn as_boolean(&self) -> Option { Some(*self) } } impl Value for u64 { - fn write(&self, w: &mut dyn Writer, enc: &mut dyn DomainEncode) -> io::Result<()> { w.write_u64(*self) } + fn write(&self, w: &mut dyn Writer, _enc: &mut dyn DomainEncode) -> io::Result<()> { w.write_u64(*self) } fn value_clone(&self) -> Box> where D: 'static { Box::new(*self) } fn value_class(&self) -> ValueClass { ValueClass::Atomic(AtomClass::SignedInteger) } fn as_signed_integer(&self) -> Option { @@ -411,7 +411,7 @@ impl Value for u64 { } impl Value for SignedInteger { - fn write(&self, w: &mut dyn Writer, enc: &mut dyn DomainEncode) -> io::Result<()> { w.write_signed_integer(self) } + fn write(&self, w: &mut dyn Writer, _enc: &mut dyn DomainEncode) -> io::Result<()> { w.write_signed_integer(self) } fn value_clone(&self) -> Box> where D: 'static { Box::new(self.clone()) } fn value_class(&self) -> ValueClass { ValueClass::Atomic(AtomClass::SignedInteger) } fn as_signed_integer(&self) -> Option { @@ -420,7 +420,7 @@ impl Value for SignedInteger { } impl Value for f32 { - fn write(&self, w: &mut dyn Writer, enc: &mut dyn DomainEncode) -> io::Result<()> { w.write_f32(*self) } + fn write(&self, w: &mut dyn Writer, _enc: &mut dyn DomainEncode) -> io::Result<()> { w.write_f32(*self) } fn value_clone(&self) -> Box> where D: 'static { Box::new(*self) } fn value_class(&self) -> ValueClass { ValueClass::Atomic(AtomClass::Float) } fn as_float(&self) -> Option { Some(*self) } @@ -428,7 +428,7 @@ impl Value for f32 { } impl Value for f64 { - fn write(&self, w: &mut dyn Writer, enc: &mut dyn DomainEncode) -> io::Result<()> { w.write_f64(*self) } + fn write(&self, w: &mut dyn Writer, _enc: &mut dyn DomainEncode) -> io::Result<()> { w.write_f64(*self) } fn value_clone(&self) -> Box> where D: 'static { Box::new(*self) } fn value_class(&self) -> ValueClass { ValueClass::Atomic(AtomClass::Float) } fn as_float(&self) -> Option { Some(*self as f32) } @@ -436,14 +436,14 @@ impl Value for f64 { } impl Value for str { - fn write(&self, w: &mut dyn Writer, enc: &mut dyn DomainEncode) -> io::Result<()> { w.write_string(self) } + fn write(&self, w: &mut dyn Writer, _enc: &mut dyn DomainEncode) -> io::Result<()> { w.write_string(self) } fn value_clone(&self) -> Box> where D: 'static { Box::new(self.to_owned()) } fn value_class(&self) -> ValueClass { ValueClass::Atomic(AtomClass::String) } fn as_string(&self) -> Option> { Some(Cow::Borrowed(self)) } } impl Value for String { - fn write(&self, w: &mut dyn Writer, enc: &mut dyn DomainEncode) -> io::Result<()> { w.write_string(self) } + fn write(&self, w: &mut dyn Writer, _enc: &mut dyn DomainEncode) -> io::Result<()> { w.write_string(self) } fn value_clone(&self) -> Box> where D: 'static { Box::new(self.clone()) } fn value_class(&self) -> ValueClass { ValueClass::Atomic(AtomClass::String) } fn as_string(&self) -> Option> { Some(Cow::Borrowed(self)) } @@ -454,7 +454,7 @@ impl Value for String { pub struct Bytes>(T); impl + Debug, D: Domain> Value for Bytes { - fn write(&self, w: &mut dyn Writer, enc: &mut dyn DomainEncode) -> io::Result<()> { w.write_bytes(self.0.as_ref()) } + fn write(&self, w: &mut dyn Writer, _enc: &mut dyn DomainEncode) -> io::Result<()> { w.write_bytes(self.0.as_ref()) } fn value_clone(&self) -> Box> where D: 'static { Box::new(Bytes(self.0.as_ref().to_owned())) } fn value_class(&self) -> ValueClass { ValueClass::Atomic(AtomClass::ByteString) } fn as_bytestring(&self) -> Option> { Some(Cow::Borrowed(self.0.as_ref())) } @@ -471,7 +471,7 @@ impl + Debug> Symbol { } impl + Debug, D: Domain> Value for Symbol { - fn write(&self, w: &mut dyn Writer, enc: &mut dyn DomainEncode) -> io::Result<()> { w.write_symbol(self.0.as_ref()) } + fn write(&self, w: &mut dyn Writer, _enc: &mut dyn DomainEncode) -> io::Result<()> { w.write_symbol(self.0.as_ref()) } fn value_clone(&self) -> Box> where D: 'static { Box::new(Symbol(self.0.as_ref().to_owned())) } fn value_class(&self) -> ValueClass { ValueClass::Atomic(AtomClass::Symbol) } fn as_symbol(&self) -> Option> { Some(Cow::Borrowed(self.0.as_ref())) }