Skip to content

Commit

Permalink
Fixed a bug in CuTime serialization support
Browse files Browse the repository at this point in the history
  • Loading branch information
gbin committed Jun 27, 2024
1 parent e0a772a commit e05dc4f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion copper_value/src/benc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ impl Encode for Value {
Value::Seq(v) => v.to_owned().encode(encoder),
Value::Map(v) => v.to_owned().encode(encoder),
Value::Option(v) => v.to_owned().encode(encoder),
Value::Newtype(v) => v.to_owned().encode(encoder),
Value::CuTime(v) => v.to_owned().encode(encoder),
Value::Newtype(v) => v.to_owned().encode(encoder),
}
}
}
5 changes: 1 addition & 4 deletions copper_value/src/de.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use crate::Value;
use serde::{de, forward_to_deserialize_any};
use std::collections::BTreeMap;
use std::error::Error;
use std::fmt;
use std::marker::PhantomData;

use crate::Value;

#[derive(Debug)]
pub enum Unexpected {
Bool(bool),
Expand Down Expand Up @@ -283,7 +282,6 @@ impl<'de> de::Visitor<'de> for ValueVisitor {
}

fn visit_newtype_struct<D: de::Deserializer<'de>>(self, d: D) -> Result<Value, D::Error> {
println!("visit_newtype_struct");
d.deserialize_any(ValueVisitor)
.map(|v| Value::Newtype(Box::new(v)))
}
Expand Down Expand Up @@ -315,7 +313,6 @@ impl<'de> de::Visitor<'de> for ValueVisitor {

impl<'de> de::Deserialize<'de> for Value {
fn deserialize<D: de::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
println!("Value::deserialize");
d.deserialize_any(ValueVisitor)
}
}
Expand Down
17 changes: 17 additions & 0 deletions copper_value/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ impl PartialOrd for Value {
#[cfg(test)]
mod tests {
use super::*;
use bincode::config::standard;
use copper_clock::RobotClock;
use serde_derive::{Deserialize, Serialize};
use std::time::Duration;
Expand Down Expand Up @@ -507,4 +508,20 @@ mod tests {
let foo = CuTime::deserialize(input).unwrap();
assert_eq!(foo, CuTime::from(Duration::from_nanos(42)));
}
#[test]
fn cutime_value_encode_decode() {
let c = Value::CuTime(CuTime::from(Duration::from_nanos(42)));
let v = bincode::encode_to_vec(&c, standard()).expect("encode failed");
let (v2, s) = bincode::decode_from_slice::<Value, _>(v.as_slice(), standard())
.expect("decode failed");
assert_eq!(s, v.len());
assert_eq!(&v2, &c);
}

#[test]
fn test_cutime_tovalue() {
let c = CuTime::from(Duration::from_nanos(42));
let v = to_value(&c).expect("to_value failed");
assert_eq!(v, Value::CuTime(c));
}
}
15 changes: 12 additions & 3 deletions copper_value/src/ser.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::Value;
use copper_clock::CuTime;
use serde::ser;
use std::collections::BTreeMap;
use std::error::Error;
use std::fmt;

use crate::Value;

#[derive(Debug)]
pub enum SerializerError {
Custom(String),
Expand Down Expand Up @@ -175,12 +175,21 @@ impl ser::Serializer for Serializer {

fn serialize_newtype_struct<T: ?Sized>(
self,
_name: &'static str,
name: &'static str,
value: &T,
) -> Result<Self::Ok, Self::Error>
where
T: ser::Serialize,
{
if name == "CuDuration" || name == "CuTime" {
return value.serialize(Serializer).map(|v| {
if let Value::U64(v) = v {
Value::CuTime(CuTime::from(v))
} else {
v
}
});
}
value
.serialize(Serializer)
.map(|v| Value::Newtype(Box::new(v)))
Expand Down

0 comments on commit e05dc4f

Please sign in to comment.