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
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//! Delays

use core::future::Future;

/// Millisecond delay
///
/// `UXX` denotes the range type of the delay time. `UXX` can be `u8`, `u16`, etc. A single type can
/// implement this trait for different types of `UXX`.
pub trait AsyncDelayMs<UXX> {
    /// Delay future for polling on completion
    type DelayFuture<'f>: Future<Output = ()>;

    /// Pauses execution for `ms` milliseconds
    fn async_delay_ms(&mut self, ms: UXX) -> Self::DelayFuture<'_>;
}

/// Microsecond delay
///
/// `UXX` denotes the range type of the delay time. `UXX` can be `u8`, `u16`, etc. A single type can
/// implement this trait for different types of `UXX`.
pub trait AsyncDelayUs<UXX> {
    /// Delay future for polling on completion
    type DelayFuture<'f>: Future<Output = ()>;

    /// Pauses execution for `us` microseconds
    fn async_delay_us(&mut self, us: UXX) -> Self::DelayFuture<'_>;
}

/// Implement `AsyncDelayMs<u16>`, `AsyncDelayMs<u8>` and `AsyncDelayMs<i32>`
/// based on the `AsyncDelayMs<u32>` implementation.
#[macro_export]
macro_rules! impl_delay_ms_for_ms_u32 {
    ($delay:ty) => {
        impl AsyncDelayMs<u16> for $delay {
            type DelayFuture<'f> = <$delay as AsyncDelayMs<u32>>::DelayFuture<'f>;

            #[inline(always)]
            fn async_delay_ms(&mut self, ms: u16) -> Self::DelayFuture<'_> {
                self.async_delay_ms(u32::from(ms))
            }
        }

        impl AsyncDelayMs<u8> for $delay {
            type DelayFuture<'f> = <$delay as AsyncDelayMs<u32>>::DelayFuture<'f>;

            #[inline(always)]
            fn async_delay_ms(&mut self, ms: u8) -> Self::DelayFuture<'_> {
                self.async_delay_ms(u32::from(ms))
            }
        }

        impl AsyncDelayMs<i32> for $delay {
            type DelayFuture<'f> = <$delay as AsyncDelayMs<u32>>::DelayFuture<'f>;

            #[inline(always)]
            fn async_delay_ms(&mut self, ms: i32) -> Self::DelayFuture<'_> {
                assert!(ms >= 0);
                self.async_delay_ms(ms as u32)
            }
        }
    };
}

/// Implement `AsyncDelayUs<u16>`, `AsyncDelayUs<u8>` and `AsyncDelayUs<i32>`
/// based on the `AsyncDelayUs<u32>` implementation.
#[macro_export]
macro_rules! impl_delay_us_for_us_u32 {
    ($delay:ty) => {
        impl AsyncDelayUs<u16> for $delay {
            type DelayFuture<'f> = <$delay as AsyncDelayUs<u32>>::DelayFuture<'f>;

            #[inline(always)]
            fn async_delay_us(&mut self, us: u16) -> Self::DelayFuture<'_> {
                self.async_delay_us(u32::from(us))
            }
        }

        impl AsyncDelayUs<u8> for $delay {
            type DelayFuture<'f> = <$delay as AsyncDelayUs<u32>>::DelayFuture<'f>;

            #[inline(always)]
            fn async_delay_us(&mut self, us: u8) -> Self::DelayFuture<'_> {
                self.async_delay_us(u32::from(us))
            }
        }

        impl AsyncDelayUs<i32> for $delay {
            type DelayFuture<'f> = <$delay as AsyncDelayUs<u32>>::DelayFuture<'f>;

            #[inline(always)]
            fn async_delay_us(&mut self, us: i32) -> Self::DelayFuture<'_> {
                assert!(us >= 0);
                self.async_delay_us(us as u32)
            }
        }
    };
}

/// Implement `AsyncDelayUs<u32>`, `AsyncDelayUs<u16>`, `AsyncDelayUs<u8>` and `AsyncDelayUs<i32>`
/// based on the `AsyncDelayUs<u64>` implementation.
#[macro_export]
macro_rules! impl_delay_us_for_us_u64 {
    ($delay:ty) => {
        impl AsyncDelayUs<u32> for $delay {
            type DelayFuture<'f> = <$delay as AsyncDelayUs<u64>>::DelayFuture<'f>;

            #[inline(always)]
            fn async_delay_us(&mut self, us: u32) -> Self::DelayFuture<'_> {
                self.async_delay_us(us as u64)
            }
        }

        $crate::impl_delay_us_for_us_u32!($delay);
    };
}

/// Implement `AsyncDelayMs<u32>`, `AsyncDelayMs<u16>`, `AsyncDelayMs<u8>` and `AsyncDelayMs<i32>`
/// based on the `AsyncDelayUs<u64>` implementation.
#[macro_export]
macro_rules! impl_delay_ms_for_us_u64 {
    ($delay:ty) => {
        impl AsyncDelayMs<u32> for $delay {
            type DelayFuture<'f> = <$delay as AsyncDelayUs<u64>>::DelayFuture<'f>;

            fn async_delay_ms(&mut self, ms: u32) -> Self::DelayFuture<'_> {
                self.async_delay_us((ms as u64) * 1000)
            }
        }

        $crate::impl_delay_ms_for_ms_u32!($delay);
    };
}