timer-deque-rs 0.8.0

A OS based timer and timer queue which implements timeout queues of different types.
Documentation
/*-
 * timer-deque-rs - a Rust crate which provides timer and timer queues based on target OS
 *  functionality.
 * 
 * Copyright (C) 2025 Aleksandr Morozov alex@nixd.org
 *  4neko.org alex@4neko.org
 * 
 * The timer-rs crate can be redistributed and/or modified
 * under the terms of either of the following licenses:
 *
 *   1. the Mozilla Public License Version 2.0 (the “MPL”) OR
 *                     
 *   2. The MIT License (MIT)
 *                     
 *   3. EUROPEAN UNION PUBLIC LICENCE v. 1.2 EUPL © the European Union 2007, 2016
 */

/// A common code for the timer setup.
pub mod timer;

/// A common code for timer polling.
pub mod poll;

/// All portable code for OS in `unix` family.
#[cfg(target_family = "unix")]
pub mod unix;


#[cfg(target_family = "unix")]
pub use unix::{timespec, TimerSetTimeFlags, UnixFd, TimerType, TimerFlags};

/// Windows specific code.
#[cfg(target_os = "windows")]
pub mod windows;

#[cfg(target_family = "windows")]
pub use windows::{timespec, TimerSetTimeFlags, UnixFd, TimerType, TimerFlags};


pub use timer::
{
    TimerReadRes, 
    TimerExpMode, 
    FdTimerCom, 
    AbsoluteTime, 
    RelativeTime,
};

#[cfg(all(target_family = "unix", feature = "enable_mio_compat"))]
pub use timer::TimerFdMioCompat;

pub use poll::{TimerPoll, PollEventType, PolledTimerFd, TimerPollOps};
pub use timer::{TimerFd, FdTimerMarker, TimerId, AsTimerId};

#[cfg(target_family = "windows")]
/// Error handling.
pub mod portable_error
{
    use std::fmt;

    use windows::core::Error;

    /// An error description.
    #[derive(Debug, Clone, PartialEq, Eq)]
    pub struct TimerPortableErr
    {
        /// [Errno] error code
        last_err: Error,

        /// Description
        msg: String
    }

    impl fmt::Display for TimerPortableErr
    {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result 
        {
            write!(f, "errno: '{}', msg: '{}'", self.last_err, self.msg)
        }
    }

    impl TimerPortableErr
    {
        pub 
        fn new(last_err: Error, msg: String) -> Self
        {
            return Self{ last_err: last_err, msg: msg };
        }

        pub 
        fn get_errno(&self) -> Error
        {
            return self.last_err.clone();
        }
    }

    /// A `type` for [Result].
    pub type TimerPortResult<T> = Result<T, TimerPortableErr>;


    #[macro_export]
    macro_rules! portable_err 
    {
        ($last_err:expr,$($arg:tt)*) => (
            return std::result::Result::Err( $crate::timer_portable::portable_error::TimerPortableErr::new($last_err, format!($($arg)*)) )
        )
    }

    #[macro_export]
    macro_rules! map_portable_err 
    {
        ($last_err:expr,$($arg:tt)*) => (
            $crate::timer_portable::portable_error::TimerPortableErr::new($last_err, format!($($arg)*))
        )
    }
}

#[cfg(target_family = "unix")]
/// Error handling.
pub mod portable_error
{
    use std::fmt;

    use nix::errno::Errno;

    /// An error description.
    #[derive(Debug, Clone, PartialEq, Eq)]
    pub struct TimerPortableErr
    {
        /// [Errno] error code
        last_err: Errno,

        /// Description
        msg: String
    }

    impl fmt::Display for TimerPortableErr
    {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result 
        {
            write!(f, "errno: '{}', msg: '{}'", self.last_err, self.msg)
        }
    }

    impl TimerPortableErr
    {
        pub 
        fn new(last_err: Errno, msg: String) -> Self
        {
            return Self{ last_err: last_err, msg: msg };
        }

        pub 
        fn get_errno(&self) -> Errno
        {
            return self.last_err;
        }
    }

    /// A `type` for [Result].
    pub type TimerPortResult<T> = Result<T, TimerPortableErr>;


    #[macro_export]
    macro_rules! portable_err 
    {
        ($last_err:expr,$($arg:tt)*) => (
            return std::result::Result::Err( $crate::timer_portable::portable_error::TimerPortableErr::new($last_err, format!($($arg)*)) )
        )
    }

    #[macro_export]
    macro_rules! map_portable_err 
    {
        ($last_err:expr,$($arg:tt)*) => (
            $crate::timer_portable::portable_error::TimerPortableErr::new($last_err, format!($($arg)*))
        )
    }
}