      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
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173

    //! Channel that delivers messages periodically.
    //!
    //! Messages cannot be sent into this kind of channel; they are materialized on demand.

    use std::thread;
    use std::time::{Duration, Instant};

    use crossbeam_utils::atomic::AtomicCell;

    use context::Context;
    use err::{RecvTimeoutError, TryRecvError};
    use select::{Operation, SelectHandle, Token};

    /// Result of a receive operation.
    pub type TickToken = Option<Instant>;

    /// Channel that delivers messages periodically.
    pub struct Channel {
        /// The instant at which the next message will be delivered.
        delivery_time: AtomicCell<Instant>,

        /// The time interval in which messages get delivered.
        duration: Duration,
    }

    impl Channel {
        /// Creates a channel that delivers messages periodically.
        #[inline]
        pub fn new(dur: Duration) -> Self {
            Channel {
                delivery_time: AtomicCell::new(Instant::now() + dur),
                duration: dur,
            }
        }

        /// Attempts to receive a message without blocking.
        #[inline]
        pub fn try_recv(&self) -> Result<Instant, TryRecvError> {
            loop {
                let now = Instant::now();
                let delivery_time = self.delivery_time.load();

                if now < delivery_time {
                    return Err(TryRecvError::Empty);
                }

                if self
                    .delivery_time
                    .compare_exchange(delivery_time, now + self.duration)
                    .is_ok()
                {
                    return Ok(delivery_time);
                }
            }
        }

        /// Receives a message from the channel.
        #[inline]
        pub fn recv(&self, deadline: Option<Instant>) -> Result<Instant, RecvTimeoutError> {
            loop {
                // Compute the time to sleep until the next message or the deadline.
                let offset = {
                    let mut delivery_time = self.delivery_time.load();
                    let now = Instant::now();

                    // Check if we can receive the next message.
                    if now >= delivery_time
                        && self
                            .delivery_time
                            .compare_exchange(delivery_time, now + self.duration)
                            .is_ok()
                    {
                        return Ok(delivery_time);
                    }

                    // Check if the operation deadline has been reached.
                    if let Some(d) = deadline {
                        if now >= d {
                            return Err(RecvTimeoutError::Timeout);
                        }

                        delivery_time.min(d) - now
                    } else {
                        delivery_time - now
                    }
                };

                thread::sleep(offset);
            }
        }

        /// Reads a message from the channel.
        #[inline]
        pub unsafe fn read(&self, token: &mut Token) -> Result<Instant, ()> {
            token.tick.ok_or(())
        }

        /// Returns `true` if the channel is empty.
        #[inline]
        pub fn is_empty(&self) -> bool {
            Instant::now() < self.delivery_time.load()
        }

        /// Returns `true` if the channel is full.
        #[inline]
        pub fn is_full(&self) -> bool {
            !self.is_empty()
        }

        /// Returns the number of messages in the channel.
        #[inline]
        pub fn len(&self) -> usize {
            if self.is_empty() {
                0
            } else {
                1
            }
        }

        /// Returns the capacity of the channel.
        #[inline]
        pub fn capacity(&self) -> Option<usize> {
            Some(1)
        }
    }

    impl SelectHandle for Channel {
        #[inline]
        fn try_select(&self, token: &mut Token) -> bool {
            match self.try_recv() {
                Ok(msg) => {
                    token.tick = Some(msg);
                    true
                }
                Err(TryRecvError::Disconnected) => {
                    token.tick = None;
                    true
                }
                Err(TryRecvError::Empty) => false,
            }
        }

        #[inline]
        fn deadline(&self) -> Option<Instant> {
            Some(self.delivery_time.load())
        }

        #[inline]
        fn register(&self, _oper: Operation, _cx: &Context) -> bool {
            self.is_ready()
        }

        #[inline]
        fn unregister(&self, _oper: Operation) {}

        #[inline]
        fn accept(&self, token: &mut Token, _cx: &Context) -> bool {
            self.try_select(token)
        }

        #[inline]
        fn is_ready(&self) -> bool {
            !self.is_empty()
        }

        #[inline]
        fn watch(&self, _oper: Operation, _cx: &Context) -> bool {
            self.is_ready()
        }

        #[inline]
        fn unwatch(&self, _oper: Operation) {}
    }

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g., `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g., `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g., `str,u8` or `String,struct:Vec,test`)