Skip to content

Commit 69bf2b7

Browse files
committed
bincode support
1 parent 91a679e commit 69bf2b7

3 files changed

Lines changed: 99 additions & 1 deletion

File tree

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ features = ["full"]
1616
features = ["full"]
1717

1818
[dependencies]
19+
bincode = { version = "2.0.1", optional = true }
1920
chrono = { version = "0.4", optional = true }
2021
dateparser = { version = "0.2", optional = true }
2122
serde = { version = "1.0", features = ["derive"], optional = true }
@@ -32,5 +33,7 @@ web-time = "1.1"
3233
[features]
3334
#default = [ "full" ]
3435
chrono = ["dep:chrono", "dateparser"]
35-
full = [ "chrono", "serde", "sqlx" ]
36+
full = [ "chrono", "serde", "bincode", "sqlx" ]
3637
as-float-secs = []
38+
serde = ["dep:serde"]
39+
bincode = ["dep:bincode"]

src/impl_bincode.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
use bincode::{Decode, Encode};
2+
3+
use crate::{Monotonic, Timestamp};
4+
5+
//
6+
// Timestamp
7+
//
8+
#[cfg(not(feature = "as-float-secs"))]
9+
impl Encode for Timestamp {
10+
fn encode<E: bincode::enc::Encoder>(
11+
&self,
12+
encoder: &mut E,
13+
) -> Result<(), bincode::error::EncodeError> {
14+
let nanos: u64 = self.as_nanos().try_into().unwrap();
15+
nanos.encode(encoder)
16+
}
17+
}
18+
19+
#[cfg(feature = "as-float-secs")]
20+
impl Encode for Timestamp {
21+
fn encode<E: bincode::enc::Encoder>(
22+
&self,
23+
encoder: &mut E,
24+
) -> Result<(), bincode::error::EncodeError> {
25+
let secs = self.as_secs_f64();
26+
secs.encode(encoder)
27+
}
28+
}
29+
30+
#[cfg(not(feature = "as-float-secs"))]
31+
impl<C> Decode<C> for Timestamp {
32+
fn decode<D: bincode::de::Decoder<Context = C>>(
33+
decoder: &mut D,
34+
) -> Result<Self, bincode::error::DecodeError> {
35+
let nanos = u64::decode(decoder)?;
36+
Ok(nanos.into())
37+
}
38+
}
39+
40+
#[cfg(feature = "as-float-secs")]
41+
impl<C> Decode<C> for Timestamp {
42+
fn decode<D: bincode::de::Decoder<Context = C>>(
43+
decoder: &mut D,
44+
) -> Result<Self, bincode::error::DecodeError> {
45+
let secs = f64::decode(decoder)?;
46+
Ok(Timestamp::from_secs_f64(secs))
47+
}
48+
}
49+
50+
//
51+
// Monotonic
52+
//
53+
impl Encode for Monotonic {
54+
fn encode<E: bincode::enc::Encoder>(
55+
&self,
56+
encoder: &mut E,
57+
) -> Result<(), bincode::error::EncodeError> {
58+
let nanos: u64 = self.as_nanos().try_into().unwrap();
59+
nanos.encode(encoder)
60+
}
61+
}
62+
63+
impl<C> Decode<C> for Monotonic {
64+
fn decode<D: bincode::de::Decoder<Context = C>>(
65+
decoder: &mut D,
66+
) -> Result<Self, bincode::error::DecodeError> {
67+
let nanos = u64::decode(decoder)?;
68+
Ok(nanos.into())
69+
}
70+
}
71+
72+
#[cfg(test)]
73+
mod tests {
74+
use super::{Monotonic, Timestamp};
75+
76+
#[test]
77+
fn test_timestamp_bincode() {
78+
let ts = Timestamp::from_nanos(1_500_000_000);
79+
let encoded = bincode::encode_to_vec(ts, bincode::config::standard()).unwrap();
80+
let (decoded, _) =
81+
bincode::decode_from_slice(&encoded, bincode::config::standard()).unwrap();
82+
assert_eq!(ts, decoded);
83+
}
84+
85+
#[test]
86+
fn test_monotonic_bincode() {
87+
let mono = Monotonic::from_nanos(2_500_000_000);
88+
let encoded = bincode::encode_to_vec(mono, bincode::config::standard()).unwrap();
89+
let (decoded, _) =
90+
bincode::decode_from_slice(&encoded, bincode::config::standard()).unwrap();
91+
assert_eq!(mono, decoded);
92+
}
93+
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ pub use timestamp::Timestamp;
66
mod common;
77
mod convert;
88
mod error;
9+
#[cfg(feature = "bincode")]
10+
mod impl_bincode;
911
#[cfg(feature = "chrono")]
1012
mod impl_chrono;
1113
#[cfg(feature = "serde")]

0 commit comments

Comments
 (0)