Added core::time::Duration and std::time::SystemTime

This commit is contained in:
Victor Koenders 2021-10-16 13:32:12 +02:00
parent 9cf577d9bc
commit acbd385649
5 changed files with 60 additions and 6 deletions

View File

@ -1,4 +1,7 @@
use core::cell::{Cell, RefCell}; use core::{
cell::{Cell, RefCell},
time::Duration,
};
use super::{BorrowDecodable, BorrowDecode, Decodable, Decode}; use super::{BorrowDecodable, BorrowDecode, Decodable, Decode};
use crate::error::DecodeError; use crate::error::DecodeError;
@ -177,7 +180,7 @@ where
} }
} }
impl<'de, T> Decodable for Cell<T> impl<T> Decodable for Cell<T>
where where
T: Decodable, T: Decodable,
{ {
@ -187,7 +190,7 @@ where
} }
} }
impl<'de, T> Decodable for RefCell<T> impl<T> Decodable for RefCell<T>
where where
T: Decodable, T: Decodable,
{ {
@ -197,6 +200,14 @@ where
} }
} }
impl Decodable for Duration {
fn decode<D: Decode>(mut decoder: D) -> Result<Self, DecodeError> {
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 impl<'a, 'de, T> Decode for &'a mut T
where where
T: Decode, T: Decode,

View File

@ -1,4 +1,7 @@
use core::cell::{Cell, RefCell}; use core::{
cell::{Cell, RefCell},
time::Duration,
};
use super::{Encode, Encodeable}; use super::{Encode, Encodeable};
use crate::error::EncodeError; use crate::error::EncodeError;
@ -195,6 +198,14 @@ where
} }
} }
impl Encodeable for Duration {
fn encode<E: 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 impl<'a, T> Encodeable for &'a T
where where
T: Encodeable, T: Encodeable,

View File

@ -30,6 +30,15 @@ pub enum EncodeError {
/// The type name of the mutex for debugging purposes /// The type name of the mutex for debugging purposes
type_name: &'static str, 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 /// Errors that can be encounted by decoding a type

View File

@ -4,9 +4,11 @@ use crate::{
enc::{write::Writer, Encode, Encodeable, Encoder}, enc::{write::Writer, Encode, Encodeable, Encoder},
error::{DecodeError, EncodeError}, error::{DecodeError, EncodeError},
}; };
use core::time::Duration;
use std::{ use std::{
ffi::{CStr, CString}, ffi::{CStr, CString},
sync::{Mutex, RwLock}, 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`. /// 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)) Ok(RwLock::new(t))
} }
} }
impl Encodeable for SystemTime {
fn encode<E: 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<D: Decode>(decoder: D) -> Result<Self, DecodeError> {
let duration = Duration::decode(decoder)?;
Ok(SystemTime::UNIX_EPOCH + duration)
}
}

View File

@ -1,7 +1,7 @@
mod utils; mod utils;
use core::cell::Cell; use core::cell::{Cell, RefCell};
use std::cell::RefCell; use core::time::Duration;
use utils::the_same; use utils::the_same;
#[test] #[test]
@ -74,6 +74,8 @@ fn test_numbers() {
the_same(Cell::<u32>::new(15)); the_same(Cell::<u32>::new(15));
the_same(RefCell::<u32>::new(15)); the_same(RefCell::<u32>::new(15));
the_same(Duration::new(5, 730023852));
} }
#[test] #[test]