1 //! Channel that never delivers messages. 2 //! 3 //! Messages cannot be sent into this kind of channel. 4 5 use std::marker::PhantomData; 6 use std::time::Instant; 7 8 use crate::context::Context; 9 use crate::err::{RecvTimeoutError, TryRecvError}; 10 use crate::select::{Operation, SelectHandle, Token}; 11 use crate::utils; 12 13 /// This flavor doesn't need a token. 14 pub(crate) type NeverToken = (); 15 16 /// Channel that never delivers messages. 17 pub(crate) struct Channel<T> { 18 _marker: PhantomData<T>, 19 } 20 21 impl<T> Channel<T> { 22 /// Creates a channel that never delivers messages. 23 #[inline] new() -> Self24 pub(crate) fn new() -> Self { 25 Channel { 26 _marker: PhantomData, 27 } 28 } 29 30 /// Attempts to receive a message without blocking. 31 #[inline] try_recv(&self) -> Result<T, TryRecvError>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] recv(&self, deadline: Option<Instant>) -> Result<T, RecvTimeoutError>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] read(&self, _token: &mut Token) -> Result<T, ()>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] is_empty(&self) -> bool51 pub(crate) fn is_empty(&self) -> bool { 52 true 53 } 54 55 /// Returns `true` if the channel is full. 56 #[inline] is_full(&self) -> bool57 pub(crate) fn is_full(&self) -> bool { 58 true 59 } 60 61 /// Returns the number of messages in the channel. 62 #[inline] len(&self) -> usize63 pub(crate) fn len(&self) -> usize { 64 0 65 } 66 67 /// Returns the capacity of the channel. 68 #[inline] capacity(&self) -> Option<usize>69 pub(crate) fn capacity(&self) -> Option<usize> { 70 Some(0) 71 } 72 } 73 74 impl<T> SelectHandle for Channel<T> { 75 #[inline] try_select(&self, _token: &mut Token) -> bool76 fn try_select(&self, _token: &mut Token) -> bool { 77 false 78 } 79 80 #[inline] deadline(&self) -> Option<Instant>81 fn deadline(&self) -> Option<Instant> { 82 None 83 } 84 85 #[inline] register(&self, _oper: Operation, _cx: &Context) -> bool86 fn register(&self, _oper: Operation, _cx: &Context) -> bool { 87 self.is_ready() 88 } 89 90 #[inline] unregister(&self, _oper: Operation)91 fn unregister(&self, _oper: Operation) {} 92 93 #[inline] accept(&self, token: &mut Token, _cx: &Context) -> bool94 fn accept(&self, token: &mut Token, _cx: &Context) -> bool { 95 self.try_select(token) 96 } 97 98 #[inline] is_ready(&self) -> bool99 fn is_ready(&self) -> bool { 100 false 101 } 102 103 #[inline] watch(&self, _oper: Operation, _cx: &Context) -> bool104 fn watch(&self, _oper: Operation, _cx: &Context) -> bool { 105 self.is_ready() 106 } 107 108 #[inline] unwatch(&self, _oper: Operation)109 fn unwatch(&self, _oper: Operation) {} 110 } 111