From 99e740eb971b83a8f24f42b455301831a6cda159 Mon Sep 17 00:00:00 2001 From: Avril Date: Sat, 6 Feb 2021 13:30:50 +0000 Subject: [PATCH] start main data types duration macro --- src/data.rs | 22 ++++++++++++++++++++++ src/ext.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 6 +++++- 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 src/data.rs diff --git a/src/data.rs b/src/data.rs new file mode 100644 index 0000000..524aa6e --- /dev/null +++ b/src/data.rs @@ -0,0 +1,22 @@ +//! Datatypes for the program +use super::*; + +/// A raw file or directory inode number +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)] +#[repr(transparent)] +pub struct INode(u64); + +impl INode +{ + /// Create a new `INode` wrapper from any `u64` without checking if it is a real inode. + #[inline] pub const unsafe fn new_unchecked(ino: u64) -> Self + { + Self(ino) + } + + /// Convert into raw `u64` inode number. + #[inline] pub const fn into_inner(self) -> u64 + { + self.0 + } +} diff --git a/src/ext.rs b/src/ext.rs index 71b1727..40df186 100644 --- a/src/ext.rs +++ b/src/ext.rs @@ -178,12 +178,55 @@ where S: Stream } } +/// Create a duration with time suffix `h`, `m`, `s`, `ms` or `ns`. +/// +/// # Combination +/// These can also be combined. +/// ``` +/// #use crate::duration; +/// duration!(1 h, 20 m, 30 s); +/// ``` +#[macro_export ]macro_rules! duration +{ + (0 $($_any:tt)?) => (::std::time::Duration::from_secs(0)); + ($dur:literal ms) => (::std::time::Duration::from_millis($dur)); + ($dur:literal ns) => (::std::time::Duration::from_nanos($dur)); + ($dur:literal s) => (::std::time::Duration::from_secs($dur)); + ($dur:literal m) => (::std::time::Duration::from_secs($dur * 60)); + ($dur:literal h) => (::std::time::Duration::from_secs($dur * 60 * 60)); + + ( $($dur:literal $unit:tt),*)=> { + duration!(0 s) $( + + duration!($dur $unit) + )* + }; +} + #[cfg(test)] mod tests { use super::*; + #[test] + fn duration() + { + let dur = duration!(1 h) + + duration!(10 m) + + duration!(1 s) + + duration!(10 ms, 2 ns); + println!("{:?}", dur); + let dur2 = duration!( + 1 h, + 10 m, + 1 s, + 10 ms, + 2 ns + ); + println!("{:?}", dur2); + assert_eq!(dur, dur2); + } + #[tokio::test] async fn stream_gating_with_timeout() { diff --git a/src/main.rs b/src/main.rs index ef73180..947b354 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,13 @@ +#![allow(dead_code)] + #[macro_use] extern crate pin_project; -mod ext; +#[macro_use] mod ext; pub use ext::prelude::*; +mod data; + #[tokio::main] async fn main() { println!("Hello, world!");