crossbeam_channel/flavors/
never.rs

1//! Channel that never delivers messages.
2//!
3//! Messages cannot be sent into this kind of channel.
4
5use std::marker::PhantomData;
6use std::time::Instant;
7
8use crate::context::Context;
9use crate::err::{RecvTimeoutError, TryRecvError};
10use crate::select::{Operation, SelectHandle, Token};
11use crate::utils;
12
13/// This flavor doesn't need a token.
14pub(crate) type NeverToken = ();
15
16/// Channel that never delivers messages.
17pub(crate) struct Channel<T> {
18    _marker: PhantomData<T>,
19}
20
21impl<T> Channel<T> {
22    /// Creates a channel that never delivers messages.
23    #[inline]
24    pub(crate) fn new() -> Self {
25        Channel {
26            _marker: PhantomData,
27        }
28    }
29
30    /// Attempts to receive a message without blocking.
31    #[inline]
32    pub(crate) fn try_recv(&self) -> Result<T, TryRecvError> {
33        Err(TryRecvError::Empty)
34    }
35
36    /// Receives a message from the channel.
37    #[inline]
38    pub(crate) fn recv(&self, deadline: Option<Instant>) -> Result<T, RecvTimeoutError> {
39        utils::sleep_until(deadline);
40        Err(RecvTimeoutError::Timeout)
41    }
42
43    /// Reads a message from the channel.
44    #[inline]
45    pub(crate) unsafe fn read(&self, _token: &mut Token) -> Result<T, ()> {
46        Err(())
47    }
48
49    /// Returns `true` if the channel is empty.
50    #[inline]
51    pub(crate) fn is_empty(&self) -> bool {
52        true
53    }
54
55    /// Returns `true` if the channel is full.
56    #[inline]
57    pub(crate) fn is_full(&self) -> bool {
58        true
59    }
60
61    /// Returns the number of messages in the channel.
62    #[inline]
63    pub(crate) fn len(&self) -> usize {
64        0
65    }
66
67    /// Returns the capacity of the channel.
68    #[allow(clippy::unnecessary_wraps)] // This is intentional.
69    #[inline]
70    pub(crate) fn capacity(&self) -> Option<usize> {
71        Some(0)
72    }
73}
74
75impl<T> SelectHandle for Channel<T> {
76    #[inline]
77    fn try_select(&self, _token: &mut Token) -> bool {
78        false
79    }
80
81    #[inline]
82    fn deadline(&self) -> Option<Instant> {
83        None
84    }
85
86    #[inline]
87    fn register(&self, _oper: Operation, _cx: &Context) -> bool {
88        self.is_ready()
89    }
90
91    #[inline]
92    fn unregister(&self, _oper: Operation) {}
93
94    #[inline]
95    fn accept(&self, token: &mut Token, _cx: &Context) -> bool {
96        self.try_select(token)
97    }
98
99    #[inline]
100    fn is_ready(&self) -> bool {
101        false
102    }
103
104    #[inline]
105    fn watch(&self, _oper: Operation, _cx: &Context) -> bool {
106        self.is_ready()
107    }
108
109    #[inline]
110    fn unwatch(&self, _oper: Operation) {}
111}