|
@@ -4,7 +4,8 @@ use std::{
|
|
|
collections::HashMap,
|
|
collections::HashMap,
|
|
|
hash::Hash,
|
|
hash::Hash,
|
|
|
io::{Error, ErrorKind, Read, Result, Write},
|
|
io::{Error, ErrorKind, Read, Result, Write},
|
|
|
- ops::Deref
|
|
|
|
|
|
|
+ ops::Deref,
|
|
|
|
|
+ time::{Duration, SystemTime, UNIX_EPOCH}
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
use paste::paste;
|
|
use paste::paste;
|
|
@@ -183,6 +184,38 @@ impl<T: Io> Io for Box<T> {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// TODO: implement thing similar to string length type
|
|
|
|
|
+// once thats done SystemTime Io is a lot saner
|
|
|
|
|
+impl Io for Duration {
|
|
|
|
|
+ fn read(r: &mut Reader) -> Result<Self> {
|
|
|
|
|
+ u32::read(r)
|
|
|
|
|
+ .map(u64::from)
|
|
|
|
|
+ .map(Duration::from_millis)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ fn write(&self, w: &mut Writer) -> Result<()> {
|
|
|
|
|
+ u32::try_from(self.as_millis())
|
|
|
|
|
+ .map_err(other)?
|
|
|
|
|
+ .write(w)
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// TODO: configurable precision
|
|
|
|
|
+impl Io for SystemTime {
|
|
|
|
|
+ fn read(r: &mut Reader) -> Result<Self> {
|
|
|
|
|
+ u64::read(r)
|
|
|
|
|
+ .map(Duration::from_millis)
|
|
|
|
|
+ .map(|offset| UNIX_EPOCH + offset)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ fn write(&self, w: &mut Writer) -> Result<()> {
|
|
|
|
|
+ self.duration_since(UNIX_EPOCH)
|
|
|
|
|
+ .map_err(other)
|
|
|
|
|
+ .map(|offset| offset.as_millis())?
|
|
|
|
|
+ .write(w)
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// basically Error::other but not requiring nightly
|
|
// basically Error::other but not requiring nightly
|
|
|
fn other(error: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Error {
|
|
fn other(error: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Error {
|
|
|
Error::new(ErrorKind::Other, error)
|
|
Error::new(ErrorKind::Other, error)
|