@@ -3,9 +3,17 @@ use core::mem::PinMut;
33use core:: num:: NonZeroUsize ;
44use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
55
6- use actix:: prelude:: * ;
6+ use actix:: {
7+ dev:: ToEnvelope ,
8+ fut,
9+ prelude:: * ,
10+ } ;
711use futures:: { compat:: * , prelude:: * } ;
812use log:: * ;
13+ use tokio:: prelude:: {
14+ Async as Async01 ,
15+ Poll as Poll01 ,
16+ } ;
917use typemap:: { Key , SendMap } ;
1018use pin_utils:: unsafe_pinned;
1119
@@ -19,13 +27,15 @@ pub struct Bus {
1927impl Bus {
2028 pub fn new_id ( ) -> BusId { BusId :: new ( ) }
2129
22- pub fn subscribe < M > ( id : BusId , recipient : Recipient < M > ) -> WaitSubscribe < M >
30+ pub fn subscribe < A , M > ( id : BusId ) -> WaitSubscribe < A , M >
2331 where
32+ A : Actor + Handler < M > ,
33+ A :: Context : AsyncContext < A > + ToEnvelope < A , M > ,
2434 M : Message + Send + Clone + ' static ,
2535 M :: Result : Send ,
2636 {
2737 let bus = Bus :: from_registry ( ) ;
28- WaitSubscribe :: new ( & bus, id, recipient )
38+ WaitSubscribe :: new ( & bus, id)
2939 }
3040
3141 pub fn publish < M > ( id : BusId , message : M ) -> WaitPublish < M >
@@ -97,30 +107,31 @@ impl BusId {
97107}
98108
99109struct Bucket < M > ( PhantomData < M > ) ;
100- impl < M : Message + Send + ' static > Key for Bucket < M > where M :: Result : Send {
110+ impl < M : Send + ' static > Key for Bucket < M > {
101111 type Value = SubscriptionList < BusId , M > ;
102112}
103113
104114#[ derive( Message ) ]
105- #[ rtype( result = "() " ) ]
106- pub struct Subscribe < M : actix :: Message + Send + ' static > where M :: Result : Send {
115+ #[ rtype( result = "crate::util::subscription::Receiver<M> " ) ]
116+ pub struct Subscribe < M : ' static > {
107117 receiver : BusId ,
108- pub recipient : Recipient < M > ,
118+ _msg : PhantomData < M > ,
109119}
110120
111- impl < M : Message + Send + ' static > Subscribe < M > where M :: Result : Send {
112- pub fn new ( receiver : BusId , recipient : Recipient < M > ) -> Self {
113- Subscribe { receiver, recipient }
121+ impl < M > Subscribe < M > {
122+ pub fn new ( receiver : BusId ) -> Self {
123+ Subscribe { receiver, _msg : PhantomData }
114124 }
115125}
116126
117- impl < M : Message + Send + ' static > Handler < Subscribe < M > > for Bus where M :: Result : Send {
118- type Result = ( ) ;
127+ impl < M : Send + Sized > Handler < Subscribe < M > > for Bus {
128+ type Result = MessageResult < Subscribe < M > > ;
119129
120130 fn handle ( & mut self , msg : Subscribe < M > , _: & mut Self :: Context ) -> Self :: Result {
121131 debug ! ( "Bus received Subscribe<M>" ) ;
122132 let list = self . map . entry :: < Bucket < M > > ( ) . or_insert_with ( Default :: default) ;
123- list. add ( msg. receiver , msg. recipient ) ;
133+ let rx = list. subscribe ( msg. receiver ) ;
134+ MessageResult ( rx)
124135 }
125136}
126137
@@ -137,31 +148,59 @@ impl<M: Message + Send + Clone + 'static> Handler<Publish<M>> for Bus where M::R
137148 fn handle ( & mut self , msg : Publish < M > , _: & mut Self :: Context ) -> Self :: Result {
138149 debug ! ( "Bus received Publish<M>" ) ;
139150 let list = self . map . entry :: < Bucket < M > > ( ) . or_insert_with ( Default :: default) ;
140- list. send ( msg. sender , msg. message ) ;
151+ list. do_send ( msg. sender , msg. message ) ;
141152 }
142153}
143154
144155
145- pub struct WaitSubscribe < M > where M : Message + Send + ' static , M :: Result : Send {
146- future : Compat < Request < Bus , Subscribe < M > > , ( ) > ,
156+ pub struct WaitSubscribe < A , M >
157+ where
158+ A : Actor ,
159+ M : Send + ' static ,
160+ {
161+ future : fut:: FutureWrap < Request < Bus , Subscribe < M > > , A > ,
147162}
148163
149- impl < M > WaitSubscribe < M > where M : Message + Send + ' static , M :: Result : Send {
150- unsafe_pinned ! ( future: Compat <Request <Bus , Subscribe <M >>, ( ) >) ;
151-
152- fn new ( addr : & Addr < Bus > , id : BusId , recipient : Recipient < M > ) -> Self {
153- WaitSubscribe { future : addr. send ( Subscribe :: new ( id, recipient) ) . compat ( ) }
164+ impl < A , M > WaitSubscribe < A , M >
165+ where
166+ A : Actor + Handler < M > ,
167+ A :: Context : AsyncContext < A > + ToEnvelope < A , M > ,
168+ M : Message + Send + ' static ,
169+ M :: Result : Send ,
170+ {
171+ fn new ( addr : & Addr < Bus > , id : BusId ) -> Self {
172+ WaitSubscribe {
173+ future : fut:: wrap_future ( addr. send ( Subscribe :: < M > :: new ( id) ) )
174+ }
154175 }
155176}
156177
157- impl < M > Unpin for WaitSubscribe < M > where M : Message + Send + ' static , M :: Result : Send { }
158-
159- impl < M > Future for WaitSubscribe < M > where M : Message + Send + ' static , M :: Result : Send {
160- type Output = Result < ( ) , MailboxError > ;
161-
162- fn poll ( mut self : PinMut < ' _ , Self > , cx : & mut std:: task:: Context < ' _ > ) -> Poll < Self :: Output > {
178+ impl < A , M > Unpin for WaitSubscribe < A , M >
179+ where
180+ A : Actor + Unpin ,
181+ M : Message + Send + ' static ,
182+ M :: Result : Send ,
183+ { }
184+
185+ impl < A , M > ActorFuture for WaitSubscribe < A , M >
186+ where
187+ A : Actor + Handler < M > ,
188+ A :: Context : AsyncContext < A > + ToEnvelope < A , M > ,
189+ M : Message + Send + Unpin + ' static ,
190+ M :: Result : Send + Unpin ,
191+ {
192+ type Item = ( ) ;
193+ type Error = MailboxError ;
194+ type Actor = A ;
195+
196+ fn poll ( & mut self , srv : & mut Self :: Actor , ctx : & mut <Self :: Actor as Actor >:: Context ) -> Poll01 < Self :: Item , Self :: Error > {
163197 debug ! ( "WaitSubscribe::poll" ) ;
164- self . future ( ) . poll ( cx)
198+ let rx = match self . future . poll ( srv, ctx) ? {
199+ Async01 :: Ready ( t) => t,
200+ Async01 :: NotReady => { return Ok ( Async01 :: NotReady ) ; }
201+ } ;
202+ ctx. add_message_stream ( rx. map ( Result :: Ok ) . compat ( TokioDefaultSpawn ) ) ;
203+ Ok ( Async01 :: Ready ( ( ) ) )
165204 }
166205}
167206
0 commit comments