From f1b37ac9af88a1bc88c327ce63be0e423cac840c Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Sat, 12 Oct 2019 15:40:35 +0100 Subject: [PATCH] First tentative step to allowing reuse of Value with direct containment, Rc and Arc --- implementations/rust/src/lib.rs | 73 +++---- implementations/rust/src/symbol.rs | 6 +- implementations/rust/src/value/codec.rs | 20 +- implementations/rust/src/value/de.rs | 57 ++--- implementations/rust/src/value/decoder.rs | 36 ++-- implementations/rust/src/value/encoder.rs | 16 +- implementations/rust/src/value/mod.rs | 8 +- implementations/rust/src/value/ser.rs | 2 +- implementations/rust/src/value/value.rs | 248 ++++++++++++++-------- 9 files changed, 268 insertions(+), 198 deletions(-) diff --git a/implementations/rust/src/lib.rs b/implementations/rust/src/lib.rs index 4d7af3d..63c0f55 100644 --- a/implementations/rust/src/lib.rs +++ b/implementations/rust/src/lib.rs @@ -5,10 +5,9 @@ pub mod symbol; mod ieee754_section_5_10_total_order_tests { use std::cmp::Ordering::{Less, Equal, Greater}; - use crate::value::Value; - // fn v(val: T) -> Value where Value: std::convert::From { Value::from(val) } - fn f(val: f32) -> Value { Value::from(val) } - fn d(val: f64) -> Value { Value::from(val) } + use crate::value::{Value, AValue}; + fn f(val: f32) -> Value { Value::from(val) } + fn d(val: f64) -> Value { Value::from(val) } // TODO: Test cases with a few different signalling and non-signalling NaNs @@ -98,78 +97,80 @@ mod ieee754_section_5_10_total_order_tests { #[cfg(test)] mod value_tests { - use crate::value::Value; + use crate::value::{Value, AValue}; use num::bigint::BigInt; + type VV = Value; + #[test] fn boolean_mut() { - let mut b = Value::Boolean(true); + let mut b = VV::Boolean(true); assert!(b.is_boolean()); *(b.as_boolean_mut().unwrap()) = false; - assert_eq!(b, Value::Boolean(false)); + assert_eq!(b, VV::Boolean(false)); } #[test] fn float_mut() { - let mut f = Value::from(1.0f32); + let mut f = VV::from(1.0f32); assert!(f.is_float()); *(f.as_float_mut().unwrap()) = 123.45; - assert_eq!(f, Value::from(123.45f32)); + assert_eq!(f, VV::from(123.45f32)); assert_eq!(f.as_float().unwrap(), 123.45f32); } #[test] fn double_mut() { - let mut f = Value::from(1.0); + let mut f = VV::from(1.0); assert!(f.is_double()); *(f.as_double_mut().unwrap()) = 123.45; - assert_eq!(f, Value::from(123.45)); + assert_eq!(f, VV::from(123.45)); assert_eq!(f.as_double().unwrap(), 123.45); } #[test] fn signedinteger_mut() { - let mut i = Value::from(123); + let mut i = VV::from(123); assert!(i.is_signedinteger()); *(i.as_signedinteger_mut().unwrap()) = BigInt::from(234); - assert_eq!(i, Value::from(234)); + assert_eq!(i, VV::from(234)); use num::traits::cast::ToPrimitive; assert_eq!(i.as_signedinteger().unwrap().to_i64().unwrap(), 234); } #[test] fn string_mut() { - let mut s = Value::from("hello, world!"); + let mut s = VV::from("hello, world!"); assert!(s.is_string()); s.as_string_mut().unwrap().replace_range(7..12, "there"); - assert_eq!(s, Value::from("hello, there!")); + assert_eq!(s, VV::from("hello, there!")); } #[test] fn bytes_mut() { - let mut b = Value::from(&b"hello, world!"[..]); + let mut b = VV::from(&b"hello, world!"[..]); assert!(b.is_bytestring()); b.as_bytestring_mut().unwrap().splice(7..12, Vec::from(&b"there"[..])); - assert_eq!(b, Value::from(&b"hello, there!"[..])); + assert_eq!(b, VV::from(&b"hello, there!"[..])); } #[test] fn symbol_mut() { - let mut s = Value::symbol("abcd"); + let mut s = VV::symbol("abcd"); assert!(s.is_symbol()); s.as_symbol_mut().unwrap().replace_range(..2, "AB"); - assert_eq!(s, Value::symbol("ABcd")); + assert_eq!(s, VV::symbol("ABcd")); } #[test] fn record_mut() { - let says = Value::symbol("says").wrap(); - let mut r = Value::record(says.clone(), vec![Value::from("Tony").wrap(), Value::from("Hello!").wrap()]); - r.as_record_mut().unwrap().1[0] = Value::from("Alice").wrap(); - assert_eq!(r, Value::record(says, vec![Value::from("Alice").wrap(), Value::from("Hello!").wrap()])); + let says = VV::symbol("says").wrap(); + let mut r = VV::record(says.clone(), vec![VV::from("Tony").wrap(), VV::from("Hello!").wrap()]); + r.as_record_mut().unwrap().1[0] = VV::from("Alice").wrap(); + assert_eq!(r, VV::record(says, vec![VV::from("Alice").wrap(), VV::from("Hello!").wrap()])); } #[test] fn sequence_mut() { - let mut s = Value::Sequence(vec![Value::from(1).wrap(), - Value::from(2).wrap(), - Value::from(3).wrap()]); - let r = Value::Sequence(vec![Value::from(1).wrap(), - Value::from(99).wrap(), - Value::from(3).wrap()]); - s.as_sequence_mut().unwrap()[1] = Value::from(99).wrap(); + let mut s = VV::Sequence(vec![VV::from(1).wrap(), + VV::from(2).wrap(), + VV::from(3).wrap()]); + let r = VV::Sequence(vec![VV::from(1).wrap(), + VV::from(99).wrap(), + VV::from(3).wrap()]); + s.as_sequence_mut().unwrap()[1] = VV::from(99).wrap(); assert_eq!(r, s); } } @@ -177,11 +178,11 @@ mod value_tests { #[cfg(test)] mod decoder_tests { use crate::value::Decoder; - use crate::value::Value; + use crate::value::{Value, AValue, NestedValue}; #[test] fn read_123() { let mut buf = &b"abc"[..]; - let mut d = Decoder::new(&mut buf, None); + let mut d = Decoder::<_, AValue>::new(&mut buf, None); assert_eq!(d.read().ok(), Some(97)); assert_eq!(d.read().ok(), Some(98)); assert_eq!(d.read().ok(), Some(99)); @@ -190,7 +191,7 @@ mod decoder_tests { #[test] fn skip_annotations_noskip() { let mut buf = &b"\x0521"[..]; - let mut d = Decoder::new(&mut buf, None); + let mut d = Decoder::<_, AValue>::new(&mut buf, None); let v = d.next().unwrap(); assert_eq!(v.annotations().len(), 1); assert_eq!(v.annotations()[0], Value::from(2).wrap()); @@ -199,7 +200,7 @@ mod decoder_tests { #[test] fn skip_annotations_skip() { let mut buf = &b"\x0521"[..]; - let mut d = Decoder::new(&mut buf, None); + let mut d = Decoder::<_, AValue>::new(&mut buf, None); d.set_read_annotations(false); let v = d.next().unwrap(); assert_eq!(v.annotations().len(), 0); @@ -218,7 +219,7 @@ mod samples_tests { use std::collections::BTreeMap; #[derive(Debug, serde::Serialize, serde::Deserialize)] - struct ExpectedPlaceholderMapping(DecodePlaceholderMap); + struct ExpectedPlaceholderMapping(DecodePlaceholderMap); #[derive(Debug, serde::Serialize, serde::Deserialize)] struct TestCases { @@ -239,7 +240,7 @@ mod samples_tests { #[test] fn run() -> codec::Result<()> { let mut fh = std::fs::File::open("../../tests/samples.bin").unwrap(); - let mut d = Decoder::new(&mut fh, None); + let mut d = Decoder::<_, AValue>::new(&mut fh, None); let tests: TestCases = from_value(&d.next().unwrap()).unwrap(); // println!("{:#?}", tests); diff --git a/implementations/rust/src/symbol.rs b/implementations/rust/src/symbol.rs index 41f1bad..5b98462 100644 --- a/implementations/rust/src/symbol.rs +++ b/implementations/rust/src/symbol.rs @@ -1,17 +1,17 @@ -use crate::value::Value; +use crate::value::{Value, AValue}; #[derive(Debug, PartialEq, Eq, PartialOrd, Ord)] pub struct Symbol(pub String); impl serde::Serialize for Symbol { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer { - Value::symbol(&self.0).serialize(serializer) + Value::::symbol(&self.0).serialize(serializer) } } impl<'de> serde::Deserialize<'de> for Symbol { fn deserialize(deserializer: D) -> Result where D: serde::Deserializer<'de> { - let v = Value::deserialize(deserializer)?; + let v = Value::::deserialize(deserializer)?; let s = v.as_symbol().ok_or(serde::de::Error::custom("Expected symbol"))?; Ok(Symbol(s.clone())) } diff --git a/implementations/rust/src/value/codec.rs b/implementations/rust/src/value/codec.rs index ce4df8f..4a9d121 100644 --- a/implementations/rust/src/value/codec.rs +++ b/implementations/rust/src/value/codec.rs @@ -1,4 +1,4 @@ -use crate::value::{decoder, encoder, invert_map, AValue}; +use crate::value::{decoder, encoder, invert_map, NestedValue}; use decoder::{Decoder, DecodePlaceholderMap}; use encoder::{Encoder, EncodePlaceholderMap}; use std::io::{Read, Write}; @@ -6,13 +6,13 @@ use std::io::{Read, Write}; pub type Error = decoder::Error; pub type Result = decoder::Result; -pub struct Codec { - pub decode_placeholders: Option, - pub encode_placeholders: Option, +pub struct Codec { + pub decode_placeholders: Option>, + pub encode_placeholders: Option>, } -impl Codec { - pub fn new(decode_placeholders: DecodePlaceholderMap) -> Self { +impl Codec { + pub fn new(decode_placeholders: DecodePlaceholderMap) -> Self { let encode_placeholders = Some(invert_map(&decode_placeholders)); Codec { decode_placeholders: Some(decode_placeholders), encode_placeholders } } @@ -21,19 +21,19 @@ impl Codec { Codec { decode_placeholders: None, encode_placeholders: None } } - pub fn decoder<'a, 'r, R: Read>(&'a self, read: &'r mut R) -> Decoder<'r, 'a, R> { + pub fn decoder<'a, 'r, R: Read>(&'a self, read: &'r mut R) -> Decoder<'r, 'a, R, N> { Decoder::new(read, self.decode_placeholders.as_ref()) } - pub fn encoder<'a, 'w, W: Write>(&'a self, write: &'w mut W) -> Encoder<'w, 'a, W> { + pub fn encoder<'a, 'w, W: Write>(&'a self, write: &'w mut W) -> Encoder<'w, 'a, W, N> { Encoder::new(write, self.encode_placeholders.as_ref()) } - pub fn decode<'r, R: Read>(&self, read: &'r mut R) -> Result { + pub fn decode<'r, R: Read>(&self, read: &'r mut R) -> Result { self.decoder(read).next() } - pub fn encode_bytes(&self, v: &AValue) -> Result> { + pub fn encode_bytes(&self, v: &N) -> Result> { let mut buf: Vec = Vec::new(); self.encoder(&mut buf).write(v)?; Ok(buf) diff --git a/implementations/rust/src/value/de.rs b/implementations/rust/src/value/de.rs index f7c5240..1e582a8 100644 --- a/implementations/rust/src/value/de.rs +++ b/implementations/rust/src/value/de.rs @@ -1,4 +1,4 @@ -use crate::value::{Value, AValue, Dictionary}; +use crate::value::{Value, NestedValue}; use crate::value::value::{Float, Double}; use crate::value::error::{Error, Result, ExpectedKind}; use num::traits::cast::ToPrimitive; @@ -6,30 +6,31 @@ use serde::Deserialize; use serde::de::{Visitor, SeqAccess, MapAccess, EnumAccess, VariantAccess, DeserializeSeed}; use std::convert::TryFrom; use std::iter::Iterator; +use std::collections::BTreeMap as Map; -pub struct Deserializer<'de> { - input: &'de AValue, +pub struct Deserializer<'de, N: NestedValue> { + input: &'de N, } -pub fn from_value<'a, T>(v: &'a AValue) -> Result where T: Deserialize<'a> { +pub fn from_value<'a, T, N: NestedValue>(v: &'a N) -> Result where T: Deserialize<'a> { let mut de = Deserializer::from_value(v); let t = T::deserialize(&mut de)?; Ok(t) } -impl<'de> Deserializer<'de> { - pub fn from_value(v: &'de AValue) -> Self { +impl<'de, N: NestedValue> Deserializer<'de, N> { + pub fn from_value(v: &'de N) -> Self { Deserializer{ input: v } } fn check<'a, T, F>(&'a mut self, f: F, k: ExpectedKind) -> Result - where F: FnOnce(&'de Value) -> Option + where F: FnOnce(&'de Value) -> Option { - f(self.input.value()).ok_or_else(|| Error::Expected(k, self.input.clone())) + f(self.input.value()).ok_or_else(|| Error::Expected(k, self.input.copy_via())) } } -impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut Deserializer<'de> { +impl<'de, 'a, N: NestedValue> serde::de::Deserializer<'de> for &'a mut Deserializer<'de, N> { type Error = Error; fn deserialize_any(self, visitor: V) -> Result where V: Visitor<'de> @@ -138,7 +139,7 @@ impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut Deserializer<'de> { let fs = self.check(|v| v.as_simple_record("UnicodeScalar", Some(1)), ExpectedKind::SimpleRecord("UnicodeScalar", Some(1)))?; let c = fs[0].value().as_u32() - .ok_or_else(|| Error::Expected(ExpectedKind::SignedInteger, self.input.clone()))?; + .ok_or_else(|| Error::Expected(ExpectedKind::SignedInteger, self.input.copy_via()))?; visitor.visit_char(char::try_from(c).or(Err(Error::InvalidUnicodeScalar(c)))?) } @@ -171,7 +172,7 @@ impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut Deserializer<'de> { self.input = &fs[0]; visitor.visit_some(self) } - None => Err(Error::Expected(ExpectedKind::Option, self.input.clone())) + None => Err(Error::Expected(ExpectedKind::Option, self.input.copy_via())) } } } @@ -181,7 +182,7 @@ impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut Deserializer<'de> { if self.input.value().is_simple_record("tuple", Some(0)) { visitor.visit_unit() } else { - Err(Error::Expected(ExpectedKind::SimpleRecord("tuple", Some(0)), self.input.clone())) + Err(Error::Expected(ExpectedKind::SimpleRecord("tuple", Some(0)), self.input.copy_via())) } } @@ -191,7 +192,7 @@ impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut Deserializer<'de> { if self.input.value().is_simple_record(name, Some(0)) { visitor.visit_unit() } else { - Err(Error::Expected(ExpectedKind::SimpleRecord(name, Some(0)), self.input.clone())) + Err(Error::Expected(ExpectedKind::SimpleRecord(name, Some(0)), self.input.copy_via())) } } @@ -267,19 +268,19 @@ impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut Deserializer<'de> { } } -pub struct VecSeq<'a, 'de: 'a> { +pub struct VecSeq<'a, 'de: 'a, N: NestedValue> { index: usize, - vec: &'de Vec, - de: &'a mut Deserializer<'de>, + vec: &'de Vec, + de: &'a mut Deserializer<'de, N>, } -impl<'de, 'a> VecSeq<'a, 'de> { - fn new(de: &'a mut Deserializer<'de>, vec: &'de Vec) -> Self { +impl<'de, 'a, N: NestedValue> VecSeq<'a, 'de, N> { + fn new(de: &'a mut Deserializer<'de, N>, vec: &'de Vec) -> Self { VecSeq{ index: 0, vec, de } } } -impl<'de, 'a> SeqAccess<'de> for VecSeq<'a, 'de> { +impl<'de, 'a, N: NestedValue> SeqAccess<'de> for VecSeq<'a, 'de, N> { type Error = Error; fn next_element_seed(&mut self, seed: T) @@ -296,19 +297,19 @@ impl<'de, 'a> SeqAccess<'de> for VecSeq<'a, 'de> { } } -pub struct DictMap<'a, 'de: 'a> { - pending: Option<&'de AValue>, - iter: Box + 'a>, - de: &'a mut Deserializer<'de>, +pub struct DictMap<'a, 'de: 'a, N: NestedValue> { + pending: Option<&'de N>, + iter: Box + 'a>, + de: &'a mut Deserializer<'de, N>, } -impl<'de, 'a> DictMap<'a, 'de> { - fn new(de: &'a mut Deserializer<'de>, d: &'de Dictionary) -> Self { +impl<'de, 'a, N: NestedValue> DictMap<'a, 'de, N> { + fn new(de: &'a mut Deserializer<'de, N>, d: &'de Map) -> Self { DictMap{ pending: None, iter: Box::new(d.into_iter()), de } } } -impl<'de, 'a> MapAccess<'de> for DictMap<'a, 'de> { +impl<'de, 'a, N: NestedValue> MapAccess<'de> for DictMap<'a, 'de, N> { type Error = Error; fn next_key_seed(&mut self, seed: K) @@ -332,7 +333,7 @@ impl<'de, 'a> MapAccess<'de> for DictMap<'a, 'de> { } } -impl<'a, 'de> EnumAccess<'de> for &'a mut Deserializer<'de> { +impl<'a, 'de, N: NestedValue> EnumAccess<'de> for &'a mut Deserializer<'de, N> { type Error = Error; type Variant = Self; @@ -348,7 +349,7 @@ impl<'a, 'de> EnumAccess<'de> for &'a mut Deserializer<'de> { } } -impl<'a, 'de> VariantAccess<'de> for &'a mut Deserializer<'de> { +impl<'a, 'de, N: NestedValue> VariantAccess<'de> for &'a mut Deserializer<'de, N> { type Error = Error; fn unit_variant(self) -> Result<()> { diff --git a/implementations/rust/src/value/decoder.rs b/implementations/rust/src/value/decoder.rs index c4f337c..31744ba 100644 --- a/implementations/rust/src/value/decoder.rs +++ b/implementations/rust/src/value/decoder.rs @@ -1,7 +1,9 @@ use std::io::{Read, ErrorKind}; +use std::collections::BTreeSet as Set; +use std::collections::BTreeMap as Map; use std::convert::TryInto; use std::convert::TryFrom; -use crate::value::value::{Value, AValue, Set, Dictionary}; +use crate::value::{Value, NestedValue}; use num::bigint::BigInt; use crate::value::constants::{Op, InvalidOp, AtomMinor, CompoundMinor}; @@ -48,13 +50,13 @@ impl Error { pub fn is_eof(&self) -> bool { if let Error::Eof = *self { true } else { false } } } -pub type DecodePlaceholderMap = std::collections::BTreeMap; +pub type DecodePlaceholderMap = std::collections::BTreeMap>; -pub struct Decoder<'a, 'b, R: Read> { +pub struct Decoder<'a, 'b, R: Read, N: NestedValue> { read: &'a mut R, index: usize, buf: Box>>, - placeholders: Option<&'b DecodePlaceholderMap>, + placeholders: Option<&'b DecodePlaceholderMap>, read_annotations: bool, } @@ -62,8 +64,8 @@ fn decodeint(bs: &[u8]) -> BigInt { BigInt::from_signed_bytes_be(bs) } -impl<'a, 'b, R: Read> Decoder<'a, 'b, R> { - pub fn new(read: &'a mut R, placeholders: Option<&'b DecodePlaceholderMap>) -> Self { +impl<'a, 'b, R: Read, N: NestedValue> Decoder<'a, 'b, R, N> { + pub fn new(read: &'a mut R, placeholders: Option<&'b DecodePlaceholderMap>) -> Self { Decoder{ read, index: 0, @@ -132,8 +134,8 @@ impl<'a, 'b, R: Read> Decoder<'a, 'b, R> { } } - pub fn readvalues(&mut self, mut count: usize) -> Result> { - let mut pieces: Vec = Vec::with_capacity(count); + pub fn readvalues(&mut self, mut count: usize) -> Result> { + let mut pieces: Vec = Vec::with_capacity(count); while count > 0 { pieces.push(self.next()?); count = count - 1; @@ -175,7 +177,7 @@ impl<'a, 'b, R: Read> Decoder<'a, 'b, R> { } } - pub fn decodebinary(minor: AtomMinor, bs: Vec) -> Result { + pub fn decodebinary(minor: AtomMinor, bs: Vec) -> Result { Ok(match minor { AtomMinor::SignedInteger => Value::from(decodeint(&bs)).wrap(), AtomMinor::String => Value::from(std::str::from_utf8(&bs)?).wrap(), @@ -184,7 +186,7 @@ impl<'a, 'b, R: Read> Decoder<'a, 'b, R> { }) } - pub fn decodecompound(minor: CompoundMinor, mut pieces: Vec) -> Result { + pub fn decodecompound(minor: CompoundMinor, mut pieces: Vec) -> Result { match minor { CompoundMinor::Record => if pieces.len() == 0 { @@ -193,7 +195,7 @@ impl<'a, 'b, R: Read> Decoder<'a, 'b, R> { let label = pieces.remove(0); Ok(Value::record(label, pieces).wrap()) }, - CompoundMinor::Sequence => Ok(Value::from(pieces).wrap()), + CompoundMinor::Sequence => Ok(Value::Sequence(pieces).wrap()), CompoundMinor::Set => { let mut s = Set::new(); while let Some(v) = pieces.pop() { @@ -205,7 +207,7 @@ impl<'a, 'b, R: Read> Decoder<'a, 'b, R> { if pieces.len() % 2 != 0 { Err(Error::Syntax("Missing dictionary value")) } else { - let mut d = Dictionary::new(); + let mut d = Map::new(); while let Some(v) = pieces.pop() { let k = pieces.pop().unwrap(); d.insert(k, v); @@ -215,10 +217,10 @@ impl<'a, 'b, R: Read> Decoder<'a, 'b, R> { } } - pub fn binarystream(&mut self, minor: AtomMinor) -> Result { + pub fn binarystream(&mut self, minor: AtomMinor) -> Result { let mut bs: Vec = Vec::new(); while !self.peekend()? { - match self.next()?.1.as_bytestring() { + match self.next()?.value().as_bytestring() { Some(chunk) => bs.extend_from_slice(chunk), None => return Err(Error::Syntax("Unexpected non-binary chunk")), } @@ -226,15 +228,15 @@ impl<'a, 'b, R: Read> Decoder<'a, 'b, R> { Self::decodebinary(minor, bs) } - pub fn valuestream(&mut self, minor: CompoundMinor) -> Result { - let mut pieces: Vec = Vec::new(); + pub fn valuestream(&mut self, minor: CompoundMinor) -> Result { + let mut pieces: Vec = Vec::new(); while !self.peekend()? { pieces.push(self.next()?); } Self::decodecompound(minor, pieces) } - pub fn next(&mut self) -> Result { + pub fn next(&mut self) -> Result { match self.nextop()? { (Op::Misc(0), 0) => Ok(Value::from(false).wrap()), (Op::Misc(0), 1) => Ok(Value::from(true).wrap()), diff --git a/implementations/rust/src/value/encoder.rs b/implementations/rust/src/value/encoder.rs index bfdb848..1888007 100644 --- a/implementations/rust/src/value/encoder.rs +++ b/implementations/rust/src/value/encoder.rs @@ -1,5 +1,5 @@ use std::io::Write; -use crate::value::value::{Value, AValue, Float, Double}; +use crate::value::value::{Value, NestedValue, Float, Double}; use num::bigint::BigInt; use num::cast::ToPrimitive; use crate::value::constants::{Op, AtomMinor, CompoundMinor}; @@ -7,15 +7,15 @@ use crate::value::constants::{Op, AtomMinor, CompoundMinor}; pub type Error = std::io::Error; pub type Result = std::result::Result<(), Error>; -pub type EncodePlaceholderMap = std::collections::BTreeMap; +pub type EncodePlaceholderMap = std::collections::BTreeMap, usize>; -pub struct Encoder<'a, 'b, W: Write> { +pub struct Encoder<'a, 'b, W: Write, N: NestedValue> { write: &'a mut W, - placeholders: Option<&'b EncodePlaceholderMap>, + placeholders: Option<&'b EncodePlaceholderMap>, } -impl<'a, 'b, W: Write> Encoder<'a, 'b, W> { - pub fn new(write: &'a mut W, placeholders: Option<&'b EncodePlaceholderMap>) -> Self { +impl<'a, 'b, W: Write, N: NestedValue> Encoder<'a, 'b, W, N> { + pub fn new(write: &'a mut W, placeholders: Option<&'b EncodePlaceholderMap>) -> Self { Encoder{ write, placeholders } } @@ -70,7 +70,7 @@ impl<'a, 'b, W: Write> Encoder<'a, 'b, W> { self.write_all(bs) } - pub fn write(&mut self, v: &AValue) -> Result { + pub fn write(&mut self, v: &N) -> Result { for ann in v.annotations() { self.write_header(Op::Misc(0), 5)?; self.write(ann)?; @@ -78,7 +78,7 @@ impl<'a, 'b, W: Write> Encoder<'a, 'b, W> { self.write_value(v.value()) } - pub fn write_value(&mut self, v: &Value) -> Result { + pub fn write_value(&mut self, v: &Value) -> Result { match self.placeholders.and_then(|m| m.get(v)) { Some(&n) => self.write_header(Op::Misc(1), n), None => match v { diff --git a/implementations/rust/src/value/mod.rs b/implementations/rust/src/value/mod.rs index e62cbfb..3b067d4 100644 --- a/implementations/rust/src/value/mod.rs +++ b/implementations/rust/src/value/mod.rs @@ -16,10 +16,12 @@ pub use encoder::EncodePlaceholderMap; pub use encoder::Encoder; pub use ser::Serializer; pub use ser::to_value; -pub use value::AValue; -pub use value::Dictionary; -pub use value::Set; +pub use value::NestedValue; pub use value::Value; +pub use value::AValue; + +pub type Set = std::collections::BTreeSet; +pub type Dictionary = std::collections::BTreeMap; pub fn invert_map(m: &std::collections::BTreeMap) -> std::collections::BTreeMap where A: Clone, B: Clone + Ord diff --git a/implementations/rust/src/value/ser.rs b/implementations/rust/src/value/ser.rs index 8781497..23f395d 100644 --- a/implementations/rust/src/value/ser.rs +++ b/implementations/rust/src/value/ser.rs @@ -1,4 +1,4 @@ -use crate::value::{Value, AValue, Dictionary}; +use crate::value::{Value, AValue, NestedValue, Dictionary}; use serde::Serialize; use crate::value::error::{Error, Result}; diff --git a/implementations/rust/src/value/value.rs b/implementations/rust/src/value/value.rs index 856a877..b3d4789 100644 --- a/implementations/rust/src/value/value.rs +++ b/implementations/rust/src/value/value.rs @@ -1,17 +1,48 @@ use std::hash::{Hash,Hasher}; use std::cmp::{Ordering}; +use std::fmt::Debug; use num::bigint::BigInt; use std::vec::Vec; use std::string::String; -use std::collections::BTreeSet; -use std::collections::BTreeMap; +use std::collections::BTreeSet as Set; +use std::collections::BTreeMap as Map; +use std::ops::Deref; use std::ops::Index; use std::ops::IndexMut; use num::traits::cast::ToPrimitive; +pub trait NestedValue: Sized + Debug + Clone + Eq + Hash + Ord { + type BoxType: Sized + Debug + Clone + Eq + Hash + Ord + Deref; + + fn wrap(v: Value) -> Self; + fn wrap_ann(anns: Vec, v: Value) -> Self; + + fn boxwrap(self) -> Self::BoxType; + fn boxunwrap(b: &Self::BoxType) -> &Self; + + fn annotations(&self) -> &Vec; + fn annotations_mut(&mut self) -> &mut Vec; + + fn value(&self) -> &Value; + fn value_mut(&mut self) -> &mut Value; + fn value_owned(self) -> Value; + + fn debug_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + for ann in self.annotations() { + write!(f, "@{:?} ", ann)?; + } + self.value().fmt(f) + } + + fn copy_via(&self) -> M { + M::wrap_ann(self.annotations().iter().map(|a| a.copy_via()).collect(), + self.value().copy_via()) + } +} + /// The `Value`s from the specification. #[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] -pub enum Value { +pub enum Value where N: NestedValue { Boolean(bool), Float(Float), Double(Double), @@ -19,16 +50,56 @@ pub enum Value { String(String), ByteString(Vec), Symbol(String), - Record(Record), - Sequence(Vec), - Set(Set), - Dictionary(Dictionary), + Record(Record), + Sequence(Vec), + Set(Set), + Dictionary(Map), } /// An possibly-annotated Value, with annotations (themselves /// possibly-annotated) in order of appearance. #[derive(Clone)] -pub struct AValue(pub Vec, pub Value); +pub struct AValue(pub Vec, pub Value); + +impl NestedValue for AValue { + type BoxType = Box; + + fn wrap(v: Value) -> Self { + Self::wrap_ann(Vec::new(), v) + } + + fn wrap_ann(anns: Vec, v: Value) -> Self { + AValue(anns, v) + } + + fn boxwrap(self) -> Self::BoxType { + Box::new(self) + } + + fn boxunwrap(b: &Self::BoxType) -> &Self { + &**b + } + + fn annotations(&self) -> &Vec { + &self.0 + } + + fn annotations_mut(&mut self) -> &mut Vec { + &mut self.0 + } + + fn value(&self) -> &Value { + &self.1 + } + + fn value_mut(&mut self) -> &mut Value { + &mut self.1 + } + + fn value_owned(self) -> Value { + self.1 + } +} /// Single-precision IEEE 754 Value #[derive(Clone, Debug)] @@ -39,11 +110,7 @@ pub struct Float(pub f32); pub struct Double(pub f64); /// A Record `Value` -pub type Record = (Box, Vec); - -pub type Set = BTreeSet; - -pub type Dictionary = BTreeMap; +pub type Record = (::BoxType, Vec); impl From for Float { fn from(v: f32) -> Self { @@ -131,7 +198,7 @@ impl Eq for Double {} impl PartialEq for AValue { fn eq(&self, other: &Self) -> bool { - self.1.eq(&other.1) + self.value().eq(other.value()) } } @@ -151,38 +218,44 @@ impl PartialOrd for AValue { impl Ord for AValue { fn cmp(&self, other: &Self) -> Ordering { - self.1.cmp(&other.1) + self.value().cmp(&other.value()) } } -impl From for Value { fn from(v: bool) -> Self { Value::Boolean(v) } } +impl Debug for AValue { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.debug_fmt(f) + } +} -impl From for Value { fn from(v: f32) -> Self { Value::Float(Float::from(v)) } } -impl From for Value { fn from(v: f64) -> Self { Value::Double(Double::from(v)) } } +impl From for Value { fn from(v: bool) -> Self { Value::Boolean(v) } } -impl From for Value { fn from(v: u8) -> Self { Value::SignedInteger(BigInt::from(v)) } } -impl From for Value { fn from(v: i8) -> Self { Value::SignedInteger(BigInt::from(v)) } } -impl From for Value { fn from(v: u16) -> Self { Value::SignedInteger(BigInt::from(v)) } } -impl From for Value { fn from(v: i16) -> Self { Value::SignedInteger(BigInt::from(v)) } } -impl From for Value { fn from(v: u32) -> Self { Value::SignedInteger(BigInt::from(v)) } } -impl From for Value { fn from(v: i32) -> Self { Value::SignedInteger(BigInt::from(v)) } } -impl From for Value { fn from(v: u64) -> Self { Value::SignedInteger(BigInt::from(v)) } } -impl From for Value { fn from(v: i64) -> Self { Value::SignedInteger(BigInt::from(v)) } } -impl From for Value { fn from(v: u128) -> Self { Value::SignedInteger(BigInt::from(v)) } } -impl From for Value { fn from(v: i128) -> Self { Value::SignedInteger(BigInt::from(v)) } } -impl From for Value { fn from(v: BigInt) -> Self { Value::SignedInteger(v) } } +impl From for Value { fn from(v: f32) -> Self { Value::Float(Float::from(v)) } } +impl From for Value { fn from(v: f64) -> Self { Value::Double(Double::from(v)) } } -impl From<&str> for Value { fn from(v: &str) -> Self { Value::String(String::from(v)) } } -impl From for Value { fn from(v: String) -> Self { Value::String(v) } } +impl From for Value { fn from(v: u8) -> Self { Value::SignedInteger(BigInt::from(v)) } } +impl From for Value { fn from(v: i8) -> Self { Value::SignedInteger(BigInt::from(v)) } } +impl From for Value { fn from(v: u16) -> Self { Value::SignedInteger(BigInt::from(v)) } } +impl From for Value { fn from(v: i16) -> Self { Value::SignedInteger(BigInt::from(v)) } } +impl From for Value { fn from(v: u32) -> Self { Value::SignedInteger(BigInt::from(v)) } } +impl From for Value { fn from(v: i32) -> Self { Value::SignedInteger(BigInt::from(v)) } } +impl From for Value { fn from(v: u64) -> Self { Value::SignedInteger(BigInt::from(v)) } } +impl From for Value { fn from(v: i64) -> Self { Value::SignedInteger(BigInt::from(v)) } } +impl From for Value { fn from(v: u128) -> Self { Value::SignedInteger(BigInt::from(v)) } } +impl From for Value { fn from(v: i128) -> Self { Value::SignedInteger(BigInt::from(v)) } } +impl From for Value { fn from(v: BigInt) -> Self { Value::SignedInteger(v) } } -impl From<&[u8]> for Value { fn from(v: &[u8]) -> Self { Value::ByteString(Vec::from(v)) } } -impl From> for Value { fn from(v: Vec) -> Self { Value::ByteString(v) } } +impl From<&str> for Value { fn from(v: &str) -> Self { Value::String(String::from(v)) } } +impl From for Value { fn from(v: String) -> Self { Value::String(v) } } -impl From> for Value { fn from(v: Vec) -> Self { Value::Sequence(v) } } -impl From for Value { fn from(v: Set) -> Self { Value::Set(v) } } -impl From for Value { fn from(v: Dictionary) -> Self { Value::Dictionary(v) } } +impl From<&[u8]> for Value { fn from(v: &[u8]) -> Self { Value::ByteString(Vec::from(v)) } } +impl From> for Value { fn from(v: Vec) -> Self { Value::ByteString(v) } } -impl std::fmt::Debug for Value { +impl From> for Value { fn from(v: Vec) -> Self { Value::Sequence(v) } } +impl From> for Value { fn from(v: Set) -> Self { Value::Set(v) } } +impl From> for Value { fn from(v: Map) -> Self { Value::Dictionary(v) } } + +impl Debug for Value { // Not *quite* a formatter for the Preserves text syntax, since it // doesn't escape strings/symbols properly. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { @@ -220,43 +293,16 @@ impl std::fmt::Debug for Value { f.write_str("#set")?; f.debug_set().entries(v.iter()).finish() } - Value::Dictionary (ref v) => f.debug_map().entries(v.iter()).finish() + Value::Dictionary(ref v) => f.debug_map().entries(v.iter()).finish() } } } -impl std::fmt::Debug for AValue { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - for ann in &self.0 { - write!(f, "@{:?} ", ann)?; - } - self.1.fmt(f) - } -} - //--------------------------------------------------------------------------- -impl AValue { - pub fn annotations(&self) -> &Vec { - &self.0 - } - - pub fn annotations_mut(&mut self) -> &mut Vec { - &mut self.0 - } - - pub fn value(&self) -> &Value { - &self.1 - } - - pub fn value_mut(&mut self) -> &mut Value { - &mut self.1 - } -} - -impl Value { - pub fn wrap(self) -> AValue { - AValue(Vec::new(), self) +impl Value { + pub fn wrap(self) -> N { + N::wrap(self) } pub fn is_boolean(&self) -> bool { @@ -388,7 +434,7 @@ impl Value { } } - pub fn symbol(s: &str) -> Value { + pub fn symbol(s: &str) -> Value { Value::Symbol(s.to_string()) } @@ -412,15 +458,15 @@ impl Value { } } - pub fn record(label: AValue, fields: Vec) -> Value { - Value::Record((Box::new(label), fields)) + pub fn record(label: N, fields: Vec) -> Value { + Value::Record((label.boxwrap(), fields)) } pub fn is_record(&self) -> bool { self.as_record().is_some() } - pub fn as_record(&self) -> Option<&Record> { + pub fn as_record(&self) -> Option<&Record> { if let Value::Record(ref r) = *self { Some(r) } else { @@ -428,7 +474,7 @@ impl Value { } } - pub fn as_record_mut(&mut self) -> Option<&mut Record> { + pub fn as_record_mut(&mut self) -> Option<&mut Record> { if let Value::Record(ref mut r) = *self { Some(r) } else { @@ -436,7 +482,7 @@ impl Value { } } - pub fn simple_record(label: &str, fields: Vec) -> Value { + pub fn simple_record(label: &str, fields: Vec) -> Value { Value::record(Value::symbol(label).wrap(), fields) } @@ -444,7 +490,7 @@ impl Value { self.as_simple_record(label, arity).is_some() } - pub fn as_simple_record(&self, label: &str, arity: Option) -> Option<&Vec> { + pub fn as_simple_record(&self, label: &str, arity: Option) -> Option<&Vec> { self.as_record().and_then(|(lp,fs)| { match *lp.value() { Value::Symbol(ref s) if s == label => @@ -462,7 +508,7 @@ impl Value { self.as_sequence().is_some() } - pub fn as_sequence(&self) -> Option<&Vec> { + pub fn as_sequence(&self) -> Option<&Vec> { if let Value::Sequence(ref s) = *self { Some(s) } else { @@ -470,7 +516,7 @@ impl Value { } } - pub fn as_sequence_mut(&mut self) -> Option<&mut Vec> { + pub fn as_sequence_mut(&mut self) -> Option<&mut Vec> { if let Value::Sequence(ref mut s) = *self { Some(s) } else { @@ -482,7 +528,7 @@ impl Value { self.as_set().is_some() } - pub fn as_set(&self) -> Option<&Set> { + pub fn as_set(&self) -> Option<&Set> { if let Value::Set(ref s) = *self { Some(s) } else { @@ -490,7 +536,7 @@ impl Value { } } - pub fn as_set_mut(&mut self) -> Option<&mut Set> { + pub fn as_set_mut(&mut self) -> Option<&mut Set> { if let Value::Set(ref mut s) = *self { Some(s) } else { @@ -502,7 +548,7 @@ impl Value { self.as_dictionary().is_some() } - pub fn as_dictionary(&self) -> Option<&Dictionary> { + pub fn as_dictionary(&self) -> Option<&Map> { if let Value::Dictionary(ref s) = *self { Some(s) } else { @@ -510,34 +556,52 @@ impl Value { } } - pub fn as_dictionary_mut(&mut self) -> Option<&mut Dictionary> { + pub fn as_dictionary_mut(&mut self) -> Option<&mut Map> { if let Value::Dictionary(ref mut s) = *self { Some(s) } else { None } } + + pub fn copy_via(&self) -> Value { + match self { + Value::Boolean(b) => Value::Boolean(*b), + Value::Float(f) => Value::Float(f.clone()), + Value::Double(d) => Value::Double(d.clone()), + Value::SignedInteger(i) => Value::SignedInteger(i.clone()), + Value::String(ref s) => Value::String(s.clone()), + Value::ByteString(ref v) => Value::ByteString(v.clone()), + Value::Symbol(ref v) => Value::String(v.clone()), + Value::Record((ref l, ref fs)) => + Value::Record((N::boxunwrap(l).copy_via::().boxwrap(), fs.iter().map(|a| a.copy_via()).collect())), + Value::Sequence(ref v) => Value::Sequence(v.iter().map(|a| a.copy_via()).collect()), + Value::Set(ref v) => Value::Set(v.iter().map(|a| a.copy_via()).collect()), + Value::Dictionary(ref v) => + Value::Dictionary(v.iter().map(|(a,b)| (a.copy_via(), b.copy_via())).collect()), + } + } } -impl Index for Value { - type Output = AValue; +impl Index for Value { + type Output = N; fn index(&self, i: usize) -> &Self::Output { &self.as_sequence().unwrap()[i] } } -impl IndexMut for Value { +impl IndexMut for Value { fn index_mut(&mut self, i: usize) -> &mut Self::Output { &mut self.as_sequence_mut().unwrap()[i] } } -impl Index<&AValue> for Value { - type Output = AValue; +impl Index<&N> for Value { + type Output = N; - fn index(&self, i: &AValue) -> &Self::Output { - &self.as_dictionary().unwrap()[i] + fn index(&self, i: &N) -> &Self::Output { + &(*self.as_dictionary().unwrap())[i] } } @@ -570,7 +634,7 @@ impl<'de> serde::Deserialize<'de> for ValueWrapper { } } -impl serde::Serialize for Value { +impl serde::Serialize for Value { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer { let mut buf: Vec = Vec::new(); crate::value::Encoder::new(&mut buf, None).write_value(self) @@ -588,12 +652,12 @@ impl serde::Serialize for AValue { } } -impl<'de> serde::Deserialize<'de> for Value { +impl<'de, N: NestedValue> serde::Deserialize<'de> for Value { fn deserialize(deserializer: D) -> Result where D: serde::Deserializer<'de> { let ValueWrapper(buf) = ValueWrapper::deserialize(deserializer)?; - let v = crate::value::Decoder::new(&mut &buf[..], None).next() + let v: N = crate::value::Decoder::new(&mut &buf[..], None).next() .or(Err(serde::de::Error::custom("Internal error")))?; - Ok(v.value().clone()) + Ok(v.value_owned()) } }