From acbd3856490d243e66ef30f2d125848742abbc62 Mon Sep 17 00:00:00 2001 From: Victor Koenders Date: Sat, 16 Oct 2021 13:32:12 +0200 Subject: [PATCH] Added core::time::Duration and std::time::SystemTime --- src/de/impls.rs | 17 ++++++++++++++--- src/enc/impls.rs | 13 ++++++++++++- src/error.rs | 9 +++++++++ src/features/impl_std.rs | 21 +++++++++++++++++++++ tests/basic_types.rs | 6 ++++-- 5 files changed, 60 insertions(+), 6 deletions(-) diff --git a/src/de/impls.rs b/src/de/impls.rs index fc6170d..4d715f3 100644 --- a/src/de/impls.rs +++ b/src/de/impls.rs @@ -1,4 +1,7 @@ -use core::cell::{Cell, RefCell}; +use core::{ + cell::{Cell, RefCell}, + time::Duration, +}; use super::{BorrowDecodable, BorrowDecode, Decodable, Decode}; use crate::error::DecodeError; @@ -177,7 +180,7 @@ where } } -impl<'de, T> Decodable for Cell +impl Decodable for Cell where T: Decodable, { @@ -187,7 +190,7 @@ where } } -impl<'de, T> Decodable for RefCell +impl Decodable for RefCell where T: Decodable, { @@ -197,6 +200,14 @@ where } } +impl Decodable for Duration { + fn decode(mut decoder: D) -> Result { + let secs = Decodable::decode(&mut decoder)?; + let nanos = Decodable::decode(&mut decoder)?; + Ok(Duration::new(secs, nanos)) + } +} + impl<'a, 'de, T> Decode for &'a mut T where T: Decode, diff --git a/src/enc/impls.rs b/src/enc/impls.rs index 912f101..3f1c63f 100644 --- a/src/enc/impls.rs +++ b/src/enc/impls.rs @@ -1,4 +1,7 @@ -use core::cell::{Cell, RefCell}; +use core::{ + cell::{Cell, RefCell}, + time::Duration, +}; use super::{Encode, Encodeable}; use crate::error::EncodeError; @@ -195,6 +198,14 @@ where } } +impl Encodeable for Duration { + fn encode(&self, mut encoder: E) -> Result<(), EncodeError> { + self.as_secs().encode(&mut encoder)?; + self.subsec_nanos().encode(&mut encoder)?; + Ok(()) + } +} + impl<'a, T> Encodeable for &'a T where T: Encodeable, diff --git a/src/error.rs b/src/error.rs index e466120..143d1f5 100644 --- a/src/error.rs +++ b/src/error.rs @@ -30,6 +30,15 @@ pub enum EncodeError { /// The type name of the mutex for debugging purposes type_name: &'static str, }, + + /// The encoder tried to encode a `SystemTime`, but it was before `SystemTime::UNIX_EPOCH` + #[cfg(feature = "std")] + InvalidSystemTime { + /// The error that was thrown by the SystemTime + inner: std::time::SystemTimeError, + /// The SystemTime that caused the error + time: std::time::SystemTime, + }, } /// Errors that can be encounted by decoding a type diff --git a/src/features/impl_std.rs b/src/features/impl_std.rs index cd9bff4..e3f2a17 100644 --- a/src/features/impl_std.rs +++ b/src/features/impl_std.rs @@ -4,9 +4,11 @@ use crate::{ enc::{write::Writer, Encode, Encodeable, Encoder}, error::{DecodeError, EncodeError}, }; +use core::time::Duration; use std::{ ffi::{CStr, CString}, sync::{Mutex, RwLock}, + time::SystemTime, }; /// Decode type `D` from the given reader. The reader can be any type that implements `std::io::Read`, e.g. `std::fs::File`. @@ -152,3 +154,22 @@ where Ok(RwLock::new(t)) } } + +impl Encodeable for SystemTime { + fn encode(&self, encoder: E) -> Result<(), EncodeError> { + let duration = self.duration_since(SystemTime::UNIX_EPOCH).map_err(|e| { + EncodeError::InvalidSystemTime { + inner: e, + time: *self, + } + })?; + duration.encode(encoder) + } +} + +impl Decodable for SystemTime { + fn decode(decoder: D) -> Result { + let duration = Duration::decode(decoder)?; + Ok(SystemTime::UNIX_EPOCH + duration) + } +} diff --git a/tests/basic_types.rs b/tests/basic_types.rs index 87adb15..c3cb4e1 100644 --- a/tests/basic_types.rs +++ b/tests/basic_types.rs @@ -1,7 +1,7 @@ mod utils; -use core::cell::Cell; -use std::cell::RefCell; +use core::cell::{Cell, RefCell}; +use core::time::Duration; use utils::the_same; #[test] @@ -74,6 +74,8 @@ fn test_numbers() { the_same(Cell::::new(15)); the_same(RefCell::::new(15)); + + the_same(Duration::new(5, 730023852)); } #[test]