From 13ec9cc67ee673e17e8765550c479a9082b81e9e Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Wed, 9 Nov 2022 15:07:32 +0100 Subject: [PATCH] Borrowed/Owned for value objects --- implementations/rust/oo/src/domain.rs | 2 +- implementations/rust/oo/src/lib.rs | 19 +- implementations/rust/oo/src/packed/mod.rs | 9 +- implementations/rust/oo/src/reader.rs | 5 +- implementations/rust/oo/src/repr.rs | 284 ++++++++++++++-------- implementations/rust/oo/src/text/mod.rs | 8 +- 6 files changed, 210 insertions(+), 117 deletions(-) diff --git a/implementations/rust/oo/src/domain.rs b/implementations/rust/oo/src/domain.rs index 8380fd9..cb1166b 100644 --- a/implementations/rust/oo/src/domain.rs +++ b/implementations/rust/oo/src/domain.rs @@ -5,7 +5,7 @@ use super::Reader; use super::Writer; use super::ValueImpl; -pub trait Domain: std::fmt::Debug + Eq + std::hash::Hash + Ord + Clone { +pub trait Domain: std::fmt::Debug + Eq + std::hash::Hash + Ord + Clone + 'static { type Decode: DomainDecode + Default; type Encode: DomainEncode + Default; } diff --git a/implementations/rust/oo/src/lib.rs b/implementations/rust/oo/src/lib.rs index 84d1d10..cac3ba2 100644 --- a/implementations/rust/oo/src/lib.rs +++ b/implementations/rust/oo/src/lib.rs @@ -33,6 +33,7 @@ pub use repr::NoValue; pub use repr::Record; pub use repr::Set; pub use repr::Symbol; +pub use repr::Value; pub use repr::ValueImpl; pub use repr::copy_via; pub use repr::iovalue; @@ -58,9 +59,9 @@ pub use writer::write_value; mod demo { use crate::*; - fn getit<'a>(d: &'a dyn ValueImpl, k: &str) -> Option<&'a dyn ValueImpl> { - d.get(&k) - } + // fn getit<'a>(d: &'a dyn ValueImpl, k: &str) -> Option<&'a dyn ValueImpl> { + // d.get(&k) + // } #[test] fn a() { let l: PlainValue = "label".parse().unwrap(); @@ -78,9 +79,9 @@ mod demo { println!("GETw abc {:?}", w.get(&"abc")); println!("GETw 123 {:?}", w.get(&123)); println!("GETw qqq {:?}", w.get(&"qqq")); - println!("GETv abc {:?}", v.get(value(&"abc"))); - println!("GETv 123 {:?}", v.get(value(&123))); - println!("GETv qqq {:?}", v.get(value(&"qqq"))); + println!("GETv abc {:?}", v.get(&*value(&"abc"))); + println!("GETv 123 {:?}", v.get(&*value(&123))); + println!("GETv qqq {:?}", v.get(&*value(&"qqq"))); for (kk, vv) in w.entries() { println!("{:#?} ==> {:#?}", kk, vv); } @@ -95,6 +96,12 @@ mod demo { // } // } } + + #[test] fn value_size() { + println!("Value size {}", std::mem::size_of::>()); + println!("&dyn ValueImpl size {}", std::mem::size_of::<&dyn ValueImpl>()); + println!("Box dyn ValueImpl size {}", std::mem::size_of::>>()); + } } #[cfg(test)] diff --git a/implementations/rust/oo/src/packed/mod.rs b/implementations/rust/oo/src/packed/mod.rs index 9539001..566519a 100644 --- a/implementations/rust/oo/src/packed/mod.rs +++ b/implementations/rust/oo/src/packed/mod.rs @@ -1,5 +1,6 @@ pub mod constants; pub mod reader; +// pub mod view; pub mod writer; pub use reader::PackedReader; @@ -15,10 +16,10 @@ use crate::IOValue; use crate::PlainValue; use crate::Reader; -pub fn from_bytes<'de, D: Domain + 'static, Dec: DomainDecode>( +pub fn from_bytes<'de, D: Domain, Dec: DomainDecode>( bs: &'de [u8], decode_embedded: &mut Dec, -) -> io::Result> { +) -> io::Result> { BytesBinarySource::new(bs).packed().next(false, decode_embedded) } @@ -26,10 +27,10 @@ pub fn iovalue_from_bytes(bs: &[u8]) -> io::Result { super::BytesBinarySource::new(bs).packed().next_iovalue(false) } -pub fn annotated_from_bytes<'de, D: Domain + 'static, Dec: DomainDecode>( +pub fn annotated_from_bytes<'de, D: Domain, Dec: DomainDecode>( bs: &'de [u8], decode_embedded: &mut Dec, -) -> io::Result> { +) -> io::Result> { super::BytesBinarySource::new(bs).packed().next(true, decode_embedded) } diff --git a/implementations/rust/oo/src/reader.rs b/implementations/rust/oo/src/reader.rs index a4213a8..c52d2ad 100644 --- a/implementations/rust/oo/src/reader.rs +++ b/implementations/rust/oo/src/reader.rs @@ -4,6 +4,7 @@ use std::io; use std::marker::PhantomData; use crate::CompoundClass; +use crate::PlainValue; use crate::SignedInteger; use crate::ValueClass; use crate::boundary as B; @@ -142,11 +143,11 @@ pub trait Reader<'de> { } } - fn next>( + fn next<'produced: 'de, D: Domain, Dec: DomainDecode>( &mut self, read_annotations: bool, dec: &mut Dec, - ) -> io::Result>> + ) -> io::Result> { let (anns, v) = match read_annotations { true => self.gather_annotations()?.ok_or_else(io_eof)?, diff --git a/implementations/rust/oo/src/repr.rs b/implementations/rust/oo/src/repr.rs index 4e30ae0..79f8a36 100644 --- a/implementations/rust/oo/src/repr.rs +++ b/implementations/rust/oo/src/repr.rs @@ -1,12 +1,14 @@ use bytemuck::TransparentWrapper; use std::any::Any; -use std::borrow::{Cow, Borrow}; +use std::borrow::Borrow; +use std::borrow::Cow; use std::cmp::Ordering; use std::fmt::Debug; use std::hash::{Hash, Hasher}; use std::io; use std::marker::PhantomData; +use std::ops::Deref; use std::str::FromStr; use std::sync::Arc; use std::vec::Vec; @@ -25,7 +27,88 @@ use crate::domain::{NoEmbeddedDomainCodec, DomainEncode, IOValueDomainCodec}; use super::float::{eq_f32, eq_f64, cmp_f32, cmp_f64}; -pub type PlainValue<'a, D = IOValue> = Box + 'a>; +pub type PlainValue<'va, D = IOValue> = Box + 'va>; + +pub enum Value<'r, D: Domain = IOValue> { + Borrowed(&'r (dyn ValueImpl + 'r)), + Owned(PlainValue<'static, D>), +} + +impl<'r, D: Domain> Value<'r, D> { + pub fn into_owned(self: Value<'r, D>) -> PlainValue<'static, D> { + match self { + Value::Borrowed(r) => return r.value_clone(), + Value::Owned(v) => return v, + } + } +} + +impl<'r, 'va: 'r, D: Domain> From<&'r (dyn ValueImpl + 'r)> for Value<'r, D> { + fn from(r: &'r (dyn ValueImpl + 'r)) -> Self { + Value::Borrowed(r) + } +} + +impl<'r, D: Domain> From> for Value<'r, D> { + fn from(v: PlainValue<'static, D>) -> Self { + Value::Owned(v) + } +} + +impl<'r, D: Domain> Deref for Value<'r, D> { + type Target = dyn ValueImpl + 'r; + + fn deref(&self) -> &Self::Target { + match self { + Value::Borrowed(r) => r, + Value::Owned(v) => v, + } + } +} + +impl<'r, D: Domain> From> for ArcValue { + fn from(v: Value<'r, D>) -> Self { + v.into_owned().into() + } +} + +impl<'r> From> for IOValue { + fn from(v: Value<'r, IOValue>) -> Self { + v.into_owned().into() + } +} + +impl<'r, 'va: 'r, D: Domain> Debug for Value<'r, D> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.deref().fmt(f) + } +} + +impl<'r, 'va: 'r, D: Domain> PartialEq for Value<'r, D> { + fn eq(&self, other: &Self) -> bool { + &**self == &**other + } +} + +impl<'r, 'va: 'r, D: Domain> Eq for Value<'r, D> {} + +impl<'r, 'va: 'r, D: Domain> PartialOrd for Value<'r, D> { + fn partial_cmp(&self, other: &Self) -> Option { + Some((&**self).cmp(&**other)) + } +} + +impl<'r, 'va: 'r, D: Domain> Ord for Value<'r, D> { + fn cmp(&self, other: &Self) -> Ordering { + (&**self).cmp(&**other) + } +} + +impl<'r, 'va: 'r, D: Domain> Hash for Value<'r, D> { + fn hash(&self, state: &mut H) { + (&**self).hash(state) + } +} /// Atomic values from the specification. pub trait ValueImpl { @@ -33,7 +116,7 @@ pub trait ValueImpl { write_value(w, self, enc) } - fn value_clone(&self) -> PlainValue<'static, D> where D: 'static; + fn value_clone(&self) -> PlainValue<'static, D>; fn value_class(&self) -> ValueClass; fn as_boolean(&self) -> Option { None } @@ -48,19 +131,19 @@ pub trait ValueImpl { fn as_symbol(&self) -> Option> { None } fn is_record(&self) -> bool { false } - fn label(&self) -> &dyn ValueImpl { panic!("Not a record") } + fn label(&self) -> Value<'_, D> { panic!("Not a record") } fn is_sequence(&self) -> bool { false } fn len(&self) -> usize { panic!("Has no length") } - fn index(&self, _i: usize) -> &dyn ValueImpl { panic!("Not indexable") } - fn iter(&self) -> Box> + '_> { panic!("Not iterable") } + fn index(&self, _i: usize) -> Value<'_, D> { panic!("Not indexable") } + fn iter(&self) -> Box> + '_> { panic!("Not iterable") } fn is_set(&self) -> bool { false } fn has(&self, _v: &dyn ValueImpl) -> bool { false } fn is_dictionary(&self) -> bool { false } - fn get(&self, _k: &dyn ValueImpl) -> Option<&dyn ValueImpl> { None } - fn entries(&self) -> Box, &dyn ValueImpl)> + '_> { panic!("Not a dictionary") } + fn get(&self, _k: &dyn ValueImpl) -> Option> { None } + fn entries(&self) -> Box, Value<'_, D>)> + '_> { panic!("Not a dictionary") } fn is_embedded(&self) -> bool { false } fn embedded(&self) -> Cow<'_, D> { panic!("Not an embedded value") } @@ -70,11 +153,11 @@ pub trait ValueImpl { fn specialized(&self) -> Option<&dyn Any> { None } } -pub fn value>(v: &V) -> &dyn ValueImpl { - v +pub fn value>(v: &V) -> Value<'_, D> { + Value::Borrowed(v) } -pub fn owned + 'static>(v: V) -> PlainValue<'static, D> { +pub fn owned<'va, D: Domain, V: ValueImpl + 'static>(v: V) -> PlainValue<'va, D> { Box::new(v) } @@ -86,7 +169,7 @@ pub fn iovalue + 'static>(v: V) -> IOValue { IOValue(arcvalue(v)) } -pub fn copy_via( +pub fn copy_via( v: &dyn ValueImpl, f: &mut F, ) -> Result, Err> @@ -95,32 +178,32 @@ where { match v.value_class() { ValueClass::Atomic(a) => Ok(match a { - AtomClass::Boolean => owned(v.as_boolean().unwrap()), - AtomClass::Float => owned(v.as_float().unwrap()), - AtomClass::Double => owned(v.as_double().unwrap()), - AtomClass::SignedInteger => owned(v.as_signed_integer().unwrap()), - AtomClass::String => owned(v.as_string().unwrap().into_owned()), - AtomClass::ByteString => owned(Bytes(v.as_bytestring().unwrap().into_owned())), - AtomClass::Symbol => owned(Symbol(v.as_symbol().unwrap().into_owned())), + AtomClass::Boolean => Box::new(v.as_boolean().unwrap()), + AtomClass::Float => Box::new(v.as_float().unwrap()), + AtomClass::Double => Box::new(v.as_double().unwrap()), + AtomClass::SignedInteger => Box::new(v.as_signed_integer().unwrap()), + AtomClass::String => Box::new(v.as_string().unwrap().into_owned()), + AtomClass::ByteString => Box::new(Bytes(v.as_bytestring().unwrap().into_owned())), + AtomClass::Symbol => Box::new(Symbol(v.as_symbol().unwrap().into_owned())), }), ValueClass::Compound(c) => Ok(match c { CompoundClass::Sequence => - owned(v.iter().map(|w| copy_via(w, f)).collect::, _>>()?), + Box::new(v.iter().map(|w| copy_via(&*w, f)).collect::, _>>()?), CompoundClass::Set => - owned(v.iter().map(|w| copy_via(w, f)).collect::, _>>()?), + Box::new(v.iter().map(|w| copy_via(&*w, f)).collect::, _>>()?), CompoundClass::Record => - owned(Record::new( - copy_via(v.label(), f)?, - v.iter().map(|w| copy_via(w, f)).collect::, _>>()?)), + Box::new(Record::new( + copy_via(&*v.label(), f)?, + v.iter().map(|w| copy_via(&*w, f)).collect::, _>>()?)), CompoundClass::Dictionary => - owned(v.entries().map(|(k, w)| Ok((copy_via(k, f)?, copy_via(w, f)?))) + Box::new(v.entries().map(|(k, w)| Ok((copy_via(&*k, f)?, copy_via(&*w, f)?))) .collect::, _>>()?), }), ValueClass::Embedded => f(&v.embedded()), } } -impl<'a, D: Domain + 'static> From<&'a dyn ValueImpl> for PlainValue<'static, D> { +impl<'a, D: Domain> From<&'a dyn ValueImpl> for PlainValue<'static, D> { fn from(v: &'a dyn ValueImpl) -> Self { v.value_clone() } @@ -128,7 +211,7 @@ impl<'a, D: Domain + 'static> From<&'a dyn ValueImpl> for PlainValue<'static, impl<'a, D: Domain, V: ValueImpl + ?Sized> ValueImpl for &'a V { fn write(&self, w: &mut dyn Writer, enc: &mut dyn DomainEncode) -> io::Result<()> { (*self).write(w, enc) } - fn value_clone(&self) -> PlainValue<'static, D> where D: 'static { (*self).value_clone() } + fn value_clone(&self) -> PlainValue<'static, D> { (*self).value_clone() } fn value_class(&self) -> ValueClass { (*self).value_class() } fn as_boolean(&self) -> Option { (*self).as_boolean() } fn as_float(&self) -> Option { (*self).as_float() } @@ -139,24 +222,24 @@ impl<'a, D: Domain, V: ValueImpl + ?Sized> ValueImpl for &'a V { fn as_bytestring(&self) -> Option> { (*self).as_bytestring() } fn as_symbol(&self) -> Option> { (*self).as_symbol() } fn is_record(&self) -> bool { (*self).is_record() } - fn label(&self) -> &dyn ValueImpl { (*self).label() } + fn label(&self) -> Value<'_, D> { (*self).label() } fn is_sequence(&self) -> bool { (*self).is_sequence() } fn len(&self) -> usize { (*self).len() } - fn index(&self, i: usize) -> &dyn ValueImpl { (*self).index(i) } - fn iter(&self) -> Box> + '_> { (*self).iter() } + fn index(&self, i: usize) -> Value<'_, D> { (*self).index(i) } + fn iter(&self) -> Box> + '_> { (*self).iter() } fn is_set(&self) -> bool { (*self).is_set() } fn has(&self, v: &dyn ValueImpl) -> bool { (*self).has(v) } fn is_dictionary(&self) -> bool { (*self).is_dictionary() } - fn get<'value>(&'value self, k: &dyn ValueImpl) -> Option<&'value dyn ValueImpl> { (*self).get(k) } - fn entries(&self) -> Box, &dyn ValueImpl)> + '_> { (*self).entries() } + fn get(&self, k: &dyn ValueImpl) -> Option> { (*self).get(k) } + fn entries(&self) -> Box, Value<'_, D>)> + '_> { (*self).entries() } fn is_embedded(&self) -> bool { (*self).is_embedded() } fn embedded(&self) -> Cow<'_, D> { (*self).embedded() } fn annotations(&self) -> Option<&[IOValue]> { (*self).annotations() } } -impl<'a, D: Domain> ValueImpl for PlainValue<'a, D> { +impl<'va, D: Domain> ValueImpl for PlainValue<'va, D> { fn write(&self, w: &mut dyn Writer, enc: &mut dyn DomainEncode) -> io::Result<()> { self.as_ref().write(w, enc) } - fn value_clone(&self) -> PlainValue<'static, D> where D: 'static { self.as_ref().value_clone() } + fn value_clone(&self) -> PlainValue<'static, D> { self.as_ref().value_clone() } fn value_class(&self) -> ValueClass { self.as_ref().value_class() } fn as_boolean(&self) -> Option { self.as_ref().as_boolean() } fn as_float(&self) -> Option { self.as_ref().as_float() } @@ -167,16 +250,16 @@ impl<'a, D: Domain> ValueImpl for PlainValue<'a, D> { fn as_bytestring(&self) -> Option> { self.as_ref().as_bytestring() } fn as_symbol(&self) -> Option> { self.as_ref().as_symbol() } fn is_record(&self) -> bool { self.as_ref().is_record() } - fn label(&self) -> &dyn ValueImpl { self.as_ref().label() } + fn label(&self) -> Value<'_, D> { self.as_ref().label() } fn is_sequence(&self) -> bool { self.as_ref().is_sequence() } fn len(&self) -> usize { self.as_ref().len() } - fn index(&self, i: usize) -> &dyn ValueImpl { self.as_ref().index(i) } - fn iter(&self) -> Box> + '_> { self.as_ref().iter() } + fn index(&self, i: usize) -> Value<'_, D> { self.as_ref().index(i) } + fn iter(&self) -> Box> + '_> { self.as_ref().iter() } fn is_set(&self) -> bool { self.as_ref().is_set() } fn has(&self, v: &dyn ValueImpl) -> bool { self.as_ref().has(v) } fn is_dictionary(&self) -> bool { self.as_ref().is_dictionary() } - fn get<'value>(&'value self, k: &dyn ValueImpl) -> Option<&'value dyn ValueImpl> { self.as_ref().get(k) } - fn entries(&self) -> Box, &dyn ValueImpl)> + '_> { self.as_ref().entries() } + fn get(&self, k: &dyn ValueImpl) -> Option> { self.as_ref().get(k) } + fn entries(&self) -> Box, Value<'_, D>)> + '_> { self.as_ref().entries() } fn is_embedded(&self) -> bool { self.as_ref().is_embedded() } fn embedded(&self) -> Cow<'_, D> { self.as_ref().embedded() } fn annotations(&self) -> Option<&[IOValue]> { self.as_ref().annotations() } @@ -188,11 +271,11 @@ impl<'a, D: Domain> Debug for dyn ValueImpl + 'a { } } -impl<'a, Err: Into, D: Domain + FromStr + 'static> FromStr for PlainValue<'a, D> { +impl, D: Domain + FromStr> FromStr for PlainValue<'static, D> { type Err = io::Error; fn from_str(s: &str) -> Result { - crate::annotated_from_str(s, &mut DefaultDomainCodec) + Ok(crate::annotated_from_str(s, &mut DefaultDomainCodec)?.value_clone()) } } @@ -233,8 +316,8 @@ impl<'a, D: Domain> Hash for dyn ValueImpl + 'a { } fn iters_eq<'a, D: Domain>( - mut i1: Box> + 'a>, - mut i2: Box> + 'a>, + mut i1: Box> + 'a>, + mut i2: Box> + 'a>, ) -> bool { loop { match i1.next() { @@ -293,8 +376,8 @@ impl<'a, D: Domain> PartialEq for dyn ValueImpl + 'a { } fn iters_cmp<'a, D: Domain>( - mut i1: Box> + 'a>, - mut i2: Box> + 'a>, + mut i1: Box> + 'a>, + mut i2: Box> + 'a>, ) -> Ordering { loop { match i1.next() { @@ -304,7 +387,7 @@ fn iters_cmp<'a, D: Domain>( } Some(v1) => match i2.next() { None => return Ordering::Greater, - Some(v2) => match v1.cmp(v2) { + Some(v2) => match v1.cmp(&v2) { Ordering::Equal => (), other => return other, } @@ -335,7 +418,7 @@ impl<'a, D: Domain> Ord for dyn ValueImpl + 'a { }, ValueClass::Compound(c) => match c { CompoundClass::Record => - self.label().cmp(other.label()).then_with( + self.label().cmp(&other.label()).then_with( || iters_cmp(self.iter(), other.iter())), CompoundClass::Sequence => iters_cmp(self.iter(), other.iter()), CompoundClass::Set => { @@ -374,7 +457,7 @@ pub enum Atom<'a> { } impl<'a> Atom<'a> { - pub fn into_value(self) -> PlainValue<'static, D> { + pub fn into_value<'b: 'a, D: Domain>(self) -> PlainValue<'b, D> { match self { Atom::Boolean(b) => Box::new(b), Atom::Float(f) => Box::new(f), @@ -414,7 +497,7 @@ impl<'a, D: Domain> ValueImpl for Atom<'a> { } } - fn value_clone(&self) -> PlainValue<'static, D> where D: 'static { + fn value_clone(&self) -> PlainValue<'static, D> { self.clone().into_value() } @@ -470,20 +553,20 @@ impl FromStr for NoValue { impl ValueImpl for NoValue { fn write(&self, _w: &mut dyn Writer, _enc: &mut dyn DomainEncode) -> io::Result<()> { unreachable!() } - fn value_clone(&self) -> PlainValue<'static, D> where D: 'static { unreachable!() } + fn value_clone(&self) -> PlainValue<'static, D> { unreachable!() } fn value_class(&self) -> ValueClass { unreachable!() } } impl ValueImpl for bool { fn write(&self, w: &mut dyn Writer, __enc: &mut dyn DomainEncode) -> io::Result<()> { w.write_bool(*self) } - fn value_clone(&self) -> PlainValue<'static, D> where D: 'static { Box::new(*self) } + fn value_clone(&self) -> PlainValue<'static, D> { Box::new(*self) } fn value_class(&self) -> ValueClass { ValueClass::Atomic(AtomClass::Boolean) } fn as_boolean(&self) -> Option { Some(*self) } } impl ValueImpl for u64 { fn write(&self, w: &mut dyn Writer, _enc: &mut dyn DomainEncode) -> io::Result<()> { w.write_u64(*self) } - fn value_clone(&self) -> PlainValue<'static, D> where D: 'static { Box::new(*self) } + fn value_clone(&self) -> PlainValue<'static, D> { Box::new(*self) } fn value_class(&self) -> ValueClass { ValueClass::Atomic(AtomClass::SignedInteger) } fn as_signed_integer(&self) -> Option { Some((*self).into()) @@ -492,7 +575,7 @@ impl ValueImpl for u64 { impl ValueImpl for SignedInteger { fn write(&self, w: &mut dyn Writer, _enc: &mut dyn DomainEncode) -> io::Result<()> { w.write_signed_integer(self) } - fn value_clone(&self) -> PlainValue<'static, D> where D: 'static { Box::new(self.clone()) } + fn value_clone(&self) -> PlainValue<'static, D> { Box::new(self.clone()) } fn value_class(&self) -> ValueClass { ValueClass::Atomic(AtomClass::SignedInteger) } fn as_signed_integer(&self) -> Option { Some(self.clone()) @@ -501,7 +584,7 @@ impl ValueImpl for SignedInteger { impl ValueImpl for f32 { fn write(&self, w: &mut dyn Writer, _enc: &mut dyn DomainEncode) -> io::Result<()> { w.write_f32(*self) } - fn value_clone(&self) -> PlainValue<'static, D> where D: 'static { Box::new(*self) } + fn value_clone(&self) -> PlainValue<'static, D> { Box::new(*self) } fn value_class(&self) -> ValueClass { ValueClass::Atomic(AtomClass::Float) } fn as_float(&self) -> Option { Some(*self) } fn as_double(&self) -> Option { Some(*self as f64) } @@ -509,7 +592,7 @@ impl ValueImpl for f32 { impl ValueImpl for f64 { fn write(&self, w: &mut dyn Writer, _enc: &mut dyn DomainEncode) -> io::Result<()> { w.write_f64(*self) } - fn value_clone(&self) -> PlainValue<'static, D> where D: 'static { Box::new(*self) } + fn value_clone(&self) -> PlainValue<'static, D> { Box::new(*self) } fn value_class(&self) -> ValueClass { ValueClass::Atomic(AtomClass::Float) } fn as_float(&self) -> Option { Some(*self as f32) } fn as_double(&self) -> Option { Some(*self) } @@ -517,14 +600,14 @@ impl ValueImpl for f64 { impl ValueImpl for str { fn write(&self, w: &mut dyn Writer, _enc: &mut dyn DomainEncode) -> io::Result<()> { w.write_string(self) } - fn value_clone(&self) -> PlainValue<'static, D> where D: 'static { Box::new(self.to_owned()) } + fn value_clone(&self) -> PlainValue<'static, D> { Box::new(self.to_owned()) } fn value_class(&self) -> ValueClass { ValueClass::Atomic(AtomClass::String) } fn as_string(&self) -> Option> { Some(Cow::Borrowed(self)) } } impl ValueImpl for String { fn write(&self, w: &mut dyn Writer, _enc: &mut dyn DomainEncode) -> io::Result<()> { w.write_string(self) } - fn value_clone(&self) -> PlainValue<'static, D> where D: 'static { Box::new(self.clone()) } + fn value_clone(&self) -> PlainValue<'static, D> { Box::new(self.clone()) } fn value_class(&self) -> ValueClass { ValueClass::Atomic(AtomClass::String) } fn as_string(&self) -> Option> { Some(Cow::Borrowed(self)) } } @@ -541,7 +624,7 @@ impl + Debug> Bytes { impl + Debug, D: Domain> ValueImpl for Bytes { fn write(&self, w: &mut dyn Writer, _enc: &mut dyn DomainEncode) -> io::Result<()> { w.write_bytes(self.0.as_ref()) } - fn value_clone(&self) -> PlainValue<'static, D> where D: 'static { Box::new(Bytes(self.0.as_ref().to_owned())) } + fn value_clone(&self) -> PlainValue<'static, D> { 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())) } } @@ -558,7 +641,7 @@ impl + Debug> Symbol { impl + Debug, D: Domain> ValueImpl for Symbol { fn write(&self, w: &mut dyn Writer, _enc: &mut dyn DomainEncode) -> io::Result<()> { w.write_symbol(self.0.as_ref()) } - fn value_clone(&self) -> PlainValue<'static, D> where D: 'static { Box::new(Symbol(self.0.as_ref().to_owned())) } + fn value_clone(&self) -> PlainValue<'static, D> { 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())) } } @@ -599,16 +682,16 @@ impl> ValueImpl for Record { w.end_record() } - fn value_clone(&self) -> PlainValue<'static, D> where D: 'static { + fn value_clone(&self) -> PlainValue<'static, D> { Box::new(Record(self.0.iter().map(|v| v.value_clone()).collect())) } fn value_class(&self) -> ValueClass { ValueClass::Compound(CompoundClass::Record) } fn is_record(&self) -> bool { true } - fn label(&self) -> &dyn ValueImpl { &self.0[0] } + fn label(&self) -> Value<'_, D> { Value::Borrowed(&self.0[0]) } fn len(&self) -> usize { self.0.len() - 1 } - fn index(&self, i: usize) -> &dyn ValueImpl { &self.0[i + 1] } - fn iter(&self) -> Box> + '_> { + fn index(&self, i: usize) -> Value<'_, D> { Value::Borrowed(&self.0[i + 1]) } + fn iter(&self) -> Box> + '_> { Box::new(self.0[1..].iter().map(value)) } } @@ -618,15 +701,15 @@ impl> ValueImpl for Vec { (&self[..]).write(w, enc) } - fn value_clone(&self) -> PlainValue<'static, D> where D: 'static { + fn value_clone(&self) -> PlainValue<'static, D> { (&self[..]).value_clone() } fn value_class(&self) -> ValueClass { ValueClass::Compound(CompoundClass::Sequence) } fn is_sequence(&self) -> bool { true } fn len(&self) -> usize { self.len() } - fn index(&self, i: usize) -> &dyn ValueImpl { &self[i] } - fn iter(&self) -> Box> + '_> { + fn index(&self, i: usize) -> Value<'_, D> { Value::Borrowed(&self[i]) } + fn iter(&self) -> Box> + '_> { Box::new(self[..].iter().map(value)) } } @@ -645,20 +728,20 @@ impl> ValueImpl for [V] { w.end_sequence() } - fn value_clone(&self) -> PlainValue<'static, D> where D: 'static { + fn value_clone(&self) -> PlainValue<'static, D> { Box::new(self.iter().map(|v| v.value_clone()).collect::>()) } fn value_class(&self) -> ValueClass { ValueClass::Compound(CompoundClass::Sequence) } fn is_sequence(&self) -> bool { true } fn len(&self) -> usize { self.len() } - fn index(&self, i: usize) -> &dyn ValueImpl { &self[i] } - fn iter(&self) -> Box> + '_> { + fn index(&self, i: usize) -> Value<'_, D> { Value::Borrowed(&self[i]) } + fn iter(&self) -> Box> + '_> { Box::new(self[..].iter().map(value)) } } -impl<'e, D: Domain, E: for<'a> Borrow> + Ord + 'e> ValueImpl for Set { +impl Borrow> + Ord + 'static> ValueImpl for Set { fn write(&self, w: &mut dyn Writer, enc: &mut dyn DomainEncode) -> io::Result<()> { w.start_set()?; let mut b = B::Type::default(); @@ -672,7 +755,7 @@ impl<'e, D: Domain, E: for<'a> Borrow> + Ord + 'e> ValueImpl for S w.end_set() } - fn value_clone(&self) -> PlainValue<'static, D> where D: 'static { + fn value_clone(&self) -> PlainValue<'static, D> { Box::new(self.iter().map(|v| Key::peel_ref(&v.borrow()).value_clone()).collect::>()) } @@ -680,8 +763,8 @@ impl<'e, D: Domain, E: for<'a> Borrow> + Ord + 'e> ValueImpl for S fn is_set(&self) -> bool { true } fn len(&self) -> usize { self.len() } fn has(&self, v: &dyn ValueImpl) -> bool { self.contains(&Key::wrap_ref(v)) } - fn iter(&self) -> Box> + '_> { - Box::new(self.iter().map(|e| Key::peel_ref(&e.borrow()))) + fn iter(&self) -> Box> + '_> { + Box::new(self.iter().map(|e| Value::Borrowed(Key::peel_ref(&e.borrow())))) } } @@ -706,7 +789,7 @@ impl<'a, 'b: 'a, D: Domain> Borrow> for &'b (dyn ValueImpl + 'b) { } } -impl<'k, D: Domain, V: ValueImpl, K: for<'a> Borrow> + Ord + 'k> ValueImpl +impl + 'static, K: for<'a> Borrow> + Ord + 'static> ValueImpl for Map { fn write(&self, w: &mut dyn Writer, enc: &mut dyn DomainEncode) -> io::Result<()> { @@ -725,7 +808,7 @@ impl<'k, D: Domain, V: ValueImpl, K: for<'a> Borrow> + Ord + 'k> V w.end_dictionary() } - fn value_clone(&self) -> PlainValue<'static, D> where D: 'static { + fn value_clone(&self) -> PlainValue<'static, D> { Box::new(ValueImpl::entries(self).map(|(k, v)| (k.value_clone(), v.value_clone())).collect::>()) } @@ -733,14 +816,15 @@ impl<'k, D: Domain, V: ValueImpl, K: for<'a> Borrow> + Ord + 'k> V fn is_dictionary(&self) -> bool { true } fn len(&self) -> usize { self.len() } fn has(&self, v: &dyn ValueImpl) -> bool { self.contains_key(&Key::wrap_ref(v)) } - fn get(&self, k: &dyn ValueImpl) -> Option<&dyn ValueImpl> { + fn get(&self, k: &dyn ValueImpl) -> Option> { match Map::get(self, &Key::wrap_ref(k)) { - Some(v) => Some(v), + Some(v) => Some(Value::Borrowed(v)), None => None, } } - fn entries(&self) -> Box, &dyn ValueImpl)> + '_> { - Box::new(self.iter().map(|(k,v)| (Key::peel_ref(&k.borrow()), value(v)))) + fn entries(&self) -> Box, Value<'_, D>)> + '_> { + Box::new(self.iter().map( + |(k,v)| (Value::Borrowed(Key::peel_ref(&k.borrow())), value(v)))) } } @@ -769,14 +853,14 @@ impl ValueImpl for Embedded { w.end_embedded() } - fn value_clone(&self) -> PlainValue<'static, D> where D: 'static { Box::new(self.clone()) } + fn value_clone(&self) -> PlainValue<'static, D> { Box::new(self.clone()) } fn value_class(&self) -> ValueClass { ValueClass::Embedded } fn is_embedded(&self) -> bool { true } fn embedded(&self) -> Cow<'_, D> { Cow::Borrowed(&self.0) } } #[derive(Debug)] -pub struct Annotations>(V, Vec, PhantomData); +pub struct Annotations + 'static>(V, Vec, PhantomData); impl> Annotations { pub fn new(value: V, anns: Vec) -> Self { @@ -809,7 +893,7 @@ impl> ValueImpl for Annotations { } } - fn value_clone(&self) -> PlainValue<'static, D> where D: 'static { + fn value_clone(&self) -> PlainValue<'static, D> { Box::new(Annotations(self.0.value_clone(), self.1.iter().map(|v| v.value_clone().into()).collect(), PhantomData)) @@ -825,16 +909,16 @@ impl> ValueImpl for Annotations { fn as_bytestring(&self) -> Option> { self.value().as_bytestring() } fn as_symbol(&self) -> Option> { self.value().as_symbol() } fn is_record(&self) -> bool { self.value().is_record() } - fn label(&self) -> &dyn ValueImpl { self.value().label() } + fn label(&self) -> Value<'_, D> { self.value().label() } fn is_sequence(&self) -> bool { self.value().is_sequence() } fn len(&self) -> usize { self.value().len() } - fn index(&self, i: usize) -> &dyn ValueImpl { self.value().index(i) } - fn iter(&self) -> Box> + '_> { self.value().iter() } + fn index(&self, i: usize) -> Value<'_, D> { self.value().index(i) } + fn iter(&self) -> Box> + '_> { self.value().iter() } fn is_set(&self) -> bool { self.value().is_set() } fn has(&self, v: &dyn ValueImpl) -> bool { self.value().has(v) } fn is_dictionary(&self) -> bool { self.value().is_dictionary() } - fn get(&self, k: &dyn ValueImpl) -> Option<&dyn ValueImpl> { self.value().get(k) } - fn entries(&self) -> Box, &dyn ValueImpl)> + '_> { self.value().entries() } + fn get(&self, k: &dyn ValueImpl) -> Option> { self.value().get(k) } + fn entries(&self) -> Box, Value<'_, D>)> + '_> { self.value().entries() } fn is_embedded(&self) -> bool { self.value().is_embedded() } fn embedded(&self) -> Cow<'_, D> { self.value().embedded() } fn annotations(&self) -> Option<&[IOValue]> { Some(&self.1) } @@ -868,7 +952,7 @@ impl> Ord for Annotations { #[derive(Clone, Eq, Hash, PartialOrd, Ord)] #[repr(transparent)] -pub struct ArcValue(Arc>); +pub struct ArcValue(Arc>); #[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] #[repr(transparent)] @@ -892,11 +976,11 @@ impl PartialEq for ArcValue { } } -impl, D: Domain + FromStr + 'static> FromStr for ArcValue { +impl, D: Domain + FromStr> FromStr for ArcValue { type Err = io::Error; fn from_str(s: &str) -> Result { - Ok(arcvalue(crate::annotated_from_str(s, &mut DefaultDomainCodec)?)) + Ok(arcvalue(crate::annotated_from_str(s, &mut DefaultDomainCodec)?.value_clone())) } } @@ -951,16 +1035,16 @@ impl ValueImpl for ArcValue { fn as_bytestring(&self) -> Option> { self.0.as_bytestring() } fn as_symbol(&self) -> Option> { self.0.as_symbol() } fn is_record(&self) -> bool { self.0.is_record() } - fn label(&self) -> &dyn ValueImpl { self.0.label() } + fn label(&self) -> Value<'_, D> { self.0.label() } fn is_sequence(&self) -> bool { self.0.is_sequence() } fn len(&self) -> usize { self.0.len() } - fn index(&self, i: usize) -> &dyn ValueImpl { self.0.index(i) } - fn iter(&self) -> Box> + '_> { self.0.iter() } + fn index(&self, i: usize) -> Value<'_, D> { self.0.index(i) } + fn iter(&self) -> Box> + '_> { self.0.iter() } fn is_set(&self) -> bool { self.0.is_set() } fn has(&self, v: &dyn ValueImpl) -> bool { self.0.has(v) } fn is_dictionary(&self) -> bool { self.0.is_dictionary() } - fn get<'value>(&'value self, k: &dyn ValueImpl) -> Option<&'value dyn ValueImpl> { self.0.get(k) } - fn entries(&self) -> Box, &dyn ValueImpl)> + '_> { self.0.entries() } + fn get(&self, k: &dyn ValueImpl) -> Option> { self.0.get(k) } + fn entries(&self) -> Box, Value<'_, D>)> + '_> { self.0.entries() } fn is_embedded(&self) -> bool { self.0.is_embedded() } fn embedded(&self) -> Cow<'_, D> { self.0.embedded() } fn annotations(&self) -> Option<&[IOValue]> { self.0.annotations() } @@ -979,16 +1063,16 @@ impl ValueImpl for IOValue { fn as_bytestring(&self) -> Option> { self.0.as_bytestring() } fn as_symbol(&self) -> Option> { self.0.as_symbol() } fn is_record(&self) -> bool { self.0.is_record() } - fn label(&self) -> &dyn ValueImpl { self.0.label() } + fn label(&self) -> Value<'_, IOValue> { self.0.label() } fn is_sequence(&self) -> bool { self.0.is_sequence() } fn len(&self) -> usize { self.0.len() } - fn index(&self, i: usize) -> &dyn ValueImpl { self.0.index(i) } - fn iter(&self) -> Box> + '_> { self.0.iter() } + fn index(&self, i: usize) -> Value<'_, IOValue> { self.0.index(i) } + fn iter(&self) -> Box> + '_> { self.0.iter() } fn is_set(&self) -> bool { self.0.is_set() } fn has(&self, v: &dyn ValueImpl) -> bool { self.0.has(v) } fn is_dictionary(&self) -> bool { self.0.is_dictionary() } - fn get<'value>(&'value self, k: &dyn ValueImpl) -> Option<&'value dyn ValueImpl> { self.0.get(k) } - fn entries(&self) -> Box, &dyn ValueImpl)> + '_> { self.0.entries() } + fn get(&self, k: &dyn ValueImpl) -> Option> { self.0.get(k) } + fn entries(&self) -> Box, Value<'_, IOValue>)> + '_> { self.0.entries() } fn is_embedded(&self) -> bool { self.0.is_embedded() } fn embedded(&self) -> Cow<'_, IOValue> { self.0.embedded() } fn annotations(&self) -> Option<&[IOValue]> { self.0.annotations() } diff --git a/implementations/rust/oo/src/text/mod.rs b/implementations/rust/oo/src/text/mod.rs index 23f922b..73aa688 100644 --- a/implementations/rust/oo/src/text/mod.rs +++ b/implementations/rust/oo/src/text/mod.rs @@ -14,10 +14,10 @@ use crate::IOValue; use crate::PlainValue; use crate::Reader; -pub fn from_str<'de, D: Domain + 'static, Dec: DomainDecode>( +pub fn from_str<'de, D: Domain, Dec: DomainDecode>( s: &'de str, decode_embedded: &mut Dec, -) -> io::Result> { +) -> io::Result> { BytesBinarySource::new(s.as_bytes()).text().next(false, decode_embedded) } @@ -25,10 +25,10 @@ pub fn iovalue_from_str(s: &str) -> io::Result { BytesBinarySource::new(s.as_bytes()).text().next_iovalue(false) } -pub fn annotated_from_str<'de, D: Domain + 'static, Dec: DomainDecode>( +pub fn annotated_from_str<'de, D: Domain, Dec: DomainDecode>( s: &'de str, decode_embedded: &mut Dec, -) -> io::Result> { +) -> io::Result> { BytesBinarySource::new(s.as_bytes()).text().next(true, decode_embedded) }