@@ -29,23 +29,53 @@ use tokio::sync::oneshot;
2929/// background, even if this future is dropped or never awaited.
3030#[ derive( Debug ) ]
3131pub struct AppendFuture {
32- rx : oneshot:: Receiver < AppendResult < AppendResponse > > ,
32+ inner : Inner ,
33+ }
34+
35+ enum Inner {
36+ Rx ( oneshot:: Receiver < AppendResult < AppendResponse > > ) ,
37+ Boxed ( Pin < Box < dyn Future < Output = AppendResult < AppendResponse > > + Send > > ) ,
38+ }
39+
40+ impl std:: fmt:: Debug for Inner {
41+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
42+ match self {
43+ Self :: Rx ( rx) => f. debug_tuple ( "Rx" ) . field ( rx) . finish ( ) ,
44+ Self :: Boxed ( _) => f. debug_tuple ( "Boxed" ) . finish ( ) ,
45+ }
46+ }
3347}
3448
3549impl AppendFuture {
3650 pub ( crate ) fn new ( rx : oneshot:: Receiver < AppendResult < AppendResponse > > ) -> Self {
37- Self { rx }
51+ Self {
52+ inner : Inner :: Rx ( rx) ,
53+ }
54+ }
55+
56+ pub ( crate ) fn from_future < F > ( fut : F ) -> Self
57+ where
58+ F : Future < Output = AppendResult < AppendResponse > > + Send + ' static ,
59+ {
60+ Self {
61+ inner : Inner :: Boxed ( Box :: pin ( fut) ) ,
62+ }
3863 }
3964}
4065
4166impl Future for AppendFuture {
4267 type Output = AppendResult < AppendResponse > ;
4368
4469 fn poll ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
45- let result = std:: task:: ready!( Pin :: new( & mut self . rx) . poll( cx) ) ;
46- match result {
47- Ok ( res) => Poll :: Ready ( res) ,
48- Err ( _) => Poll :: Ready ( Err ( AppendError :: UnexpectedEndOfStream ) ) ,
70+ match & mut self . inner {
71+ Inner :: Rx ( rx) => {
72+ let result = std:: task:: ready!( Pin :: new( rx) . poll( cx) ) ;
73+ match result {
74+ Ok ( res) => Poll :: Ready ( res) ,
75+ Err ( _) => Poll :: Ready ( Err ( AppendError :: UnexpectedEndOfStream ) ) ,
76+ }
77+ }
78+ Inner :: Boxed ( fut) => fut. as_mut ( ) . poll ( cx) ,
4979 }
5080 }
5181}
@@ -89,4 +119,33 @@ mod tests {
89119 let err = future. await . expect_err ( "should return error from task" ) ;
90120 assert ! ( matches!( err, AppendError :: UnexpectedEndOfStream ) ) ;
91121 }
122+
123+ #[ tokio:: test]
124+ async fn from_future_success ( ) {
125+ let future = AppendFuture :: from_future ( async {
126+ Ok ( AppendResponse {
127+ offset : Some ( 7 ) ,
128+ updated_schema : None ,
129+ } )
130+ } ) ;
131+ let resp = future. await . expect ( "should succeed" ) ;
132+ assert_eq ! ( resp. offset, Some ( 7 ) ) ;
133+ }
134+
135+ #[ tokio:: test]
136+ async fn from_future_error ( ) {
137+ let future = AppendFuture :: from_future ( async { Err ( AppendError :: UnexpectedEndOfStream ) } ) ;
138+ let err = future. await . expect_err ( "should return error" ) ;
139+ assert ! ( matches!( err, AppendError :: UnexpectedEndOfStream ) ) ;
140+ }
141+
142+ #[ test]
143+ fn debug_format ( ) {
144+ let ( _, rx) = oneshot:: channel ( ) ;
145+ let future_rx = AppendFuture :: new ( rx) ;
146+ assert ! ( format!( "{future_rx:?}" ) . contains( "Rx" ) ) ;
147+
148+ let future_boxed = AppendFuture :: from_future ( async { Ok ( AppendResponse :: default ( ) ) } ) ;
149+ assert ! ( format!( "{future_boxed:?}" ) . contains( "Boxed" ) ) ;
150+ }
92151}
0 commit comments