timer_deque_rs/timer_portable/
mod.rs

1/*-
2 * timer-deque-rs - a Rust crate which provides timer and timer queues based on target OS
3 *  functionality.
4 * 
5 * Copyright (C) 2025 Aleksandr Morozov alex@nixd.org
6 *  4neko.org alex@4neko.org
7 * 
8 * The timer-rs crate can be redistributed and/or modified
9 * under the terms of either of the following licenses:
10 *
11 *   1. the Mozilla Public License Version 2.0 (the “MPL”) OR
12 *                     
13 *   2. EUROPEAN UNION PUBLIC LICENCE v. 1.2 EUPL © the European Union 2007, 2016
14 */
15
16/// A common code for the timer setup.
17pub mod timer;
18
19/// A common code for timer polling.
20pub mod poll;
21
22/// Linux specific code.
23#[cfg(target_os = "linux")]
24pub mod linux;
25
26#[cfg(target_os = "linux")]
27pub use linux::timer_poll::TimerEventWatch;
28
29#[cfg(target_os = "linux")]
30pub type DefaultEventWatch = TimerEventWatch;
31
32/// BSD specific code (including OSX as it is compat).
33#[cfg(any(
34    target_os = "freebsd",
35    target_os = "dragonfly",
36    target_os = "netbsd",
37    target_os = "openbsd",
38    target_os = "macos",
39))]
40pub mod bsd;
41
42#[cfg(
43    any(
44        target_os = "freebsd",
45        target_os = "dragonfly",
46        target_os = "netbsd",
47        target_os = "openbsd",
48        target_os = "macos",
49    )
50)]
51pub use bsd::TimerEventWatch;
52
53#[cfg(any(
54    target_os = "freebsd",
55    target_os = "dragonfly",
56    target_os = "netbsd",
57    target_os = "openbsd",
58    target_os = "macos",
59))]
60pub type DefaultEventWatch = TimerEventWatch;
61
62pub use timer::
63{
64    TimerFlags, 
65    TimerType, 
66    TimerSetTimeFlags, 
67    TimerReadRes, 
68    TimerExpMode, 
69    FdTimerCom, 
70    AbsoluteTime, 
71    RelativeTime
72};
73pub use poll::{TimerPoll, PollEventType, PollResult};
74
75/// Error handling.
76pub mod portable_error
77{
78    use std::fmt;
79
80    use nix::errno::Errno;
81
82    /// An error description.
83    #[derive(Debug, Clone, PartialEq, Eq)]
84    pub struct TimerPortableErr
85    {
86        /// [Errno] error code
87        last_err: Errno,
88
89        /// Description
90        msg: String
91    }
92
93    impl fmt::Display for TimerPortableErr
94    {
95        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result 
96        {
97            write!(f, "errno: '{}', msg: '{}'", self.last_err, self.msg)
98        }
99    }
100
101    impl TimerPortableErr
102    {
103        pub 
104        fn new(last_err: Errno, msg: String) -> Self
105        {
106            return Self{ last_err: last_err, msg: msg };
107        }
108
109        pub 
110        fn get_errno(&self) -> Errno
111        {
112            return self.last_err;
113        }
114    }
115
116    /// A `type` for [Result].
117    pub type TimerPortResult<T> = Result<T, TimerPortableErr>;
118
119
120    #[macro_export]
121    macro_rules! portable_err 
122    {
123        ($last_err:expr,$($arg:tt)*) => (
124            return std::result::Result::Err( $crate::timer_portable::portable_error::TimerPortableErr::new($last_err, format!($($arg)*)) )
125        )
126    }
127
128    #[macro_export]
129    macro_rules! map_portable_err 
130    {
131        ($last_err:expr,$($arg:tt)*) => (
132            $crate::timer_portable::portable_error::TimerPortableErr::new($last_err, format!($($arg)*))
133        )
134    }
135}