Skip to main content

Module serde

Module serde 

Source
Expand description

How a time crosses to a config file, a log or a peer.

Every value serializes as the string its Display writes, where a person reads the format: RFC 3339 for an instant, "1m30s" for a span, "24 ticks" for a count of ticks. A format no person reads, such as bincode or postcard, takes each as its count. A peer that sends a count in a format a person reads names the unit at the field, and the counted type picks the module.

Each unit has an option module for a field that may be absent. Every module reads a count as a number or a decimal string, refusing one past what the type holds, and writes a number, but for nanoseconds since the epoch: those pass what an f64 holds exactly, so a JSON reader backed by one would round them, and they are written as a string.

§Examples

use serde::{Deserialize, Serialize};
use serde_json::from_str;
use t2t_core::serde::{timedelta, timestamp};
use t2t_core::{Timedelta, Timestamp};

#[derive(Serialize, Deserialize)]
struct Listing {
    #[serde(with = "timestamp::secs")]
    expiry: Timestamp,
    #[serde(with = "timedelta::millis")]
    poll: Timedelta,
}

let listing: Listing = from_str(r#"{"expiry":1700000300,"poll":250}"#)?;
assert_eq!(listing.expiry, Timestamp::from_secs(1_700_000_300), "whole seconds");
assert_eq!(listing.poll, Timedelta::from_millis(250), "whole milliseconds");

Modules§

timedelta
A Timedelta as a count, in the unit the module names.
timestamp
A Timestamp as a count since the Unix epoch, in the unit the module names.