AValue -> PlainValue, RcValue and ArcValue
This commit is contained in:
parent
f1b37ac9af
commit
54d1e349b3
|
@ -5,9 +5,9 @@ pub mod symbol;
|
||||||
mod ieee754_section_5_10_total_order_tests {
|
mod ieee754_section_5_10_total_order_tests {
|
||||||
use std::cmp::Ordering::{Less, Equal, Greater};
|
use std::cmp::Ordering::{Less, Equal, Greater};
|
||||||
|
|
||||||
use crate::value::{Value, AValue};
|
use crate::value::{Value, PlainValue};
|
||||||
fn f(val: f32) -> Value<AValue> { Value::from(val) }
|
fn f(val: f32) -> Value<PlainValue> { Value::from(val) }
|
||||||
fn d(val: f64) -> Value<AValue> { Value::from(val) }
|
fn d(val: f64) -> Value<PlainValue> { Value::from(val) }
|
||||||
|
|
||||||
// TODO: Test cases with a few different signalling and non-signalling NaNs
|
// TODO: Test cases with a few different signalling and non-signalling NaNs
|
||||||
|
|
||||||
|
@ -97,10 +97,10 @@ mod ieee754_section_5_10_total_order_tests {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod value_tests {
|
mod value_tests {
|
||||||
use crate::value::{Value, AValue};
|
use crate::value::{Value, PlainValue};
|
||||||
use num::bigint::BigInt;
|
use num::bigint::BigInt;
|
||||||
|
|
||||||
type VV = Value<AValue>;
|
type VV = Value<PlainValue>;
|
||||||
|
|
||||||
#[test] fn boolean_mut() {
|
#[test] fn boolean_mut() {
|
||||||
let mut b = VV::Boolean(true);
|
let mut b = VV::Boolean(true);
|
||||||
|
@ -178,11 +178,11 @@ mod value_tests {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod decoder_tests {
|
mod decoder_tests {
|
||||||
use crate::value::Decoder;
|
use crate::value::Decoder;
|
||||||
use crate::value::{Value, AValue, NestedValue};
|
use crate::value::{Value, PlainValue, NestedValue};
|
||||||
|
|
||||||
#[test] fn read_123() {
|
#[test] fn read_123() {
|
||||||
let mut buf = &b"abc"[..];
|
let mut buf = &b"abc"[..];
|
||||||
let mut d = Decoder::<_, AValue>::new(&mut buf, None);
|
let mut d = Decoder::<_, PlainValue>::new(&mut buf, None);
|
||||||
assert_eq!(d.read().ok(), Some(97));
|
assert_eq!(d.read().ok(), Some(97));
|
||||||
assert_eq!(d.read().ok(), Some(98));
|
assert_eq!(d.read().ok(), Some(98));
|
||||||
assert_eq!(d.read().ok(), Some(99));
|
assert_eq!(d.read().ok(), Some(99));
|
||||||
|
@ -191,7 +191,7 @@ mod decoder_tests {
|
||||||
|
|
||||||
#[test] fn skip_annotations_noskip() {
|
#[test] fn skip_annotations_noskip() {
|
||||||
let mut buf = &b"\x0521"[..];
|
let mut buf = &b"\x0521"[..];
|
||||||
let mut d = Decoder::<_, AValue>::new(&mut buf, None);
|
let mut d = Decoder::<_, PlainValue>::new(&mut buf, None);
|
||||||
let v = d.next().unwrap();
|
let v = d.next().unwrap();
|
||||||
assert_eq!(v.annotations().len(), 1);
|
assert_eq!(v.annotations().len(), 1);
|
||||||
assert_eq!(v.annotations()[0], Value::from(2).wrap());
|
assert_eq!(v.annotations()[0], Value::from(2).wrap());
|
||||||
|
@ -200,7 +200,7 @@ mod decoder_tests {
|
||||||
|
|
||||||
#[test] fn skip_annotations_skip() {
|
#[test] fn skip_annotations_skip() {
|
||||||
let mut buf = &b"\x0521"[..];
|
let mut buf = &b"\x0521"[..];
|
||||||
let mut d = Decoder::<_, AValue>::new(&mut buf, None);
|
let mut d = Decoder::<_, PlainValue>::new(&mut buf, None);
|
||||||
d.set_read_annotations(false);
|
d.set_read_annotations(false);
|
||||||
let v = d.next().unwrap();
|
let v = d.next().unwrap();
|
||||||
assert_eq!(v.annotations().len(), 0);
|
assert_eq!(v.annotations().len(), 0);
|
||||||
|
@ -212,26 +212,25 @@ mod decoder_tests {
|
||||||
mod samples_tests {
|
mod samples_tests {
|
||||||
use crate::symbol::Symbol;
|
use crate::symbol::Symbol;
|
||||||
use crate::value::{Codec, Decoder, codec};
|
use crate::value::{Codec, Decoder, codec};
|
||||||
use crate::value::{Value, AValue};
|
use crate::value::{Value, PlainValue, Map};
|
||||||
use crate::value::DecodePlaceholderMap;
|
use crate::value::DecodePlaceholderMap;
|
||||||
use crate::value::to_value;
|
use crate::value::to_value;
|
||||||
use crate::value::from_value;
|
use crate::value::from_value;
|
||||||
use std::collections::BTreeMap;
|
|
||||||
|
|
||||||
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
||||||
struct ExpectedPlaceholderMapping(DecodePlaceholderMap<AValue>);
|
struct ExpectedPlaceholderMapping(DecodePlaceholderMap<PlainValue>);
|
||||||
|
|
||||||
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
||||||
struct TestCases {
|
struct TestCases {
|
||||||
decode_placeholders: ExpectedPlaceholderMapping,
|
decode_placeholders: ExpectedPlaceholderMapping,
|
||||||
tests: BTreeMap<Symbol, TestCase>
|
tests: Map<Symbol, TestCase>
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
#[derive(Debug, serde::Serialize, serde::Deserialize)]
|
||||||
enum TestCase {
|
enum TestCase {
|
||||||
Test(#[serde(with = "serde_bytes")] Vec<u8>, AValue),
|
Test(#[serde(with = "serde_bytes")] Vec<u8>, PlainValue),
|
||||||
NondeterministicTest(#[serde(with = "serde_bytes")] Vec<u8>, AValue),
|
NondeterministicTest(#[serde(with = "serde_bytes")] Vec<u8>, PlainValue),
|
||||||
StreamingTest(#[serde(with = "serde_bytes")] Vec<u8>, AValue),
|
StreamingTest(#[serde(with = "serde_bytes")] Vec<u8>, PlainValue),
|
||||||
ParseError(String),
|
ParseError(String),
|
||||||
ParseShort(String),
|
ParseShort(String),
|
||||||
DecodeError(#[serde(with = "serde_bytes")] Vec<u8>),
|
DecodeError(#[serde(with = "serde_bytes")] Vec<u8>),
|
||||||
|
@ -240,7 +239,7 @@ mod samples_tests {
|
||||||
|
|
||||||
#[test] fn run() -> codec::Result<()> {
|
#[test] fn run() -> codec::Result<()> {
|
||||||
let mut fh = std::fs::File::open("../../tests/samples.bin").unwrap();
|
let mut fh = std::fs::File::open("../../tests/samples.bin").unwrap();
|
||||||
let mut d = Decoder::<_, AValue>::new(&mut fh, None);
|
let mut d = Decoder::<_, PlainValue>::new(&mut fh, None);
|
||||||
let tests: TestCases = from_value(&d.next().unwrap()).unwrap();
|
let tests: TestCases = from_value(&d.next().unwrap()).unwrap();
|
||||||
// println!("{:#?}", tests);
|
// println!("{:#?}", tests);
|
||||||
|
|
||||||
|
@ -296,7 +295,7 @@ mod samples_tests {
|
||||||
#[serde(with = "serde_bytes")] &'a [u8],
|
#[serde(with = "serde_bytes")] &'a [u8],
|
||||||
#[serde(with = "serde_bytes")] Vec<u8>,
|
#[serde(with = "serde_bytes")] Vec<u8>,
|
||||||
i16,
|
i16,
|
||||||
AValue);
|
PlainValue);
|
||||||
let v = SimpleValue("hello".to_string(),
|
let v = SimpleValue("hello".to_string(),
|
||||||
"sym1".to_string(),
|
"sym1".to_string(),
|
||||||
Symbol("sym2".to_string()),
|
Symbol("sym2".to_string()),
|
||||||
|
@ -306,7 +305,7 @@ mod samples_tests {
|
||||||
12345,
|
12345,
|
||||||
Value::from("hi").wrap());
|
Value::from("hi").wrap());
|
||||||
println!("{:#?}", v);
|
println!("{:#?}", v);
|
||||||
let w = to_value(&v).unwrap();
|
let w: PlainValue = to_value(&v).unwrap();
|
||||||
println!("{:#?}", w);
|
println!("{:#?}", w);
|
||||||
let x = from_value(&w).unwrap();
|
let x = from_value(&w).unwrap();
|
||||||
println!("{:#?}", &x);
|
println!("{:#?}", &x);
|
||||||
|
|
|
@ -1,18 +1,18 @@
|
||||||
use crate::value::{Value, AValue};
|
use crate::value::{Value, PlainValue, NestedValue};
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
pub struct Symbol(pub String);
|
pub struct Symbol(pub String);
|
||||||
|
|
||||||
impl serde::Serialize for Symbol {
|
impl serde::Serialize for Symbol {
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
|
||||||
Value::<AValue>::symbol(&self.0).serialize(serializer)
|
Value::<PlainValue>::symbol(&self.0).wrap().serialize(serializer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'de> serde::Deserialize<'de> for Symbol {
|
impl<'de> serde::Deserialize<'de> for Symbol {
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de> {
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de> {
|
||||||
let v = Value::<AValue>::deserialize(deserializer)?;
|
let v = PlainValue::deserialize(deserializer)?;
|
||||||
let s = v.as_symbol().ok_or(serde::de::Error::custom("Expected symbol"))?;
|
let s = v.value().as_symbol().ok_or(serde::de::Error::custom("Expected symbol"))?;
|
||||||
Ok(Symbol(s.clone()))
|
Ok(Symbol(s.clone()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::value::{Value, NestedValue};
|
use crate::value::{Value, NestedValue, Map};
|
||||||
use crate::value::value::{Float, Double};
|
use crate::value::value::{Float, Double};
|
||||||
use crate::value::error::{Error, Result, ExpectedKind};
|
use crate::value::error::{Error, Result, ExpectedKind};
|
||||||
use num::traits::cast::ToPrimitive;
|
use num::traits::cast::ToPrimitive;
|
||||||
|
@ -6,7 +6,6 @@ use serde::Deserialize;
|
||||||
use serde::de::{Visitor, SeqAccess, MapAccess, EnumAccess, VariantAccess, DeserializeSeed};
|
use serde::de::{Visitor, SeqAccess, MapAccess, EnumAccess, VariantAccess, DeserializeSeed};
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::iter::Iterator;
|
use std::iter::Iterator;
|
||||||
use std::collections::BTreeMap as Map;
|
|
||||||
|
|
||||||
pub struct Deserializer<'de, N: NestedValue> {
|
pub struct Deserializer<'de, N: NestedValue> {
|
||||||
input: &'de N,
|
input: &'de N,
|
||||||
|
@ -342,7 +341,7 @@ impl<'a, 'de, N: NestedValue> EnumAccess<'de> for &'a mut Deserializer<'de, N> {
|
||||||
{
|
{
|
||||||
let (lp, _) = self.check(|v| v.as_record(), ExpectedKind::Record)?;
|
let (lp, _) = self.check(|v| v.as_record(), ExpectedKind::Record)?;
|
||||||
let v = self.input;
|
let v = self.input;
|
||||||
self.input = lp;
|
self.input = N::boxunwrap(lp);
|
||||||
let variant = seed.deserialize(&mut *self)?;
|
let variant = seed.deserialize(&mut *self)?;
|
||||||
self.input = v;
|
self.input = v;
|
||||||
Ok((variant, self))
|
Ok((variant, self))
|
||||||
|
|
|
@ -1,9 +1,7 @@
|
||||||
use std::io::{Read, ErrorKind};
|
use std::io::{Read, ErrorKind};
|
||||||
use std::collections::BTreeSet as Set;
|
|
||||||
use std::collections::BTreeMap as Map;
|
|
||||||
use std::convert::TryInto;
|
use std::convert::TryInto;
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use crate::value::{Value, NestedValue};
|
use crate::value::{Value, NestedValue, Set, Map};
|
||||||
use num::bigint::BigInt;
|
use num::bigint::BigInt;
|
||||||
use crate::value::constants::{Op, InvalidOp, AtomMinor, CompoundMinor};
|
use crate::value::constants::{Op, InvalidOp, AtomMinor, CompoundMinor};
|
||||||
|
|
||||||
|
@ -50,7 +48,7 @@ impl Error {
|
||||||
pub fn is_eof(&self) -> bool { if let Error::Eof = *self { true } else { false } }
|
pub fn is_eof(&self) -> bool { if let Error::Eof = *self { true } else { false } }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type DecodePlaceholderMap<N> = std::collections::BTreeMap<usize, Value<N>>;
|
pub type DecodePlaceholderMap<N> = Map<usize, Value<N>>;
|
||||||
|
|
||||||
pub struct Decoder<'a, 'b, R: Read, N: NestedValue> {
|
pub struct Decoder<'a, 'b, R: Read, N: NestedValue> {
|
||||||
read: &'a mut R,
|
read: &'a mut R,
|
||||||
|
@ -255,9 +253,9 @@ impl<'a, 'b, R: Read, N: NestedValue> Decoder<'a, 'b, R, N> {
|
||||||
self.skip()?;
|
self.skip()?;
|
||||||
annotations.push(self.next()?);
|
annotations.push(self.next()?);
|
||||||
}
|
}
|
||||||
let mut v = self.next()?;
|
let v = self.next()?;
|
||||||
v.annotations_mut().extend(annotations);
|
assert!(v.annotations().len() == 0);
|
||||||
Ok(v)
|
Ok(N::wrap_ann(annotations, v.value_owned()))
|
||||||
} else {
|
} else {
|
||||||
self.next()?;
|
self.next()?;
|
||||||
self.next()
|
self.next()
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use crate::value::value::{Value, NestedValue, Float, Double};
|
use crate::value::value::{Value, NestedValue, Float, Double, Map};
|
||||||
use num::bigint::BigInt;
|
use num::bigint::BigInt;
|
||||||
use num::cast::ToPrimitive;
|
use num::cast::ToPrimitive;
|
||||||
use crate::value::constants::{Op, AtomMinor, CompoundMinor};
|
use crate::value::constants::{Op, AtomMinor, CompoundMinor};
|
||||||
|
@ -7,7 +7,7 @@ use crate::value::constants::{Op, AtomMinor, CompoundMinor};
|
||||||
pub type Error = std::io::Error;
|
pub type Error = std::io::Error;
|
||||||
pub type Result = std::result::Result<(), Error>;
|
pub type Result = std::result::Result<(), Error>;
|
||||||
|
|
||||||
pub type EncodePlaceholderMap<N> = std::collections::BTreeMap<Value<N>, usize>;
|
pub type EncodePlaceholderMap<N> = Map<Value<N>, usize>;
|
||||||
|
|
||||||
pub struct Encoder<'a, 'b, W: Write, N: NestedValue> {
|
pub struct Encoder<'a, 'b, W: Write, N: NestedValue> {
|
||||||
write: &'a mut W,
|
write: &'a mut W,
|
||||||
|
@ -98,7 +98,7 @@ impl<'a, 'b, W: Write, N: NestedValue> Encoder<'a, 'b, W, N> {
|
||||||
Value::Symbol(ref s) => self.write_atom(AtomMinor::Symbol, s.as_bytes()),
|
Value::Symbol(ref s) => self.write_atom(AtomMinor::Symbol, s.as_bytes()),
|
||||||
Value::Record((ref l, ref fs)) => {
|
Value::Record((ref l, ref fs)) => {
|
||||||
self.write_header(Op::Compound(CompoundMinor::Record), fs.len() + 1)?;
|
self.write_header(Op::Compound(CompoundMinor::Record), fs.len() + 1)?;
|
||||||
self.write(l)?;
|
self.write(N::boxunwrap(l))?;
|
||||||
Ok(for f in fs { self.write(f)?; })
|
Ok(for f in fs { self.write(f)?; })
|
||||||
}
|
}
|
||||||
Value::Sequence(ref vs) => {
|
Value::Sequence(ref vs) => {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use num::bigint::BigInt;
|
use num::bigint::BigInt;
|
||||||
use crate::value::AValue;
|
use crate::value::PlainValue;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
|
@ -7,7 +7,7 @@ pub enum Error {
|
||||||
InvalidUnicodeScalar(u32),
|
InvalidUnicodeScalar(u32),
|
||||||
NumberTooLarge(BigInt),
|
NumberTooLarge(BigInt),
|
||||||
CannotDeserializeAny,
|
CannotDeserializeAny,
|
||||||
Expected(ExpectedKind, AValue),
|
Expected(ExpectedKind, PlainValue),
|
||||||
InternalMagicError,
|
InternalMagicError,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,14 +18,16 @@ pub use ser::Serializer;
|
||||||
pub use ser::to_value;
|
pub use ser::to_value;
|
||||||
pub use value::NestedValue;
|
pub use value::NestedValue;
|
||||||
pub use value::Value;
|
pub use value::Value;
|
||||||
pub use value::AValue;
|
pub use value::AnnotatedValue;
|
||||||
|
pub use value::PlainValue;
|
||||||
|
pub use value::RcValue;
|
||||||
|
pub use value::ArcValue;
|
||||||
|
pub use value::Set;
|
||||||
|
pub use value::Map;
|
||||||
|
|
||||||
pub type Set = std::collections::BTreeSet<AValue>;
|
pub fn invert_map<A, B>(m: &Map<A, B>) -> Map<B, A>
|
||||||
pub type Dictionary = std::collections::BTreeMap<AValue, AValue>;
|
|
||||||
|
|
||||||
pub fn invert_map<A, B>(m: &std::collections::BTreeMap<A, B>) -> std::collections::BTreeMap<B, A>
|
|
||||||
where A: Clone, B: Clone + Ord
|
where A: Clone, B: Clone + Ord
|
||||||
{
|
{
|
||||||
use std::iter::FromIterator;
|
use std::iter::FromIterator;
|
||||||
std::collections::BTreeMap::from_iter(m.iter().map(|(a, b)| (b.clone(), a.clone())))
|
Map::from_iter(m.iter().map(|(a, b)| (b.clone(), a.clone())))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,33 +1,36 @@
|
||||||
use crate::value::{Value, AValue, NestedValue, Dictionary};
|
use crate::value::{Value, NestedValue, Map};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use crate::value::error::{Error, Result};
|
use crate::value::error::{Error, Result};
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
pub struct Serializer;
|
pub struct Serializer<N: NestedValue> {
|
||||||
|
phantom: PhantomData<N>
|
||||||
pub struct SerializeDictionary {
|
|
||||||
next_key: Option<AValue>,
|
|
||||||
items: Dictionary,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct SerializeRecord {
|
pub struct SerializeDictionary<N: NestedValue> {
|
||||||
|
next_key: Option<N>,
|
||||||
|
items: Map<N,N>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct SerializeRecord<N: NestedValue> {
|
||||||
name: &'static str,
|
name: &'static str,
|
||||||
vec: Vec<AValue>,
|
vec: Vec<N>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct SerializeSequence {
|
pub struct SerializeSequence<N: NestedValue> {
|
||||||
vec: Vec<AValue>,
|
vec: Vec<N>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl serde::Serializer for Serializer {
|
impl<N: NestedValue> serde::Serializer for Serializer<N> {
|
||||||
type Ok = AValue;
|
type Ok = N;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
type SerializeSeq = SerializeSequence;
|
type SerializeSeq = SerializeSequence<N>;
|
||||||
type SerializeTuple = SerializeRecord;
|
type SerializeTuple = SerializeRecord<N>;
|
||||||
type SerializeTupleStruct = SerializeRecord;
|
type SerializeTupleStruct = SerializeRecord<N>;
|
||||||
type SerializeTupleVariant = SerializeRecord;
|
type SerializeTupleVariant = SerializeRecord<N>;
|
||||||
type SerializeMap = SerializeDictionary;
|
type SerializeMap = SerializeDictionary<N>;
|
||||||
type SerializeStruct = SerializeRecord;
|
type SerializeStruct = SerializeRecord<N>;
|
||||||
type SerializeStructVariant = SerializeRecord;
|
type SerializeStructVariant = SerializeRecord<N>;
|
||||||
|
|
||||||
fn serialize_bool(self, v: bool) -> Result<Self::Ok> {
|
fn serialize_bool(self, v: bool) -> Result<Self::Ok> {
|
||||||
Ok(Value::from(v).wrap())
|
Ok(Value::from(v).wrap())
|
||||||
|
@ -114,7 +117,7 @@ impl serde::Serializer for Serializer {
|
||||||
Result<Self::Ok> where T: Serialize
|
Result<Self::Ok> where T: Serialize
|
||||||
{
|
{
|
||||||
if name == crate::value::value::MAGIC {
|
if name == crate::value::value::MAGIC {
|
||||||
let v = to_value(value)?;
|
let v: crate::value::PlainValue = to_value(value)?;
|
||||||
let mut buf: &[u8] = v.value().as_bytestring().ok_or(Error::InternalMagicError)?;
|
let mut buf: &[u8] = v.value().as_bytestring().ok_or(Error::InternalMagicError)?;
|
||||||
crate::value::Decoder::new(&mut buf, None).next().or(Err(Error::InternalMagicError))
|
crate::value::Decoder::new(&mut buf, None).next().or(Err(Error::InternalMagicError))
|
||||||
} else {
|
} else {
|
||||||
|
@ -159,7 +162,7 @@ impl serde::Serializer for Serializer {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_map(self, _count: Option<usize>) -> Result<Self::SerializeMap> {
|
fn serialize_map(self, _count: Option<usize>) -> Result<Self::SerializeMap> {
|
||||||
Ok(SerializeDictionary{ next_key: None, items: Dictionary::new() })
|
Ok(SerializeDictionary{ next_key: None, items: Map::new() })
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_struct(self, name: &'static str, count: usize) -> Result<Self::SerializeStruct> {
|
fn serialize_struct(self, name: &'static str, count: usize) -> Result<Self::SerializeStruct> {
|
||||||
|
@ -177,8 +180,8 @@ impl serde::Serializer for Serializer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl serde::ser::SerializeMap for SerializeDictionary {
|
impl<N: NestedValue> serde::ser::SerializeMap for SerializeDictionary<N> {
|
||||||
type Ok = AValue;
|
type Ok = N;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<()> where T: Serialize {
|
fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<()> where T: Serialize {
|
||||||
|
@ -197,19 +200,19 @@ impl serde::ser::SerializeMap for SerializeDictionary {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SerializeRecord {
|
impl<N: NestedValue> SerializeRecord<N> {
|
||||||
fn push(&mut self, value: AValue) -> Result<()> {
|
fn push(&mut self, value: N) -> Result<()> {
|
||||||
self.vec.push(value);
|
self.vec.push(value);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn finish(self) -> Result<AValue> {
|
fn finish(self) -> Result<N> {
|
||||||
Ok(Value::simple_record(self.name, self.vec).wrap())
|
Ok(Value::simple_record(self.name, self.vec).wrap())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl serde::ser::SerializeStruct for SerializeRecord {
|
impl<N: NestedValue> serde::ser::SerializeStruct for SerializeRecord<N> {
|
||||||
type Ok = AValue;
|
type Ok = N;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn serialize_field<T: ?Sized>(&mut self, _name: &'static str, value: &T) -> Result<()>
|
fn serialize_field<T: ?Sized>(&mut self, _name: &'static str, value: &T) -> Result<()>
|
||||||
|
@ -223,8 +226,8 @@ impl serde::ser::SerializeStruct for SerializeRecord {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl serde::ser::SerializeStructVariant for SerializeRecord {
|
impl<N: NestedValue> serde::ser::SerializeStructVariant for SerializeRecord<N> {
|
||||||
type Ok = AValue;
|
type Ok = N;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn serialize_field<T: ?Sized>(&mut self, _name: &'static str, value: &T) -> Result<()>
|
fn serialize_field<T: ?Sized>(&mut self, _name: &'static str, value: &T) -> Result<()>
|
||||||
|
@ -238,8 +241,8 @@ impl serde::ser::SerializeStructVariant for SerializeRecord {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl serde::ser::SerializeTuple for SerializeRecord {
|
impl<N: NestedValue> serde::ser::SerializeTuple for SerializeRecord<N> {
|
||||||
type Ok = AValue;
|
type Ok = N;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<()> where T: Serialize {
|
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<()> where T: Serialize {
|
||||||
|
@ -251,8 +254,8 @@ impl serde::ser::SerializeTuple for SerializeRecord {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl serde::ser::SerializeTupleStruct for SerializeRecord {
|
impl<N: NestedValue> serde::ser::SerializeTupleStruct for SerializeRecord<N> {
|
||||||
type Ok = AValue;
|
type Ok = N;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<()> where T: Serialize {
|
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<()> where T: Serialize {
|
||||||
|
@ -264,8 +267,8 @@ impl serde::ser::SerializeTupleStruct for SerializeRecord {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl serde::ser::SerializeTupleVariant for SerializeRecord {
|
impl<N: NestedValue> serde::ser::SerializeTupleVariant for SerializeRecord<N> {
|
||||||
type Ok = AValue;
|
type Ok = N;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<()> where T: Serialize {
|
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<()> where T: Serialize {
|
||||||
|
@ -277,8 +280,8 @@ impl serde::ser::SerializeTupleVariant for SerializeRecord {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl serde::ser::SerializeSeq for SerializeSequence {
|
impl<N: NestedValue> serde::ser::SerializeSeq for SerializeSequence<N> {
|
||||||
type Ok = AValue;
|
type Ok = N;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<()> where T: Serialize {
|
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<()> where T: Serialize {
|
||||||
|
@ -291,6 +294,6 @@ impl serde::ser::SerializeSeq for SerializeSequence {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_value<T>(value: T) -> Result<AValue> where T: Serialize {
|
pub fn to_value<T, N>(value: T) -> Result<N> where T: Serialize, N: NestedValue {
|
||||||
value.serialize(Serializer)
|
value.serialize(Serializer{ phantom: PhantomData })
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,15 +4,15 @@ use std::fmt::Debug;
|
||||||
use num::bigint::BigInt;
|
use num::bigint::BigInt;
|
||||||
use std::vec::Vec;
|
use std::vec::Vec;
|
||||||
use std::string::String;
|
use std::string::String;
|
||||||
use std::collections::BTreeSet as Set;
|
|
||||||
use std::collections::BTreeMap as Map;
|
|
||||||
use std::ops::Deref;
|
|
||||||
use std::ops::Index;
|
use std::ops::Index;
|
||||||
use std::ops::IndexMut;
|
use std::ops::IndexMut;
|
||||||
use num::traits::cast::ToPrimitive;
|
use num::traits::cast::ToPrimitive;
|
||||||
|
|
||||||
|
pub use std::collections::BTreeSet as Set;
|
||||||
|
pub use std::collections::BTreeMap as Map;
|
||||||
|
|
||||||
pub trait NestedValue: Sized + Debug + Clone + Eq + Hash + Ord {
|
pub trait NestedValue: Sized + Debug + Clone + Eq + Hash + Ord {
|
||||||
type BoxType: Sized + Debug + Clone + Eq + Hash + Ord + Deref<Target=Self>;
|
type BoxType: Sized + Debug + Clone + Eq + Hash + Ord;
|
||||||
|
|
||||||
fn wrap(v: Value<Self>) -> Self;
|
fn wrap(v: Value<Self>) -> Self;
|
||||||
fn wrap_ann(anns: Vec<Self>, v: Value<Self>) -> Self;
|
fn wrap_ann(anns: Vec<Self>, v: Value<Self>) -> Self;
|
||||||
|
@ -21,10 +21,7 @@ pub trait NestedValue: Sized + Debug + Clone + Eq + Hash + Ord {
|
||||||
fn boxunwrap(b: &Self::BoxType) -> &Self;
|
fn boxunwrap(b: &Self::BoxType) -> &Self;
|
||||||
|
|
||||||
fn annotations(&self) -> &Vec<Self>;
|
fn annotations(&self) -> &Vec<Self>;
|
||||||
fn annotations_mut(&mut self) -> &mut Vec<Self>;
|
|
||||||
|
|
||||||
fn value(&self) -> &Value<Self>;
|
fn value(&self) -> &Value<Self>;
|
||||||
fn value_mut(&mut self) -> &mut Value<Self>;
|
|
||||||
fn value_owned(self) -> Value<Self>;
|
fn value_owned(self) -> Value<Self>;
|
||||||
|
|
||||||
fn debug_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn debug_fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
@ -56,51 +53,6 @@ pub enum Value<N> where N: NestedValue {
|
||||||
Dictionary(Map<N, N>),
|
Dictionary(Map<N, N>),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An possibly-annotated Value, with annotations (themselves
|
|
||||||
/// possibly-annotated) in order of appearance.
|
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct AValue(pub Vec<AValue>, pub Value<AValue>);
|
|
||||||
|
|
||||||
impl NestedValue for AValue {
|
|
||||||
type BoxType = Box<Self>;
|
|
||||||
|
|
||||||
fn wrap(v: Value<Self>) -> Self {
|
|
||||||
Self::wrap_ann(Vec::new(), v)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn wrap_ann(anns: Vec<Self>, v: Value<Self>) -> Self {
|
|
||||||
AValue(anns, v)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn boxwrap(self) -> Self::BoxType {
|
|
||||||
Box::new(self)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn boxunwrap(b: &Self::BoxType) -> &Self {
|
|
||||||
&**b
|
|
||||||
}
|
|
||||||
|
|
||||||
fn annotations(&self) -> &Vec<Self> {
|
|
||||||
&self.0
|
|
||||||
}
|
|
||||||
|
|
||||||
fn annotations_mut(&mut self) -> &mut Vec<Self> {
|
|
||||||
&mut self.0
|
|
||||||
}
|
|
||||||
|
|
||||||
fn value(&self) -> &Value<Self> {
|
|
||||||
&self.1
|
|
||||||
}
|
|
||||||
|
|
||||||
fn value_mut(&mut self) -> &mut Value<Self> {
|
|
||||||
&mut self.1
|
|
||||||
}
|
|
||||||
|
|
||||||
fn value_owned(self) -> Value<Self> {
|
|
||||||
self.1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Single-precision IEEE 754 Value
|
/// Single-precision IEEE 754 Value
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Float(pub f32);
|
pub struct Float(pub f32);
|
||||||
|
@ -196,38 +148,6 @@ impl PartialOrd for Double {
|
||||||
|
|
||||||
impl Eq for Double {}
|
impl Eq for Double {}
|
||||||
|
|
||||||
impl PartialEq for AValue {
|
|
||||||
fn eq(&self, other: &Self) -> bool {
|
|
||||||
self.value().eq(other.value())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Eq for AValue {}
|
|
||||||
|
|
||||||
impl Hash for AValue {
|
|
||||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
|
||||||
self.1.hash(state);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialOrd for AValue {
|
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
||||||
Some(self.cmp(other))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Ord for AValue {
|
|
||||||
fn cmp(&self, other: &Self) -> Ordering {
|
|
||||||
self.value().cmp(&other.value())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Debug for AValue {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
self.debug_fmt(f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<N: NestedValue> From<bool> for Value<N> { fn from(v: bool) -> Self { Value::Boolean(v) } }
|
impl<N: NestedValue> From<bool> for Value<N> { fn from(v: bool) -> Self { Value::Boolean(v) } }
|
||||||
|
|
||||||
impl<N: NestedValue> From<f32> for Value<N> { fn from(v: f32) -> Self { Value::Float(Float::from(v)) } }
|
impl<N: NestedValue> From<f32> for Value<N> { fn from(v: f32) -> Self { Value::Float(Float::from(v)) } }
|
||||||
|
@ -492,7 +412,7 @@ impl<N: NestedValue> Value<N> {
|
||||||
|
|
||||||
pub fn as_simple_record(&self, label: &str, arity: Option<usize>) -> Option<&Vec<N>> {
|
pub fn as_simple_record(&self, label: &str, arity: Option<usize>) -> Option<&Vec<N>> {
|
||||||
self.as_record().and_then(|(lp,fs)| {
|
self.as_record().and_then(|(lp,fs)| {
|
||||||
match *lp.value() {
|
match N::boxunwrap(lp).value() {
|
||||||
Value::Symbol(ref s) if s == label =>
|
Value::Symbol(ref s) if s == label =>
|
||||||
match arity {
|
match arity {
|
||||||
Some(expected) if fs.len() == expected => Some(fs),
|
Some(expected) if fs.len() == expected => Some(fs),
|
||||||
|
@ -643,15 +563,6 @@ impl<N: NestedValue> serde::Serialize for Value<N> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl serde::Serialize for AValue {
|
|
||||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
|
|
||||||
let mut buf: Vec<u8> = Vec::new();
|
|
||||||
crate::value::Encoder::new(&mut buf, None).write(self)
|
|
||||||
.or(Err(serde::ser::Error::custom("Internal error")))?;
|
|
||||||
ValueWrapper(buf).serialize(serializer)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'de, N: NestedValue> serde::Deserialize<'de> for Value<N> {
|
impl<'de, N: NestedValue> serde::Deserialize<'de> for Value<N> {
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de> {
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de> {
|
||||||
let ValueWrapper(buf) = ValueWrapper::deserialize(deserializer)?;
|
let ValueWrapper(buf) = ValueWrapper::deserialize(deserializer)?;
|
||||||
|
@ -661,10 +572,235 @@ impl<'de, N: NestedValue> serde::Deserialize<'de> for Value<N> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'de> serde::Deserialize<'de> for AValue {
|
pub fn serialize_nested_value<'de, S, N>(v: &N, serializer: S) ->
|
||||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de> {
|
Result<S::Ok, S::Error> where S: serde::Serializer, N: NestedValue
|
||||||
|
{
|
||||||
|
use serde::Serialize;
|
||||||
|
let mut buf: Vec<u8> = Vec::new();
|
||||||
|
crate::value::Encoder::new(&mut buf, None).write(v)
|
||||||
|
.or(Err(serde::ser::Error::custom("Internal error")))?;
|
||||||
|
ValueWrapper(buf).serialize(serializer)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deserialize_nested_value<'de, D, N>(deserializer: D) ->
|
||||||
|
Result<N, D::Error> where D: serde::Deserializer<'de>, N: NestedValue
|
||||||
|
{
|
||||||
|
use serde::Deserialize;
|
||||||
let ValueWrapper(buf) = ValueWrapper::deserialize(deserializer)?;
|
let ValueWrapper(buf) = ValueWrapper::deserialize(deserializer)?;
|
||||||
crate::value::Decoder::new(&mut &buf[..], None).next()
|
crate::value::Decoder::new(&mut &buf[..], None).next()
|
||||||
.or(Err(serde::de::Error::custom("Internal error")))
|
.or(Err(serde::de::Error::custom("Internal error")))
|
||||||
|
}
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/// An possibly-annotated Value, with annotations (themselves
|
||||||
|
/// possibly-annotated) in order of appearance.
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct AnnotatedValue<N: NestedValue>(pub Vec<N>, pub Value<N>);
|
||||||
|
|
||||||
|
impl<N: NestedValue> PartialEq for AnnotatedValue<N> {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
self.1.eq(&other.1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<N: NestedValue> Eq for AnnotatedValue<N> {}
|
||||||
|
|
||||||
|
impl<N: NestedValue> Hash for AnnotatedValue<N> {
|
||||||
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||||
|
self.1.hash(state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<N: NestedValue> PartialOrd for AnnotatedValue<N> {
|
||||||
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||||
|
Some(self.cmp(other))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<N: NestedValue> Ord for AnnotatedValue<N> {
|
||||||
|
fn cmp(&self, other: &Self) -> Ordering {
|
||||||
|
self.1.cmp(&other.1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||||
|
pub struct PlainValue(AnnotatedValue<PlainValue>);
|
||||||
|
|
||||||
|
impl NestedValue for PlainValue {
|
||||||
|
type BoxType = Box<Self>;
|
||||||
|
|
||||||
|
fn wrap(v: Value<Self>) -> Self {
|
||||||
|
Self::wrap_ann(Vec::new(), v)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn wrap_ann(anns: Vec<Self>, v: Value<Self>) -> Self {
|
||||||
|
PlainValue(AnnotatedValue(anns, v))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn boxwrap(self) -> Self::BoxType {
|
||||||
|
Box::new(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn boxunwrap(b: &Self::BoxType) -> &Self {
|
||||||
|
&**b
|
||||||
|
}
|
||||||
|
|
||||||
|
fn annotations(&self) -> &Vec<Self> {
|
||||||
|
&(self.0).0
|
||||||
|
}
|
||||||
|
|
||||||
|
fn value(&self) -> &Value<Self> {
|
||||||
|
&(self.0).1
|
||||||
|
}
|
||||||
|
|
||||||
|
fn value_owned(self) -> Value<Self> {
|
||||||
|
(self.0).1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Debug for PlainValue {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
self.debug_fmt(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl serde::Serialize for PlainValue {
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
|
||||||
|
serialize_nested_value(self, serializer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de> serde::Deserialize<'de> for PlainValue {
|
||||||
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de> {
|
||||||
|
deserialize_nested_value(deserializer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
|
pub struct RcValue(Rc<AnnotatedValue<RcValue>>);
|
||||||
|
|
||||||
|
impl NestedValue for RcValue {
|
||||||
|
type BoxType = Self;
|
||||||
|
|
||||||
|
fn wrap(v: Value<Self>) -> Self {
|
||||||
|
Self::wrap_ann(Vec::new(), v)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn wrap_ann(anns: Vec<Self>, v: Value<Self>) -> Self {
|
||||||
|
RcValue(Rc::new(AnnotatedValue(anns, v)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn boxwrap(self) -> Self::BoxType {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
fn boxunwrap(b: &Self::BoxType) -> &Self {
|
||||||
|
b
|
||||||
|
}
|
||||||
|
|
||||||
|
fn annotations(&self) -> &Vec<Self> {
|
||||||
|
&(self.0).0
|
||||||
|
}
|
||||||
|
|
||||||
|
fn value(&self) -> &Value<Self> {
|
||||||
|
&(self.0).1
|
||||||
|
}
|
||||||
|
|
||||||
|
fn value_owned(self) -> Value<Self> {
|
||||||
|
Rc::try_unwrap(self.0).unwrap_or_else(|_| panic!("value_owned on RcValue with refcount greater than one")).1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Hash for RcValue {
|
||||||
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||||
|
(self.0).1.hash(state)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Debug for RcValue {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
self.debug_fmt(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl serde::Serialize for RcValue {
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
|
||||||
|
serialize_nested_value(self, serializer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de> serde::Deserialize<'de> for RcValue {
|
||||||
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de> {
|
||||||
|
deserialize_nested_value(deserializer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
|
pub struct ArcValue(Arc<AnnotatedValue<ArcValue>>);
|
||||||
|
|
||||||
|
impl NestedValue for ArcValue {
|
||||||
|
type BoxType = Self;
|
||||||
|
|
||||||
|
fn wrap(v: Value<Self>) -> Self {
|
||||||
|
Self::wrap_ann(Vec::new(), v)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn wrap_ann(anns: Vec<Self>, v: Value<Self>) -> Self {
|
||||||
|
ArcValue(Arc::new(AnnotatedValue(anns, v)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn boxwrap(self) -> Self::BoxType {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
fn boxunwrap(b: &Self::BoxType) -> &Self {
|
||||||
|
b
|
||||||
|
}
|
||||||
|
|
||||||
|
fn annotations(&self) -> &Vec<Self> {
|
||||||
|
&(self.0).0
|
||||||
|
}
|
||||||
|
|
||||||
|
fn value(&self) -> &Value<Self> {
|
||||||
|
&(self.0).1
|
||||||
|
}
|
||||||
|
|
||||||
|
fn value_owned(self) -> Value<Self> {
|
||||||
|
Arc::try_unwrap(self.0).unwrap_or_else(|_| panic!("value_owned on ArcValue with refcount greater than one")).1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Hash for ArcValue {
|
||||||
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||||
|
(self.0).1.hash(state)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Debug for ArcValue {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
self.debug_fmt(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl serde::Serialize for ArcValue {
|
||||||
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
|
||||||
|
serialize_nested_value(self, serializer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'de> serde::Deserialize<'de> for ArcValue {
|
||||||
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de> {
|
||||||
|
deserialize_nested_value(deserializer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue