forked from syndicate-lang/preserves
Cleanups; make as_float etc return Float, add as_f32 etc
This commit is contained in:
parent
3156180601
commit
fc8709706c
|
@ -103,12 +103,12 @@ impl<'r, 'de, 'a, R: Reader<'de>> serde::de::Deserializer<'de> for &'a mut Deser
|
||||||
|
|
||||||
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
{
|
{
|
||||||
visitor.visit_f32(self.read.next_float()?)
|
visitor.visit_f32(self.read.next_f32()?)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
{
|
{
|
||||||
visitor.visit_f64(self.read.next_double()?)
|
visitor.visit_f64(self.read.next_f64()?)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
|
|
|
@ -164,18 +164,18 @@ mod value_tests {
|
||||||
|
|
||||||
#[test] fn float_mut() {
|
#[test] fn float_mut() {
|
||||||
let mut f = VV::from(1.0f32);
|
let mut f = VV::from(1.0f32);
|
||||||
assert!(f.is_float());
|
assert!(f.is_f32());
|
||||||
*(f.as_float_mut().unwrap()) = 123.45;
|
*(f.as_f32_mut().unwrap()) = 123.45;
|
||||||
assert_eq!(f, VV::from(123.45f32));
|
assert_eq!(f, VV::from(123.45f32));
|
||||||
assert_eq!((f.as_float().unwrap() - 123.45f32).abs() < std::f32::EPSILON, true);
|
assert_eq!((f.as_f32().unwrap() - 123.45f32).abs() < std::f32::EPSILON, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test] fn double_mut() {
|
#[test] fn double_mut() {
|
||||||
let mut f = VV::from(1.0);
|
let mut f = VV::from(1.0);
|
||||||
assert!(f.is_double());
|
assert!(f.is_f64());
|
||||||
*(f.as_double_mut().unwrap()) = 123.45;
|
*(f.as_f64_mut().unwrap()) = 123.45;
|
||||||
assert_eq!(f, VV::from(123.45));
|
assert_eq!(f, VV::from(123.45));
|
||||||
assert_eq!((f.as_double().unwrap() - 123.45).abs() < std::f64::EPSILON, true);
|
assert_eq!((f.as_f64().unwrap() - 123.45).abs() < std::f64::EPSILON, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test] fn signedinteger_mut() {
|
#[test] fn signedinteger_mut() {
|
||||||
|
|
|
@ -118,17 +118,17 @@ impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut Deserializer<'de>
|
||||||
|
|
||||||
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
{
|
{
|
||||||
match self.input.value().as_double() {
|
match self.input.value().as_f64() {
|
||||||
Some(d) => visitor.visit_f32(d as f32),
|
Some(d) => visitor.visit_f32(d as f32),
|
||||||
None => visitor.visit_f32(self.input.value().to_float()?),
|
None => visitor.visit_f32(self.input.value().to_f32()?),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value> where V: Visitor<'de>
|
||||||
{
|
{
|
||||||
match self.input.value().as_float() {
|
match self.input.value().as_f32() {
|
||||||
Some(f) => visitor.visit_f64(f as f64),
|
Some(f) => visitor.visit_f64(f as f64),
|
||||||
None => visitor.visit_f64(self.input.value().to_double()?),
|
None => visitor.visit_f64(self.input.value().to_f64()?),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,8 @@ pub use reader::Reader;
|
||||||
pub use repr::AnnotatedValue;
|
pub use repr::AnnotatedValue;
|
||||||
pub use repr::ArcValue;
|
pub use repr::ArcValue;
|
||||||
pub use repr::Domain;
|
pub use repr::Domain;
|
||||||
|
pub use repr::Double;
|
||||||
|
pub use repr::Float;
|
||||||
pub use repr::IOValue;
|
pub use repr::IOValue;
|
||||||
pub use repr::Map;
|
pub use repr::Map;
|
||||||
pub use repr::NestedValue;
|
pub use repr::NestedValue;
|
||||||
|
|
|
@ -401,7 +401,7 @@ impl<'de, S: BinarySource<'de>> Reader<'de> for PackedReader<'de, S> {
|
||||||
fn next_u64(&mut self) -> ReaderResult<u64> { self.next_unsigned(|n| n.to_u64()) }
|
fn next_u64(&mut self) -> ReaderResult<u64> { self.next_unsigned(|n| n.to_u64()) }
|
||||||
fn next_u128(&mut self) -> ReaderResult<u128> { self.next_unsigned(|n| n.to_u128()) }
|
fn next_u128(&mut self) -> ReaderResult<u128> { self.next_unsigned(|n| n.to_u128()) }
|
||||||
|
|
||||||
fn next_float(&mut self) -> ReaderResult<f32> {
|
fn next_f32(&mut self) -> ReaderResult<f32> {
|
||||||
match self.peek_next_nonannotation_tag()? {
|
match self.peek_next_nonannotation_tag()? {
|
||||||
Tag::Float => {
|
Tag::Float => {
|
||||||
self.skip()?;
|
self.skip()?;
|
||||||
|
@ -419,7 +419,7 @@ impl<'de, S: BinarySource<'de>> Reader<'de> for PackedReader<'de, S> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn next_double(&mut self) -> ReaderResult<f64> {
|
fn next_f64(&mut self) -> ReaderResult<f64> {
|
||||||
match self.peek_next_nonannotation_tag()? {
|
match self.peek_next_nonannotation_tag()? {
|
||||||
Tag::Float => {
|
Tag::Float => {
|
||||||
self.skip()?;
|
self.skip()?;
|
||||||
|
|
|
@ -41,8 +41,8 @@ pub trait Reader<'de> {
|
||||||
fn next_u64(&mut self) -> ReaderResult<u64> { self.demand_next(false)?.value().to_u64() }
|
fn next_u64(&mut self) -> ReaderResult<u64> { self.demand_next(false)?.value().to_u64() }
|
||||||
fn next_i128(&mut self) -> ReaderResult<i128> { self.demand_next(false)?.value().to_i128() }
|
fn next_i128(&mut self) -> ReaderResult<i128> { self.demand_next(false)?.value().to_i128() }
|
||||||
fn next_u128(&mut self) -> ReaderResult<u128> { self.demand_next(false)?.value().to_u128() }
|
fn next_u128(&mut self) -> ReaderResult<u128> { self.demand_next(false)?.value().to_u128() }
|
||||||
fn next_float(&mut self) -> ReaderResult<f32> { self.demand_next(false)?.value().to_float() }
|
fn next_f32(&mut self) -> ReaderResult<f32> { self.demand_next(false)?.value().to_f32() }
|
||||||
fn next_double(&mut self) -> ReaderResult<f64> { self.demand_next(false)?.value().to_double() }
|
fn next_f64(&mut self) -> ReaderResult<f64> { self.demand_next(false)?.value().to_f64() }
|
||||||
fn next_char(&mut self) -> ReaderResult<char> { self.demand_next(false)?.value().to_char() }
|
fn next_char(&mut self) -> ReaderResult<char> { self.demand_next(false)?.value().to_char() }
|
||||||
|
|
||||||
fn next_str(&mut self) -> ReaderResult<Cow<'de, str>> {
|
fn next_str(&mut self) -> ReaderResult<Cow<'de, str>> {
|
||||||
|
|
|
@ -242,13 +242,13 @@ impl<N: NestedValue<D>, D: Domain> Debug for Value<N, D> {
|
||||||
Value::Float(Float(v)) => write!(f, "{:?}f", v),
|
Value::Float(Float(v)) => write!(f, "{:?}f", v),
|
||||||
Value::Double(Double(v)) => write!(f, "{:?}", v),
|
Value::Double(Double(v)) => write!(f, "{:?}", v),
|
||||||
Value::SignedInteger(v) => write!(f, "{}", v),
|
Value::SignedInteger(v) => write!(f, "{}", v),
|
||||||
Value::String(ref v) => write!(f, "{:?}", v), // TODO: proper escaping!
|
Value::String(v) => write!(f, "{:?}", v), // TODO: proper escaping!
|
||||||
Value::ByteString(ref v) => {
|
Value::ByteString(v) => {
|
||||||
f.write_str("#x\"")?;
|
f.write_str("#x\"")?;
|
||||||
for b in v { write!(f, "{:02x}", b)? }
|
for b in v { write!(f, "{:02x}", b)? }
|
||||||
f.write_str("\"")
|
f.write_str("\"")
|
||||||
}
|
}
|
||||||
Value::Symbol(ref v) => {
|
Value::Symbol(v) => {
|
||||||
// TODO: proper escaping!
|
// TODO: proper escaping!
|
||||||
if v.is_empty() {
|
if v.is_empty() {
|
||||||
f.write_str("||")
|
f.write_str("||")
|
||||||
|
@ -256,7 +256,7 @@ impl<N: NestedValue<D>, D: Domain> Debug for Value<N, D> {
|
||||||
write!(f, "{}", v)
|
write!(f, "{}", v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Value::Record(ref r) => {
|
Value::Record(r) => {
|
||||||
f.write_str("<")?;
|
f.write_str("<")?;
|
||||||
r.label().fmt(f)?;
|
r.label().fmt(f)?;
|
||||||
for v in r.fields() {
|
for v in r.fields() {
|
||||||
|
@ -265,13 +265,13 @@ impl<N: NestedValue<D>, D: Domain> Debug for Value<N, D> {
|
||||||
}
|
}
|
||||||
f.write_str(">")
|
f.write_str(">")
|
||||||
}
|
}
|
||||||
Value::Sequence(ref v) => v.fmt(f),
|
Value::Sequence(v) => v.fmt(f),
|
||||||
Value::Set(ref v) => {
|
Value::Set(v) => {
|
||||||
f.write_str("#")?;
|
f.write_str("#")?;
|
||||||
f.debug_set().entries(v.iter()).finish()
|
f.debug_set().entries(v.iter()).finish()
|
||||||
}
|
}
|
||||||
Value::Dictionary(ref v) => f.debug_map().entries(v.iter()).finish(),
|
Value::Dictionary(v) => f.debug_map().entries(v.iter()).finish(),
|
||||||
Value::Domain(ref d) => write!(f, "#!{:?}", d),
|
Value::Domain(d) => write!(f, "#!{:?}", d),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -292,15 +292,15 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_boolean(&self) -> Option<bool> {
|
pub fn as_boolean(&self) -> Option<bool> {
|
||||||
if let Value::Boolean(b) = *self {
|
if let Value::Boolean(b) = self {
|
||||||
Some(b)
|
Some(*b)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_boolean_mut(&mut self) -> Option<&mut bool> {
|
pub fn as_boolean_mut(&mut self) -> Option<&mut bool> {
|
||||||
if let Value::Boolean(ref mut b) = *self {
|
if let Value::Boolean(b) = self {
|
||||||
Some(b)
|
Some(b)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
@ -315,56 +315,88 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
self.as_float().is_some()
|
self.as_float().is_some()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_float(&self) -> Option<f32> {
|
pub fn as_float(&self) -> Option<&Float> {
|
||||||
if let Value::Float(Float(f)) = *self {
|
if let Value::Float(f) = self {
|
||||||
Some(f)
|
Some(f)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_float_mut(&mut self) -> Option<&mut f32> {
|
pub fn as_float_mut(&mut self) -> Option<&mut Float> {
|
||||||
if let Value::Float(Float(ref mut f)) = *self {
|
if let Value::Float(f) = self {
|
||||||
Some(f)
|
Some(f)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_float(&self) -> Result<f32, Error> {
|
pub fn to_float(&self) -> Result<&Float, Error> {
|
||||||
self.as_float().ok_or_else(|| self.expected(ExpectedKind::Float))
|
self.as_float().ok_or_else(|| self.expected(ExpectedKind::Float))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_f32(&self) -> bool {
|
||||||
|
self.is_float()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn as_f32(&self) -> Option<f32> {
|
||||||
|
self.as_float().map(|f| f.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn as_f32_mut(&mut self) -> Option<&mut f32> {
|
||||||
|
self.as_float_mut().map(|f| &mut f.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_f32(&self) -> Result<f32, Error> {
|
||||||
|
self.to_float().map(|f| f.0)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn is_double(&self) -> bool {
|
pub fn is_double(&self) -> bool {
|
||||||
self.as_double().is_some()
|
self.as_double().is_some()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_double(&self) -> Option<f64> {
|
pub fn as_double(&self) -> Option<&Double> {
|
||||||
if let Value::Double(Double(f)) = *self {
|
if let Value::Double(f) = self {
|
||||||
Some(f)
|
Some(f)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_double_mut(&mut self) -> Option<&mut f64> {
|
pub fn as_double_mut(&mut self) -> Option<&mut Double> {
|
||||||
if let Value::Double(Double(ref mut f)) = *self {
|
if let Value::Double(f) = self {
|
||||||
Some(f)
|
Some(f)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_double(&self) -> Result<f64, Error> {
|
pub fn to_double(&self) -> Result<&Double, Error> {
|
||||||
self.as_double().ok_or_else(|| self.expected(ExpectedKind::Double))
|
self.as_double().ok_or_else(|| self.expected(ExpectedKind::Double))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_f64(&self) -> bool {
|
||||||
|
self.is_double()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn as_f64(&self) -> Option<f64> {
|
||||||
|
self.as_double().map(|f| f.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn as_f64_mut(&mut self) -> Option<&mut f64> {
|
||||||
|
self.as_double_mut().map(|f| &mut f.0)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_f64(&self) -> Result<f64, Error> {
|
||||||
|
self.to_double().map(|f| f.0)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn is_signedinteger(&self) -> bool {
|
pub fn is_signedinteger(&self) -> bool {
|
||||||
self.as_signedinteger().is_some()
|
self.as_signedinteger().is_some()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_signedinteger(&self) -> Option<&SignedInteger> {
|
pub fn as_signedinteger(&self) -> Option<&SignedInteger> {
|
||||||
if let Value::SignedInteger(ref n) = *self {
|
if let Value::SignedInteger(n) = self {
|
||||||
Some(n)
|
Some(n)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
@ -372,7 +404,7 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_signedinteger_mut(&mut self) -> Option<&mut SignedInteger> {
|
pub fn as_signedinteger_mut(&mut self) -> Option<&mut SignedInteger> {
|
||||||
if let Value::SignedInteger(ref mut n) = *self {
|
if let Value::SignedInteger(n) = self {
|
||||||
Some(n)
|
Some(n)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
@ -499,7 +531,7 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_string(&self) -> Option<&String> {
|
pub fn as_string(&self) -> Option<&String> {
|
||||||
if let Value::String(ref s) = *self {
|
if let Value::String(s) = self {
|
||||||
Some(s)
|
Some(s)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
@ -507,7 +539,7 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_string_mut(&mut self) -> Option<&mut String> {
|
pub fn as_string_mut(&mut self) -> Option<&mut String> {
|
||||||
if let Value::String(ref mut s) = *self {
|
if let Value::String(s) = self {
|
||||||
Some(s)
|
Some(s)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
@ -523,7 +555,7 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_bytestring(&self) -> Option<&Vec<u8>> {
|
pub fn as_bytestring(&self) -> Option<&Vec<u8>> {
|
||||||
if let Value::ByteString(ref s) = *self {
|
if let Value::ByteString(s) = self {
|
||||||
Some(s)
|
Some(s)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
@ -531,7 +563,7 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_bytestring_mut(&mut self) -> Option<&mut Vec<u8>> {
|
pub fn as_bytestring_mut(&mut self) -> Option<&mut Vec<u8>> {
|
||||||
if let Value::ByteString(ref mut s) = *self {
|
if let Value::ByteString(s) = self {
|
||||||
Some(s)
|
Some(s)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
@ -551,7 +583,7 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_symbol(&self) -> Option<&String> {
|
pub fn as_symbol(&self) -> Option<&String> {
|
||||||
if let Value::Symbol(ref s) = *self {
|
if let Value::Symbol(s) = self {
|
||||||
Some(s)
|
Some(s)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
@ -559,7 +591,7 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_symbol_mut(&mut self) -> Option<&mut String> {
|
pub fn as_symbol_mut(&mut self) -> Option<&mut String> {
|
||||||
if let Value::Symbol(ref mut s) = *self {
|
if let Value::Symbol(s) = self {
|
||||||
Some(s)
|
Some(s)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
@ -581,7 +613,7 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_record(&self, arity: Option<usize>) -> Option<&Record<N>> {
|
pub fn as_record(&self, arity: Option<usize>) -> Option<&Record<N>> {
|
||||||
if let Value::Record(ref r) = *self {
|
if let Value::Record(r) = self {
|
||||||
match arity {
|
match arity {
|
||||||
Some(expected) if r.arity() == expected => Some(r),
|
Some(expected) if r.arity() == expected => Some(r),
|
||||||
Some(_other) => None,
|
Some(_other) => None,
|
||||||
|
@ -593,7 +625,7 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_record_mut(&mut self, arity: Option<usize>) -> Option<&mut Record<N>> {
|
pub fn as_record_mut(&mut self, arity: Option<usize>) -> Option<&mut Record<N>> {
|
||||||
if let Value::Record(ref mut r) = *self {
|
if let Value::Record(r) = self {
|
||||||
match arity {
|
match arity {
|
||||||
Some(expected) if r.arity() == expected => Some(r),
|
Some(expected) if r.arity() == expected => Some(r),
|
||||||
Some(_other) => None,
|
Some(_other) => None,
|
||||||
|
@ -629,7 +661,7 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
pub fn as_simple_record(&self, label: &str, arity: Option<usize>) -> Option<&[N]> {
|
pub fn as_simple_record(&self, label: &str, arity: Option<usize>) -> Option<&[N]> {
|
||||||
self.as_record(arity).and_then(|r| {
|
self.as_record(arity).and_then(|r| {
|
||||||
match r.label().value() {
|
match r.label().value() {
|
||||||
Value::Symbol(ref s) if s == label => Some(r.fields()),
|
Value::Symbol(s) if s == label => Some(r.fields()),
|
||||||
_ => None
|
_ => None
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -657,7 +689,7 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_sequence(&self) -> Option<&Vec<N>> {
|
pub fn as_sequence(&self) -> Option<&Vec<N>> {
|
||||||
if let Value::Sequence(ref s) = *self {
|
if let Value::Sequence(s) = self {
|
||||||
Some(s)
|
Some(s)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
@ -665,7 +697,7 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_sequence_mut(&mut self) -> Option<&mut Vec<N>> {
|
pub fn as_sequence_mut(&mut self) -> Option<&mut Vec<N>> {
|
||||||
if let Value::Sequence(ref mut s) = *self {
|
if let Value::Sequence(s) = self {
|
||||||
Some(s)
|
Some(s)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
@ -681,7 +713,7 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_set(&self) -> Option<&Set<N>> {
|
pub fn as_set(&self) -> Option<&Set<N>> {
|
||||||
if let Value::Set(ref s) = *self {
|
if let Value::Set(s) = self {
|
||||||
Some(s)
|
Some(s)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
@ -689,7 +721,7 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_set_mut(&mut self) -> Option<&mut Set<N>> {
|
pub fn as_set_mut(&mut self) -> Option<&mut Set<N>> {
|
||||||
if let Value::Set(ref mut s) = *self {
|
if let Value::Set(s) = self {
|
||||||
Some(s)
|
Some(s)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
@ -705,7 +737,7 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_dictionary(&self) -> Option<&Map<N, N>> {
|
pub fn as_dictionary(&self) -> Option<&Map<N, N>> {
|
||||||
if let Value::Dictionary(ref s) = *self {
|
if let Value::Dictionary(s) = self {
|
||||||
Some(s)
|
Some(s)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
@ -713,7 +745,7 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_dictionary_mut(&mut self) -> Option<&mut Map<N, N>> {
|
pub fn as_dictionary_mut(&mut self) -> Option<&mut Map<N, N>> {
|
||||||
if let Value::Dictionary(ref mut s) = *self {
|
if let Value::Dictionary(s) = self {
|
||||||
Some(s)
|
Some(s)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
@ -729,7 +761,7 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_embedded(&self) -> Option<&D> {
|
pub fn as_embedded(&self) -> Option<&D> {
|
||||||
if let Value::Domain(ref d) = *self {
|
if let Value::Domain(d) = self {
|
||||||
Some(d)
|
Some(d)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
@ -748,18 +780,18 @@ impl<N: NestedValue<D>, D: Domain> Value<N, D> {
|
||||||
Value::Boolean(b) => Value::Boolean(*b),
|
Value::Boolean(b) => Value::Boolean(*b),
|
||||||
Value::Float(f) => Value::Float(f.clone()),
|
Value::Float(f) => Value::Float(f.clone()),
|
||||||
Value::Double(d) => Value::Double(d.clone()),
|
Value::Double(d) => Value::Double(d.clone()),
|
||||||
Value::SignedInteger(ref n) => Value::SignedInteger(n.clone()),
|
Value::SignedInteger(n) => Value::SignedInteger(n.clone()),
|
||||||
Value::String(ref s) => Value::String(s.clone()),
|
Value::String(s) => Value::String(s.clone()),
|
||||||
Value::ByteString(ref v) => Value::ByteString(v.clone()),
|
Value::ByteString(v) => Value::ByteString(v.clone()),
|
||||||
Value::Symbol(ref v) => Value::Symbol(v.clone()),
|
Value::Symbol(v) => Value::Symbol(v.clone()),
|
||||||
Value::Record(ref r) =>
|
Value::Record(r) =>
|
||||||
Value::Record(Record(r.fields_vec().iter().map(|a| a.copy_via(f))
|
Value::Record(Record(r.fields_vec().iter().map(|a| a.copy_via(f))
|
||||||
.collect::<Result<Vec<_>, _>>()?)),
|
.collect::<Result<Vec<_>, _>>()?)),
|
||||||
Value::Sequence(ref v) => Value::Sequence(v.iter().map(|a| a.copy_via(f))
|
Value::Sequence(v) => Value::Sequence(v.iter().map(|a| a.copy_via(f))
|
||||||
.collect::<IOResult<Vec<_>>>()?),
|
.collect::<IOResult<Vec<_>>>()?),
|
||||||
Value::Set(ref v) => Value::Set(v.iter().map(|a| a.copy_via(f))
|
Value::Set(v) => Value::Set(v.iter().map(|a| a.copy_via(f))
|
||||||
.collect::<IOResult<Set<_>>>()?),
|
.collect::<IOResult<Set<_>>>()?),
|
||||||
Value::Dictionary(ref v) =>
|
Value::Dictionary(v) =>
|
||||||
Value::Dictionary(v.iter().map(|(a,b)| Ok((a.copy_via(f)?, b.copy_via(f)?)))
|
Value::Dictionary(v.iter().map(|(a,b)| Ok((a.copy_via(f)?, b.copy_via(f)?)))
|
||||||
.collect::<IOResult<Map<_, _>>>()?),
|
.collect::<IOResult<Map<_, _>>>()?),
|
||||||
Value::Domain(d) => f(d)?,
|
Value::Domain(d) => f(d)?,
|
||||||
|
@ -833,9 +865,9 @@ impl<N: NestedValue<D>, D: Domain> Annotations<N, D> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn maybe_slice(&self) -> Option<&[N]> {
|
pub fn maybe_slice(&self) -> Option<&[N]> {
|
||||||
match self.0 {
|
match &self.0 {
|
||||||
None => None,
|
None => None,
|
||||||
Some(ref b) => Some(&&b[..]),
|
Some(b) => Some(&b[..]),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -857,10 +889,10 @@ impl<N: NestedValue<D>, D: Domain> Annotations<N, D> {
|
||||||
where
|
where
|
||||||
F: Fn(&D) -> IOResult<Value<M, E>>
|
F: Fn(&D) -> IOResult<Value<M, E>>
|
||||||
{
|
{
|
||||||
Ok(match self.0 {
|
Ok(match &self.0 {
|
||||||
None =>
|
None =>
|
||||||
Annotations(None, PhantomData),
|
Annotations(None, PhantomData),
|
||||||
Some(ref b) =>
|
Some(b) =>
|
||||||
Annotations(Some(b.iter().map(|a| a.copy_via(f)).collect::<Result<Vec<_>, _>>()?),
|
Annotations(Some(b.iter().map(|a| a.copy_via(f)).collect::<Result<Vec<_>, _>>()?),
|
||||||
PhantomData),
|
PhantomData),
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue