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 #[inline]
69 pub(crate) fn capacity(&self) -> Option<usize> {
70 Some(0)
71 }
72}
73
74impl<T> SelectHandle for Channel<T> {
75 #[inline]
76 fn try_select(&self, _token: &mut Token) -> bool {
77 false
78 }
79
80 #[inline]
81 fn deadline(&self) -> Option<Instant> {
82 None
83 }
84
85 #[inline]
86 fn register(&self, _oper: Operation, _cx: &Context) -> bool {
87 self.is_ready()
88 }
89
90 #[inline]
91 fn unregister(&self, _oper: Operation) {}
92
93 #[inline]
94 fn accept(&self, token: &mut Token, _cx: &Context) -> bool {
95 self.try_select(token)
96 }
97
98 #[inline]
99 fn is_ready(&self) -> bool {
100 false
101 }
102
103 #[inline]
104 fn watch(&self, _oper: Operation, _cx: &Context) -> bool {
105 self.is_ready()
106 }
107
108 #[inline]
109 fn unwatch(&self, _oper: Operation) {}
110}
111