1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use std::time::Duration;

/// Ffi-safe equivalent of ::std::time::Duration .
#[derive(
    Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Deserialize, Serialize, StableAbi,
)]
#[repr(C)]
#[sabi(inside_abi_stable_crate)]
pub struct RDuration {
    seconds: u64,
    subsec_nanos: u32,
}

impl RDuration {
    /// Constructs this RDuration from seconds and the nanoseconds inside a second .
    pub const fn new(seconds: u64, subsec_nanos: u32) -> Self {
        Self {
            subsec_nanos,
            seconds,
        }
    }

    /// The ammount of fractional nanoseconds (total_nanoseconds % 1_000_000_000) 
    /// of this RDuration.
    pub const fn subsec_nanos(&self) -> u32 {
        self.subsec_nanos
    }

    /// The ammount of seconds pf this RDuration.
    pub const fn seconds(&self) -> u64 {
        self.seconds
    }
}

impl_from_rust_repr! {
    impl From<Duration> for RDuration {
        fn(v){
            RDuration {
                subsec_nanos: v.subsec_nanos(),
                seconds: v.as_secs(),
            }
        }
    }
}

impl_into_rust_repr! {
    impl Into<Duration> for RDuration {
        fn(this){
            Duration::new(this.seconds, this.subsec_nanos)
        }
    }
}