From fc8709706c3979b62a576ceb4d3d8b0791c62a16 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Wed, 30 Jun 2021 10:10:38 +0200 Subject: [PATCH] Cleanups; make as_float etc return Float, add as_f32 etc --- implementations/rust/preserves/src/de.rs | 4 +- implementations/rust/preserves/src/lib.rs | 12 +- .../rust/preserves/src/value/de.rs | 8 +- .../rust/preserves/src/value/mod.rs | 2 + .../rust/preserves/src/value/packed/reader.rs | 4 +- .../rust/preserves/src/value/reader.rs | 4 +- .../rust/preserves/src/value/repr.rs | 134 +++++++++++------- 7 files changed, 101 insertions(+), 67 deletions(-) diff --git a/implementations/rust/preserves/src/de.rs b/implementations/rust/preserves/src/de.rs index 581e172..10f450a 100644 --- a/implementations/rust/preserves/src/de.rs +++ b/implementations/rust/preserves/src/de.rs @@ -103,12 +103,12 @@ impl<'r, 'de, 'a, R: Reader<'de>> serde::de::Deserializer<'de> for &'a mut Deser fn deserialize_f32(self, visitor: V) -> Result where V: Visitor<'de> { - visitor.visit_f32(self.read.next_float()?) + visitor.visit_f32(self.read.next_f32()?) } fn deserialize_f64(self, visitor: V) -> Result where V: Visitor<'de> { - visitor.visit_f64(self.read.next_double()?) + visitor.visit_f64(self.read.next_f64()?) } fn deserialize_char(self, visitor: V) -> Result where V: Visitor<'de> diff --git a/implementations/rust/preserves/src/lib.rs b/implementations/rust/preserves/src/lib.rs index c92a90a..54338eb 100644 --- a/implementations/rust/preserves/src/lib.rs +++ b/implementations/rust/preserves/src/lib.rs @@ -164,18 +164,18 @@ mod value_tests { #[test] fn float_mut() { let mut f = VV::from(1.0f32); - assert!(f.is_float()); - *(f.as_float_mut().unwrap()) = 123.45; + assert!(f.is_f32()); + *(f.as_f32_mut().unwrap()) = 123.45; 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() { let mut f = VV::from(1.0); - assert!(f.is_double()); - *(f.as_double_mut().unwrap()) = 123.45; + assert!(f.is_f64()); + *(f.as_f64_mut().unwrap()) = 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() { diff --git a/implementations/rust/preserves/src/value/de.rs b/implementations/rust/preserves/src/value/de.rs index a9c6c62..3857701 100644 --- a/implementations/rust/preserves/src/value/de.rs +++ b/implementations/rust/preserves/src/value/de.rs @@ -118,17 +118,17 @@ impl<'de, 'a> serde::de::Deserializer<'de> for &'a mut Deserializer<'de> fn deserialize_f32(self, visitor: V) -> Result where V: Visitor<'de> { - match self.input.value().as_double() { + match self.input.value().as_f64() { 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(self, visitor: V) -> Result where V: Visitor<'de> { - match self.input.value().as_float() { + match self.input.value().as_f32() { 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()?), } } diff --git a/implementations/rust/preserves/src/value/mod.rs b/implementations/rust/preserves/src/value/mod.rs index 1785ca6..c612d18 100644 --- a/implementations/rust/preserves/src/value/mod.rs +++ b/implementations/rust/preserves/src/value/mod.rs @@ -16,6 +16,8 @@ pub use reader::Reader; pub use repr::AnnotatedValue; pub use repr::ArcValue; pub use repr::Domain; +pub use repr::Double; +pub use repr::Float; pub use repr::IOValue; pub use repr::Map; pub use repr::NestedValue; diff --git a/implementations/rust/preserves/src/value/packed/reader.rs b/implementations/rust/preserves/src/value/packed/reader.rs index d922d3f..ef0c75f 100644 --- a/implementations/rust/preserves/src/value/packed/reader.rs +++ b/implementations/rust/preserves/src/value/packed/reader.rs @@ -401,7 +401,7 @@ impl<'de, S: BinarySource<'de>> Reader<'de> for PackedReader<'de, S> { fn next_u64(&mut self) -> ReaderResult { self.next_unsigned(|n| n.to_u64()) } fn next_u128(&mut self) -> ReaderResult { self.next_unsigned(|n| n.to_u128()) } - fn next_float(&mut self) -> ReaderResult { + fn next_f32(&mut self) -> ReaderResult { match self.peek_next_nonannotation_tag()? { Tag::Float => { self.skip()?; @@ -419,7 +419,7 @@ impl<'de, S: BinarySource<'de>> Reader<'de> for PackedReader<'de, S> { } } - fn next_double(&mut self) -> ReaderResult { + fn next_f64(&mut self) -> ReaderResult { match self.peek_next_nonannotation_tag()? { Tag::Float => { self.skip()?; diff --git a/implementations/rust/preserves/src/value/reader.rs b/implementations/rust/preserves/src/value/reader.rs index 66ac1fe..b8114da 100644 --- a/implementations/rust/preserves/src/value/reader.rs +++ b/implementations/rust/preserves/src/value/reader.rs @@ -41,8 +41,8 @@ pub trait Reader<'de> { fn next_u64(&mut self) -> ReaderResult { self.demand_next(false)?.value().to_u64() } fn next_i128(&mut self) -> ReaderResult { self.demand_next(false)?.value().to_i128() } fn next_u128(&mut self) -> ReaderResult { self.demand_next(false)?.value().to_u128() } - fn next_float(&mut self) -> ReaderResult { self.demand_next(false)?.value().to_float() } - fn next_double(&mut self) -> ReaderResult { self.demand_next(false)?.value().to_double() } + fn next_f32(&mut self) -> ReaderResult { self.demand_next(false)?.value().to_f32() } + fn next_f64(&mut self) -> ReaderResult { self.demand_next(false)?.value().to_f64() } fn next_char(&mut self) -> ReaderResult { self.demand_next(false)?.value().to_char() } fn next_str(&mut self) -> ReaderResult> { diff --git a/implementations/rust/preserves/src/value/repr.rs b/implementations/rust/preserves/src/value/repr.rs index 1cde6fc..b37f5df 100644 --- a/implementations/rust/preserves/src/value/repr.rs +++ b/implementations/rust/preserves/src/value/repr.rs @@ -242,13 +242,13 @@ impl, D: Domain> Debug for Value { 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) => { + Value::String(v) => write!(f, "{:?}", v), // TODO: proper escaping! + Value::ByteString(v) => { f.write_str("#x\"")?; for b in v { write!(f, "{:02x}", b)? } f.write_str("\"") } - Value::Symbol(ref v) => { + Value::Symbol(v) => { // TODO: proper escaping! if v.is_empty() { f.write_str("||") @@ -256,7 +256,7 @@ impl, D: Domain> Debug for Value { write!(f, "{}", v) } } - Value::Record(ref r) => { + Value::Record(r) => { f.write_str("<")?; r.label().fmt(f)?; for v in r.fields() { @@ -265,13 +265,13 @@ impl, D: Domain> Debug for Value { } f.write_str(">") } - Value::Sequence(ref v) => v.fmt(f), - Value::Set(ref v) => { + Value::Sequence(v) => v.fmt(f), + Value::Set(v) => { f.write_str("#")?; f.debug_set().entries(v.iter()).finish() } - Value::Dictionary(ref v) => f.debug_map().entries(v.iter()).finish(), - Value::Domain(ref d) => write!(f, "#!{:?}", d), + Value::Dictionary(v) => f.debug_map().entries(v.iter()).finish(), + Value::Domain(d) => write!(f, "#!{:?}", d), } } } @@ -292,15 +292,15 @@ impl, D: Domain> Value { } pub fn as_boolean(&self) -> Option { - if let Value::Boolean(b) = *self { - Some(b) + 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 { + if let Value::Boolean(b) = self { Some(b) } else { None @@ -315,56 +315,88 @@ impl, D: Domain> Value { self.as_float().is_some() } - pub fn as_float(&self) -> Option { - if let Value::Float(Float(f)) = *self { + pub fn as_float(&self) -> Option<&Float> { + if let Value::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 { + pub fn as_float_mut(&mut self) -> Option<&mut Float> { + if let Value::Float(f) = self { Some(f) } else { None } } - pub fn to_float(&self) -> Result { + pub fn to_float(&self) -> Result<&Float, Error> { 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 { + 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 { + self.to_float().map(|f| f.0) + } + pub fn is_double(&self) -> bool { self.as_double().is_some() } - pub fn as_double(&self) -> Option { - if let Value::Double(Double(f)) = *self { + pub fn as_double(&self) -> Option<&Double> { + if let Value::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 { + pub fn as_double_mut(&mut self) -> Option<&mut Double> { + if let Value::Double(f) = self { Some(f) } else { None } } - pub fn to_double(&self) -> Result { + pub fn to_double(&self) -> Result<&Double, Error> { 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 { + 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 { + self.to_double().map(|f| f.0) + } + pub fn is_signedinteger(&self) -> bool { self.as_signedinteger().is_some() } pub fn as_signedinteger(&self) -> Option<&SignedInteger> { - if let Value::SignedInteger(ref n) = *self { + if let Value::SignedInteger(n) = self { Some(n) } else { None @@ -372,7 +404,7 @@ impl, D: Domain> Value { } 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) } else { None @@ -499,7 +531,7 @@ impl, D: Domain> Value { } pub fn as_string(&self) -> Option<&String> { - if let Value::String(ref s) = *self { + if let Value::String(s) = self { Some(s) } else { None @@ -507,7 +539,7 @@ impl, D: Domain> Value { } 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) } else { None @@ -523,7 +555,7 @@ impl, D: Domain> Value { } pub fn as_bytestring(&self) -> Option<&Vec> { - if let Value::ByteString(ref s) = *self { + if let Value::ByteString(s) = self { Some(s) } else { None @@ -531,7 +563,7 @@ impl, D: Domain> Value { } pub fn as_bytestring_mut(&mut self) -> Option<&mut Vec> { - if let Value::ByteString(ref mut s) = *self { + if let Value::ByteString(s) = self { Some(s) } else { None @@ -551,7 +583,7 @@ impl, D: Domain> Value { } pub fn as_symbol(&self) -> Option<&String> { - if let Value::Symbol(ref s) = *self { + if let Value::Symbol(s) = self { Some(s) } else { None @@ -559,7 +591,7 @@ impl, D: Domain> Value { } 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) } else { None @@ -581,7 +613,7 @@ impl, D: Domain> Value { } pub fn as_record(&self, arity: Option) -> Option<&Record> { - if let Value::Record(ref r) = *self { + if let Value::Record(r) = self { match arity { Some(expected) if r.arity() == expected => Some(r), Some(_other) => None, @@ -593,7 +625,7 @@ impl, D: Domain> Value { } pub fn as_record_mut(&mut self, arity: Option) -> Option<&mut Record> { - if let Value::Record(ref mut r) = *self { + if let Value::Record(r) = self { match arity { Some(expected) if r.arity() == expected => Some(r), Some(_other) => None, @@ -629,7 +661,7 @@ impl, D: Domain> Value { pub fn as_simple_record(&self, label: &str, arity: Option) -> Option<&[N]> { self.as_record(arity).and_then(|r| { match r.label().value() { - Value::Symbol(ref s) if s == label => Some(r.fields()), + Value::Symbol(s) if s == label => Some(r.fields()), _ => None } }) @@ -657,7 +689,7 @@ impl, D: Domain> Value { } pub fn as_sequence(&self) -> Option<&Vec> { - if let Value::Sequence(ref s) = *self { + if let Value::Sequence(s) = self { Some(s) } else { None @@ -665,7 +697,7 @@ impl, D: Domain> Value { } pub fn as_sequence_mut(&mut self) -> Option<&mut Vec> { - if let Value::Sequence(ref mut s) = *self { + if let Value::Sequence(s) = self { Some(s) } else { None @@ -681,7 +713,7 @@ impl, D: Domain> Value { } pub fn as_set(&self) -> Option<&Set> { - if let Value::Set(ref s) = *self { + if let Value::Set(s) = self { Some(s) } else { None @@ -689,7 +721,7 @@ impl, D: Domain> Value { } pub fn as_set_mut(&mut self) -> Option<&mut Set> { - if let Value::Set(ref mut s) = *self { + if let Value::Set(s) = self { Some(s) } else { None @@ -705,7 +737,7 @@ impl, D: Domain> Value { } pub fn as_dictionary(&self) -> Option<&Map> { - if let Value::Dictionary(ref s) = *self { + if let Value::Dictionary(s) = self { Some(s) } else { None @@ -713,7 +745,7 @@ impl, D: Domain> Value { } pub fn as_dictionary_mut(&mut self) -> Option<&mut Map> { - if let Value::Dictionary(ref mut s) = *self { + if let Value::Dictionary(s) = self { Some(s) } else { None @@ -729,7 +761,7 @@ impl, D: Domain> Value { } pub fn as_embedded(&self) -> Option<&D> { - if let Value::Domain(ref d) = *self { + if let Value::Domain(d) = self { Some(d) } else { None @@ -748,18 +780,18 @@ impl, D: Domain> Value { Value::Boolean(b) => Value::Boolean(*b), Value::Float(f) => Value::Float(f.clone()), Value::Double(d) => Value::Double(d.clone()), - Value::SignedInteger(ref n) => Value::SignedInteger(n.clone()), - Value::String(ref s) => Value::String(s.clone()), - Value::ByteString(ref v) => Value::ByteString(v.clone()), - Value::Symbol(ref v) => Value::Symbol(v.clone()), - Value::Record(ref r) => + Value::SignedInteger(n) => Value::SignedInteger(n.clone()), + Value::String(s) => Value::String(s.clone()), + Value::ByteString(v) => Value::ByteString(v.clone()), + Value::Symbol(v) => Value::Symbol(v.clone()), + Value::Record(r) => Value::Record(Record(r.fields_vec().iter().map(|a| a.copy_via(f)) .collect::, _>>()?)), - 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::>>()?), - 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::>>()?), - Value::Dictionary(ref v) => + Value::Dictionary(v) => Value::Dictionary(v.iter().map(|(a,b)| Ok((a.copy_via(f)?, b.copy_via(f)?))) .collect::>>()?), Value::Domain(d) => f(d)?, @@ -833,9 +865,9 @@ impl, D: Domain> Annotations { } pub fn maybe_slice(&self) -> Option<&[N]> { - match self.0 { + match &self.0 { None => None, - Some(ref b) => Some(&&b[..]), + Some(b) => Some(&b[..]), } } @@ -857,10 +889,10 @@ impl, D: Domain> Annotations { where F: Fn(&D) -> IOResult> { - Ok(match self.0 { + Ok(match &self.0 { None => Annotations(None, PhantomData), - Some(ref b) => + Some(b) => Annotations(Some(b.iter().map(|a| a.copy_via(f)).collect::, _>>()?), PhantomData), })