@@ -40,6 +40,7 @@ extern crate alloc;
4040
4141use core:: fmt;
4242use core:: future:: Future ;
43+ use core:: marker:: PhantomPinned ;
4344use core:: pin:: Pin ;
4445use core:: sync:: atomic:: { AtomicUsize , Ordering } ;
4546use core:: task:: { Context , Poll } ;
@@ -52,6 +53,7 @@ use event_listener::{Event, EventListener};
5253use event_listener_strategy:: { easy_wrapper, EventListenerFuture , Strategy } ;
5354use futures_core:: ready;
5455use futures_core:: stream:: Stream ;
56+ use pin_project_lite:: pin_project;
5557
5658struct Channel < T > {
5759 /// Inner message queue.
@@ -132,8 +134,9 @@ pub fn bounded<T>(cap: usize) -> (Sender<T>, Receiver<T>) {
132134 channel : channel. clone ( ) ,
133135 } ;
134136 let r = Receiver {
135- listener : EventListener :: new ( ) ,
137+ listener : None ,
136138 channel,
139+ _pin : PhantomPinned ,
137140 } ;
138141 ( s, r)
139142}
@@ -172,8 +175,9 @@ pub fn unbounded<T>() -> (Sender<T>, Receiver<T>) {
172175 channel : channel. clone ( ) ,
173176 } ;
174177 let r = Receiver {
175- listener : EventListener :: new ( ) ,
178+ listener : None ,
176179 channel,
180+ _pin : PhantomPinned ,
177181 } ;
178182 ( s, r)
179183}
@@ -247,7 +251,8 @@ impl<T> Sender<T> {
247251 Send :: _new ( SendInner {
248252 sender : self ,
249253 msg : Some ( msg) ,
250- listener : EventListener :: new ( ) ,
254+ listener : None ,
255+ _pin : PhantomPinned ,
251256 } )
252257 }
253258
@@ -477,7 +482,7 @@ impl<T> Clone for Sender<T> {
477482 }
478483}
479484
480- pin_project_lite :: pin_project! {
485+ pin_project ! {
481486 /// The receiving side of a channel.
482487 ///
483488 /// Receivers can be cloned and shared among threads. When all receivers associated with a channel
@@ -491,8 +496,11 @@ pin_project_lite::pin_project! {
491496 channel: Arc <Channel <T >>,
492497
493498 // Listens for a send or close event to unblock this stream.
499+ listener: Option <EventListener >,
500+
501+ // Keeping this type `!Unpin` enables future optimizations.
494502 #[ pin]
495- listener : EventListener ,
503+ _pin : PhantomPinned
496504 }
497505
498506 impl <T > PinnedDrop for Receiver <T > {
@@ -567,7 +575,8 @@ impl<T> Receiver<T> {
567575 pub fn recv ( & self ) -> Recv < ' _ , T > {
568576 Recv :: _new ( RecvInner {
569577 receiver : self ,
570- listener : EventListener :: new ( ) ,
578+ listener : None ,
579+ _pin : PhantomPinned ,
571580 } )
572581 }
573582
@@ -787,7 +796,8 @@ impl<T> Clone for Receiver<T> {
787796
788797 Receiver {
789798 channel : self . channel . clone ( ) ,
790- listener : EventListener :: new ( ) ,
799+ listener : None ,
800+ _pin : PhantomPinned ,
791801 }
792802 }
793803}
@@ -800,8 +810,9 @@ impl<T> Stream for Receiver<T> {
800810 // If this stream is listening for events, first wait for a notification.
801811 {
802812 let this = self . as_mut ( ) . project ( ) ;
803- if this. listener . is_listening ( ) {
804- ready ! ( this. listener. poll( cx) ) ;
813+ if let Some ( listener) = this. listener . as_mut ( ) {
814+ ready ! ( Pin :: new( listener) . poll( cx) ) ;
815+ * this. listener = None ;
805816 }
806817 }
807818
@@ -810,26 +821,26 @@ impl<T> Stream for Receiver<T> {
810821 match self . try_recv ( ) {
811822 Ok ( msg) => {
812823 // The stream is not blocked on an event - drop the listener.
813- let mut this = self . project ( ) ;
814- this. listener . as_mut ( ) . set ( EventListener :: new ( ) ) ;
824+ let this = self . as_mut ( ) . project ( ) ;
825+ * this. listener = None ;
815826 return Poll :: Ready ( Some ( msg) ) ;
816827 }
817828 Err ( TryRecvError :: Closed ) => {
818829 // The stream is not blocked on an event - drop the listener.
819- let mut this = self . project ( ) ;
820- this. listener . as_mut ( ) . set ( EventListener :: new ( ) ) ;
830+ let this = self . as_mut ( ) . project ( ) ;
831+ * this. listener = None ;
821832 return Poll :: Ready ( None ) ;
822833 }
823834 Err ( TryRecvError :: Empty ) => { }
824835 }
825836
826837 // Receiving failed - now start listening for notifications or wait for one.
827- let mut this = self . as_mut ( ) . project ( ) ;
828- if this. listener . is_listening ( ) {
838+ let this = self . as_mut ( ) . project ( ) ;
839+ if this. listener . is_some ( ) {
829840 // Go back to the outer loop to wait for a notification.
830841 break ;
831842 } else {
832- this. listener . as_mut ( ) . listen ( & this. channel . stream_ops ) ;
843+ * this. listener = Some ( this. channel . stream_ops . listen ( ) ) ;
833844 }
834845 }
835846 }
@@ -914,7 +925,8 @@ impl<T> WeakReceiver<T> {
914925 }
915926 Ok ( _) => Some ( Receiver {
916927 channel : self . channel . clone ( ) ,
917- listener : EventListener :: new ( ) ,
928+ listener : None ,
929+ _pin : PhantomPinned ,
918930 } ) ,
919931 }
920932 }
@@ -1084,13 +1096,22 @@ easy_wrapper! {
10841096 pub ( crate ) wait( ) ;
10851097}
10861098
1087- pin_project_lite :: pin_project! {
1099+ pin_project ! {
10881100 #[ derive( Debug ) ]
1101+ #[ project( !Unpin ) ]
10891102 struct SendInner <' a, T > {
1103+ // Reference to the original sender.
10901104 sender: & ' a Sender <T >,
1105+
1106+ // The message to send.
10911107 msg: Option <T >,
1108+
1109+ // Listener waiting on the channel.
1110+ listener: Option <EventListener >,
1111+
1112+ // Keeping this type `!Unpin` enables future optimizations.
10921113 #[ pin]
1093- listener : EventListener ,
1114+ _pin : PhantomPinned
10941115 }
10951116}
10961117
@@ -1103,7 +1124,7 @@ impl<'a, T> EventListenerFuture for SendInner<'a, T> {
11031124 strategy : & mut S ,
11041125 context : & mut S :: Context ,
11051126 ) -> Poll < Result < ( ) , SendError < T > > > {
1106- let mut this = self . project ( ) ;
1127+ let this = self . project ( ) ;
11071128
11081129 loop {
11091130 let msg = this. msg . take ( ) . unwrap ( ) ;
@@ -1115,11 +1136,11 @@ impl<'a, T> EventListenerFuture for SendInner<'a, T> {
11151136 }
11161137
11171138 // Sending failed - now start listening for notifications or wait for one.
1118- if this. listener . is_listening ( ) {
1139+ if this. listener . is_some ( ) {
11191140 // Poll using the given strategy
1120- ready ! ( S :: poll( strategy, this. listener. as_mut ( ) , context) ) ;
1141+ ready ! ( S :: poll( strategy, & mut * this. listener, context) ) ;
11211142 } else {
1122- this. listener . as_mut ( ) . listen ( & this. sender . channel . send_ops ) ;
1143+ * this. listener = Some ( this. sender . channel . send_ops . listen ( ) ) ;
11231144 }
11241145 }
11251146 }
@@ -1134,12 +1155,19 @@ easy_wrapper! {
11341155 pub ( crate ) wait( ) ;
11351156}
11361157
1137- pin_project_lite :: pin_project! {
1158+ pin_project ! {
11381159 #[ derive( Debug ) ]
1160+ #[ project( !Unpin ) ]
11391161 struct RecvInner <' a, T > {
1162+ // Reference to the receiver.
11401163 receiver: & ' a Receiver <T >,
1164+
1165+ // Listener waiting on the channel.
1166+ listener: Option <EventListener >,
1167+
1168+ // Keeping this type `!Unpin` enables future optimizations.
11411169 #[ pin]
1142- listener : EventListener ,
1170+ _pin : PhantomPinned
11431171 }
11441172}
11451173
@@ -1152,7 +1180,7 @@ impl<'a, T> EventListenerFuture for RecvInner<'a, T> {
11521180 strategy : & mut S ,
11531181 cx : & mut S :: Context ,
11541182 ) -> Poll < Result < T , RecvError > > {
1155- let mut this = self . project ( ) ;
1183+ let this = self . project ( ) ;
11561184
11571185 loop {
11581186 // Attempt to receive a message.
@@ -1163,13 +1191,11 @@ impl<'a, T> EventListenerFuture for RecvInner<'a, T> {
11631191 }
11641192
11651193 // Receiving failed - now start listening for notifications or wait for one.
1166- if this. listener . is_listening ( ) {
1194+ if this. listener . is_some ( ) {
11671195 // Poll using the given strategy
1168- ready ! ( S :: poll( strategy, this. listener. as_mut ( ) , cx) ) ;
1196+ ready ! ( S :: poll( strategy, & mut * this. listener, cx) ) ;
11691197 } else {
1170- this. listener
1171- . as_mut ( )
1172- . listen ( & this. receiver . channel . recv_ops ) ;
1198+ * this. listener = Some ( this. receiver . channel . recv_ops . listen ( ) ) ;
11731199 }
11741200 }
11751201 }
0 commit comments