pub struct Seconds<F = i64>(/* private fields */);Expand description
A duration or timestamp as number of seconds.
Durations are written as their length and timestamps as the time
since the Unix epoch (1970-01-01T00:00:00Z). F is how the
number is written:
i64(the default): an integer. The fraction of a second is truncated, towards the past for timestamps and towards zero for durations (like the seconds of the types themselves).f64: a float with the fraction of a second (which loses precision for large values).String: an integer as string, truncated likei64.
When deserializing, all of these are accepted independent of F:
integers, floats and strings that contain a number (also with a
fraction or an exponent, like 1.5 or 2e3), as well as the value
in the form it serializes as without the adapter. Fractions are
rounded to the nearest nanosecond, floats are read as the shortest
decimal number that is the same float (0.1 seconds are exactly 100
milliseconds).
The supported types are listed in TimeValue. Millis,
Micros and Nanos are the same for smaller units.
use std::time::{Duration, SystemTime};
use deser::adapters::{Millis, Seconds};
use deser::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct Session {
#[deser(as = Seconds)]
created: SystemTime,
#[deser(as = Seconds<f64>)]
timeout: Duration,
#[deser(as = Millis)]
latency: Duration,
}
let input = r#"{"created":1700000000,"timeout":1.5,"latency":250}"#;
let session: Session = deser_json::from_str(input).unwrap();
assert_eq!(session.timeout, Duration::from_millis(1500));
assert_eq!(session.latency, Duration::from_millis(250));
assert_eq!(deser_json::to_string(&session).unwrap(), input);Trait Implementations§
Source§impl<'de, T: TimeValue, F: UnitFormat> Deserialize<'de, T> for Seconds<F>
impl<'de, T: TimeValue, F: UnitFormat> Deserialize<'de, T> for Seconds<F>
Source§fn deserialize_atom(
slot: &mut Slot<T, Self>,
atom: Atom<'_>,
state: &mut State,
) -> Result<(), Error>
fn deserialize_atom( slot: &mut Slot<T, Self>, atom: Atom<'_>, state: &mut State, ) -> Result<(), Error>
Deserializes an atom into the slot. Read more
Source§fn expecting() -> Cow<'static, str>
fn expecting() -> Cow<'static, str>
Returns what the value expects, for error messages. Read more
Source§fn deserialize_into<'out>(
out: &'out mut Option<T>,
state: &mut State,
) -> SinkHandle<'out, 'de>
fn deserialize_into<'out>( out: &'out mut Option<T>, state: &mut State, ) -> SinkHandle<'out, 'de>
Creates a sink that deserializes the value into the given slot. Read more
Source§fn deserialize_borrowed_atom(
slot: &mut Slot<T, Self>,
atom: Atom<'de>,
state: &mut State,
) -> Result<(), Error>
fn deserialize_borrowed_atom( slot: &mut Slot<T, Self>, atom: Atom<'de>, state: &mut State, ) -> Result<(), Error>
Deserializes an atom that borrows from the data being deserialized
into the slot. Read more
Source§fn describe_type(d: &mut dyn Describe)
fn describe_type(d: &mut dyn Describe)
Describes the Rust shape of the type. Read more
Source§fn initial_value() -> Option<T>
fn initial_value() -> Option<T>
Provides the value of a missing struct field. Read more
Source§fn deserialize_update<'out>(
value: &'out mut T,
state: &mut State,
) -> SinkHandle<'out, 'de>
fn deserialize_update<'out>( value: &'out mut T, state: &mut State, ) -> SinkHandle<'out, 'de>
Creates a sink that updates an existing value. Read more
Source§impl<T: TimeValue, F: UnitFormat> Serialize<T> for Seconds<F>
impl<T: TimeValue, F: UnitFormat> Serialize<T> for Seconds<F>
Source§fn serialize<'a>(value: &'a T, _state: &mut State) -> Result<Emit<'a>, Error>
fn serialize<'a>(value: &'a T, _state: &mut State) -> Result<Emit<'a>, Error>
Serializes the value.
Source§fn finish(value: &T, state: &mut State) -> Result<(), Error>
fn finish(value: &T, state: &mut State) -> Result<(), Error>
Invoked after the serialization finished. Read more
Source§fn container_shape(value: &T) -> ContainerShape
fn container_shape(value: &T) -> ContainerShape
Returns the shape of the value if it’s a map or sequence. Read more
Auto Trait Implementations§
impl<F> Freeze for Seconds<F>
impl<F> RefUnwindSafe for Seconds<F>
impl<F> Send for Seconds<F>
impl<F> Sync for Seconds<F>
impl<F> Unpin for Seconds<F>
impl<F> UnsafeUnpin for Seconds<F>
impl<F> UnwindSafe for Seconds<F>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more