preserves/implementations/rust/src/value/value.rs

805 lines
23 KiB
Rust
Raw Normal View History

2019-06-29 22:02:27 +00:00
use std::hash::{Hash,Hasher};
use std::cmp::{Ordering};
use std::fmt::Debug;
2019-06-29 22:02:27 +00:00
use num::bigint::BigInt;
use std::vec::Vec;
use std::string::String;
use std::ops::Index;
use std::ops::IndexMut;
2019-09-16 23:58:32 +00:00
use num::traits::cast::ToPrimitive;
2019-06-29 22:02:27 +00:00
pub use std::collections::BTreeSet as Set;
pub use std::collections::BTreeMap as Map;
pub trait NestedValue: Sized + Debug + Clone + Eq + Hash + Ord {
type BoxType: Sized + Debug + Clone + Eq + Hash + Ord;
fn wrap(v: Value<Self>) -> Self;
fn wrap_ann(anns: Vec<Self>, v: Value<Self>) -> Self;
fn boxwrap(self) -> Self::BoxType;
fn boxunwrap(b: &Self::BoxType) -> &Self;
fn annotations(&self) -> &[Self];
fn value(&self) -> &Value<Self>;
fn value_owned(self) -> Value<Self>;
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<M: NestedValue>(&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<N> where N: NestedValue {
2019-06-29 22:02:27 +00:00
Boolean(bool),
Float(Float),
Double(Double),
SignedInteger(BigInt),
String(String),
ByteString(Vec<u8>),
Symbol(String),
Record(Record<N>),
Sequence(Vec<N>),
Set(Set<N>),
Dictionary(Map<N, N>),
2019-06-29 22:02:27 +00:00
}
/// Single-precision IEEE 754 Value
2019-06-29 22:02:27 +00:00
#[derive(Clone, Debug)]
2019-09-09 21:08:26 +00:00
pub struct Float(pub f32);
2019-06-29 22:02:27 +00:00
/// Double-precision IEEE 754 Value
2019-06-29 22:02:27 +00:00
#[derive(Clone, Debug)]
2019-09-09 21:08:26 +00:00
pub struct Double(pub f64);
2019-06-29 22:02:27 +00:00
/// A Record `Value`
pub type Record<N> = (<N as NestedValue>::BoxType, Vec<N>);
2019-09-09 21:08:26 +00:00
2019-06-29 22:02:27 +00:00
impl From<f32> for Float {
fn from(v: f32) -> Self {
Float(v)
}
}
impl From<Float> for f32 {
fn from(v: Float) -> Self {
v.0
}
}
impl Hash for Float {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.to_bits().hash(state);
}
}
impl PartialEq for Float {
fn eq(&self, other: &Self) -> bool {
self.0.to_bits() == other.0.to_bits()
}
}
impl Ord for Float {
fn cmp(&self, other: &Self) -> Ordering {
2019-07-04 09:47:32 +00:00
let mut a: u32 = self.0.to_bits();
let mut b: u32 = other.0.to_bits();
2019-06-29 22:02:27 +00:00
if a & 0x80000000 != 0 { a ^= 0x7fffffff; }
if b & 0x80000000 != 0 { b ^= 0x7fffffff; }
2019-06-30 22:43:32 +00:00
(a as i32).cmp(&(b as i32))
2019-06-29 22:02:27 +00:00
}
}
impl PartialOrd for Float {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Eq for Float {}
impl From<f64> for Double {
fn from(v: f64) -> Self {
Double(v)
}
}
impl From<Double> for f64 {
fn from(v: Double) -> Self {
v.0
}
}
impl Hash for Double {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.to_bits().hash(state);
}
}
impl PartialEq for Double {
fn eq(&self, other: &Self) -> bool {
return self.0.to_bits() == other.0.to_bits();
}
}
impl Ord for Double {
fn cmp(&self, other: &Self) -> Ordering {
2019-07-04 09:47:32 +00:00
let mut a: u64 = self.0.to_bits();
let mut b: u64 = other.0.to_bits();
2019-06-29 22:02:27 +00:00
if a & 0x8000000000000000 != 0 { a ^= 0x7fffffffffffffff; }
if b & 0x8000000000000000 != 0 { b ^= 0x7fffffffffffffff; }
2019-06-30 22:43:32 +00:00
(a as i64).cmp(&(b as i64))
2019-06-29 22:02:27 +00:00
}
}
impl PartialOrd for Double {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Eq for Double {}
impl<N: NestedValue> From<bool> for Value<N> { fn from(v: bool) -> Self { Value::Boolean(v) } }
2019-06-29 22:02:27 +00:00
impl<N: NestedValue> From<f32> for Value<N> { fn from(v: f32) -> Self { Value::Float(Float::from(v)) } }
impl<N: NestedValue> From<f64> for Value<N> { fn from(v: f64) -> Self { Value::Double(Double::from(v)) } }
2019-06-29 22:02:27 +00:00
impl<N: NestedValue> From<u8> for Value<N> { fn from(v: u8) -> Self { Value::from(v as i128) } }
impl<N: NestedValue> From<i8> for Value<N> { fn from(v: i8) -> Self { Value::from(v as i128) } }
impl<N: NestedValue> From<u16> for Value<N> { fn from(v: u16) -> Self { Value::from(v as i128) } }
impl<N: NestedValue> From<i16> for Value<N> { fn from(v: i16) -> Self { Value::from(v as i128) } }
impl<N: NestedValue> From<u32> for Value<N> { fn from(v: u32) -> Self { Value::from(v as i128) } }
impl<N: NestedValue> From<i32> for Value<N> { fn from(v: i32) -> Self { Value::from(v as i128) } }
impl<N: NestedValue> From<u64> for Value<N> { fn from(v: u64) -> Self { Value::from(v as i128) } }
impl<N: NestedValue> From<i64> for Value<N> { fn from(v: i64) -> Self { Value::from(v as i128) } }
impl<N: NestedValue> From<u128> for Value<N> { fn from(v: u128) -> Self { Value::SignedInteger(BigInt::from(v)) } }
impl<N: NestedValue> From<i128> for Value<N> { fn from(v: i128) -> Self { Value::SignedInteger(BigInt::from(v)) } }
impl<N: NestedValue> From<BigInt> for Value<N> { fn from(v: BigInt) -> Self { Value::SignedInteger(v) } }
2019-06-29 22:02:27 +00:00
impl<N: NestedValue> From<&str> for Value<N> { fn from(v: &str) -> Self { Value::String(String::from(v)) } }
impl<N: NestedValue> From<String> for Value<N> { fn from(v: String) -> Self { Value::String(v) } }
impl<N: NestedValue> From<&[u8]> for Value<N> { fn from(v: &[u8]) -> Self { Value::ByteString(Vec::from(v)) } }
impl<N: NestedValue> From<Vec<u8>> for Value<N> { fn from(v: Vec<u8>) -> Self { Value::ByteString(v) } }
impl<N: NestedValue> From<Vec<N>> for Value<N> { fn from(v: Vec<N>) -> Self { Value::Sequence(v) } }
impl<N: NestedValue> From<Set<N>> for Value<N> { fn from(v: Set<N>) -> Self { Value::Set(v) } }
impl<N: NestedValue> From<Map<N, N>> for Value<N> { fn from(v: Map<N, N>) -> Self { Value::Dictionary(v) } }
2019-09-09 21:08:26 +00:00
impl<N: NestedValue> Debug for Value<N> {
// 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 {
match self {
Value::Boolean(false) => f.write_str("#false"),
2019-09-15 15:16:17 +00:00
Value::Boolean(true) => f.write_str("#true"),
Value::Float(Float(v)) => write!(f, "{:?}f", v),
Value::Double(Double(v)) => write!(f, "{:?}", v),
Value::SignedInteger(v) => write!(f, "{}", v),
Value::String(ref v) => write!(f, "{:?}", v), // TODO: proper escaping!
Value::ByteString(ref v) => {
f.write_str("#hex{")?;
for b in v { write!(f, "{:02x}", b)? }
f.write_str("}")
}
Value::Symbol(ref v) => {
// TODO: proper escaping!
if v.len() == 0 {
f.write_str("||")
} else {
write!(f, "{}", v)
}
}
Value::Record((ref l, ref fs)) => {
f.write_str("<")?;
l.fmt(f)?;
for v in fs {
f.write_str(" ")?;
v.fmt(f)?;
}
f.write_str(">")
}
Value::Sequence(ref v) => v.fmt(f),
Value::Set(ref v) => {
f.write_str("#set")?;
f.debug_set().entries(v.iter()).finish()
}
Value::Dictionary(ref v) => f.debug_map().entries(v.iter()).finish()
}
}
}
//---------------------------------------------------------------------------
impl<N: NestedValue> Value<N> {
pub fn wrap(self) -> N {
N::wrap(self)
}
pub fn is_boolean(&self) -> bool {
self.as_boolean().is_some()
}
pub fn as_boolean(&self) -> Option<bool> {
if let Value::Boolean(b) = *self {
Some(b)
} else {
None
}
}
pub fn as_boolean_mut(&mut self) -> Option<&mut bool> {
if let Value::Boolean(ref mut b) = *self {
Some(b)
} else {
None
}
}
pub fn is_float(&self) -> bool {
self.as_float().is_some()
}
pub fn as_float(&self) -> Option<f32> {
if let Value::Float(Float(f)) = *self {
Some(f)
} else {
None
}
}
pub fn as_float_mut(&mut self) -> Option<&mut f32> {
if let Value::Float(Float(ref mut f)) = *self {
Some(f)
} else {
None
}
}
pub fn is_double(&self) -> bool {
self.as_double().is_some()
}
pub fn as_double(&self) -> Option<f64> {
if let Value::Double(Double(f)) = *self {
Some(f)
} else {
None
}
}
pub fn as_double_mut(&mut self) -> Option<&mut f64> {
if let Value::Double(Double(ref mut f)) = *self {
Some(f)
} else {
None
}
}
pub fn is_signedinteger(&self) -> bool {
self.as_signedinteger().is_some()
}
pub fn as_signedinteger(&self) -> Option<&BigInt> {
if let Value::SignedInteger(ref i) = *self {
Some(i)
} else {
None
}
}
pub fn as_signedinteger_mut(&mut self) -> Option<&mut BigInt> {
if let Value::SignedInteger(ref mut i) = *self {
Some(i)
} else {
None
}
}
2019-09-16 23:58:32 +00:00
pub fn as_u8(&self) -> Option<u8> { self.as_signedinteger().and_then(|i| i.to_u8()) }
pub fn as_i8(&self) -> Option<i8> { self.as_signedinteger().and_then(|i| i.to_i8()) }
pub fn as_u16(&self) -> Option<u16> { self.as_signedinteger().and_then(|i| i.to_u16()) }
pub fn as_i16(&self) -> Option<i16> { self.as_signedinteger().and_then(|i| i.to_i16()) }
pub fn as_u32(&self) -> Option<u32> { self.as_signedinteger().and_then(|i| i.to_u32()) }
pub fn as_i32(&self) -> Option<i32> { self.as_signedinteger().and_then(|i| i.to_i32()) }
pub fn as_u64(&self) -> Option<u64> { self.as_signedinteger().and_then(|i| i.to_u64()) }
pub fn as_i64(&self) -> Option<i64> { self.as_signedinteger().and_then(|i| i.to_i64()) }
pub fn is_string(&self) -> bool {
self.as_string().is_some()
}
pub fn as_string(&self) -> Option<&String> {
if let Value::String(ref s) = *self {
Some(s)
} else {
None
}
}
pub fn as_string_mut(&mut self) -> Option<&mut String> {
if let Value::String(ref mut s) = *self {
Some(s)
} else {
None
}
}
pub fn is_bytestring(&self) -> bool {
self.as_bytestring().is_some()
}
pub fn as_bytestring(&self) -> Option<&Vec<u8>> {
if let Value::ByteString(ref s) = *self {
Some(s)
} else {
None
}
}
pub fn as_bytestring_mut(&mut self) -> Option<&mut Vec<u8>> {
if let Value::ByteString(ref mut s) = *self {
Some(s)
} else {
None
}
}
pub fn symbol(s: &str) -> Value<N> {
Value::Symbol(s.to_string())
}
pub fn is_symbol(&self) -> bool {
self.as_symbol().is_some()
}
pub fn as_symbol(&self) -> Option<&String> {
if let Value::Symbol(ref s) = *self {
Some(s)
} else {
None
}
}
pub fn as_symbol_mut(&mut self) -> Option<&mut String> {
if let Value::Symbol(ref mut s) = *self {
Some(s)
} else {
None
}
}
pub fn record(label: N, fields: Vec<N>) -> Value<N> {
Value::Record((label.boxwrap(), fields))
}
pub fn is_record(&self) -> bool {
self.as_record().is_some()
}
pub fn as_record(&self) -> Option<&Record<N>> {
if let Value::Record(ref r) = *self {
Some(r)
} else {
None
}
}
pub fn as_record_mut(&mut self) -> Option<&mut Record<N>> {
if let Value::Record(ref mut r) = *self {
Some(r)
} else {
None
}
}
pub fn simple_record(label: &str, fields: Vec<N>) -> Value<N> {
2019-09-19 20:35:00 +00:00
Value::record(Value::symbol(label).wrap(), fields)
2019-09-16 23:58:32 +00:00
}
pub fn is_simple_record(&self, label: &str, arity: Option<usize>) -> bool {
self.as_simple_record(label, arity).is_some()
}
pub fn as_simple_record(&self, label: &str, arity: Option<usize>) -> Option<&Vec<N>> {
2019-09-16 23:58:32 +00:00
self.as_record().and_then(|(lp,fs)| {
match N::boxunwrap(lp).value() {
2019-09-16 23:58:32 +00:00
Value::Symbol(ref s) if s == label =>
match arity {
Some(expected) if fs.len() == expected => Some(fs),
Some(_other) => None,
None => Some(fs)
}
_ => None
}
})
}
pub fn is_sequence(&self) -> bool {
self.as_sequence().is_some()
}
pub fn as_sequence(&self) -> Option<&Vec<N>> {
if let Value::Sequence(ref s) = *self {
Some(s)
} else {
None
}
}
pub fn as_sequence_mut(&mut self) -> Option<&mut Vec<N>> {
if let Value::Sequence(ref mut s) = *self {
Some(s)
} else {
None
}
}
pub fn is_set(&self) -> bool {
self.as_set().is_some()
}
pub fn as_set(&self) -> Option<&Set<N>> {
if let Value::Set(ref s) = *self {
Some(s)
} else {
None
}
}
pub fn as_set_mut(&mut self) -> Option<&mut Set<N>> {
if let Value::Set(ref mut s) = *self {
Some(s)
} else {
None
}
}
pub fn is_dictionary(&self) -> bool {
self.as_dictionary().is_some()
}
pub fn as_dictionary(&self) -> Option<&Map<N, N>> {
if let Value::Dictionary(ref s) = *self {
Some(s)
} else {
None
}
}
pub fn as_dictionary_mut(&mut self) -> Option<&mut Map<N, N>> {
if let Value::Dictionary(ref mut s) = *self {
Some(s)
} else {
None
}
}
pub fn copy_via<M: NestedValue>(&self) -> Value<M> {
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::<M>().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<N: NestedValue> Index<usize> for Value<N> {
type Output = N;
fn index(&self, i: usize) -> &Self::Output {
&self.as_sequence().unwrap()[i]
}
}
impl<N: NestedValue> IndexMut<usize> for Value<N> {
fn index_mut(&mut self, i: usize) -> &mut Self::Output {
&mut self.as_sequence_mut().unwrap()[i]
}
}
impl<N: NestedValue> Index<&N> for Value<N> {
type Output = N;
fn index(&self, i: &N) -> &Self::Output {
&(*self.as_dictionary().unwrap())[i]
}
}
2019-09-16 23:58:32 +00:00
//---------------------------------------------------------------------------
// This part is a terrible hack
pub static MAGIC: &str = "$____Preserves_Value";
#[derive(serde::Serialize)]
#[serde(rename = "$____Preserves_Value")]
struct ValueWrapper(#[serde(with = "serde_bytes")] Vec<u8>);
struct ValueWrapperVisitor;
impl<'de> serde::de::Visitor<'de> for ValueWrapperVisitor {
type Value = ValueWrapper;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(formatter, "an encoded value wrapper")
}
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> where E: serde::de::Error {
Ok(ValueWrapper(Vec::from(v)))
}
}
impl<'de> serde::Deserialize<'de> for ValueWrapper {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de> {
deserializer.deserialize_newtype_struct(MAGIC, ValueWrapperVisitor)
}
}
impl<N: NestedValue> serde::Serialize for Value<N> {
2019-09-16 23:58:32 +00:00
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_value(self)
.or(Err(serde::ser::Error::custom("Internal error")))?;
ValueWrapper(buf).serialize(serializer)
}
}
impl<'de, N: NestedValue> serde::Deserialize<'de> for Value<N> {
2019-09-16 23:58:32 +00:00
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: serde::Deserializer<'de> {
let ValueWrapper(buf) = ValueWrapper::deserialize(deserializer)?;
let v: N = crate::value::Decoder::new(&mut &buf[..], None).next()
2019-09-16 23:58:32 +00:00
.or(Err(serde::de::Error::custom("Internal error")))?;
Ok(v.value_owned())
2019-09-16 23:58:32 +00:00
}
}
pub fn serialize_nested_value<'de, S, N>(v: &N, serializer: S) ->
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)?;
crate::value::Decoder::new(&mut &buf[..], None).next()
.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 PlainValue {
pub fn annotations_mut(&mut self) -> &mut Vec<Self> {
&mut (self.0).0
}
pub fn value_mut(&mut self) -> &mut Value<Self> {
&mut (self.0).1
}
}
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) -> &[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 {
2019-09-16 23:58:32 +00:00
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, Hash, 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) -> &[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 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, Hash, 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) -> &[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 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)
2019-09-16 23:58:32 +00:00
}
}