@@ -42,17 +42,31 @@ impl ContextExt for Context<'_> {
4242 Some ( extra)
4343 }
4444}
45+ pin_project_lite:: pin_project! {
46+ /// Returned [`Future`] for [`Runtime::submit`].
47+ ///
48+ /// When this is dropped and the operation hasn't finished yet, it will try to
49+ /// cancel the operation.
50+ ///
51+ /// By default, this implements `Future<Output = BufResult<usize, T>>`. If
52+ /// [`Extra`] is needed, call [`.with_extra()`] to get a `Submit<T, Extra>`
53+ /// which implements `Future<Output = (BufResult<usize, T>, Extra)>`.
54+ ///
55+ /// [`.with_extra()`]: Submit::with_extra
56+ pub struct Submit <T : OpCode , E = ( ) > {
57+ runtime: Runtime ,
58+ state: Option <State <T , E >>,
59+ }
60+
61+ impl <T : OpCode , E > PinnedDrop for Submit <T , E > {
62+ fn drop( this: Pin <& mut Self >) {
63+ let this = this. project( ) ;
64+ if let Some ( State :: Submitted { key, .. } ) = this. state. take( ) {
65+ this. runtime. cancel( key) ;
66+ }
67+ }
68+ }
4569
46- /// Return type for `Runtime::submit`
47- ///
48- /// By default, this implements `Future<Output = BufResult<usize, T>>`. If
49- /// [`Extra`] is needed, call [`.with_extra()`] to get a `Submit<T, Extra>`
50- /// which implements `Future<Output = (BufResult<usize, T>, Extra)>`.
51- ///
52- /// [`.with_extra()`]: Submit::with_extra
53- pub struct Submit < T : OpCode , E = ( ) > {
54- runtime : Runtime ,
55- state : Option < State < T , E > > ,
5670}
5771
5872enum State < T : OpCode , E > {
@@ -107,12 +121,13 @@ impl<T: OpCode + 'static> Future for Submit<T, ()> {
107121 type Output = BufResult < usize , T > ;
108122
109123 fn poll ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
110- let this = unsafe { self . get_unchecked_mut ( ) } ;
124+ let this = self . project ( ) ;
125+
111126 loop {
112127 match this. state . take ( ) . expect ( "Cannot poll after ready" ) {
113128 State :: Submitted { key, .. } => match this. runtime . poll_task ( cx. waker ( ) , key) {
114129 PushEntry :: Pending ( key) => {
115- this. state = Some ( State :: submitted ( key) ) ;
130+ * this. state = Some ( State :: submitted ( key) ) ;
116131 return Poll :: Pending ;
117132 }
118133 PushEntry :: Ready ( res) => return Poll :: Ready ( res) ,
@@ -127,7 +142,7 @@ impl<T: OpCode + 'static> Future for Submit<T, ()> {
127142 cancel. register ( & key) ;
128143 } ;
129144
130- this. state = Some ( State :: submitted ( key) )
145+ * this. state = Some ( State :: submitted ( key) )
131146 }
132147 PushEntry :: Ready ( res) => {
133148 return Poll :: Ready ( res) ;
@@ -143,13 +158,14 @@ impl<T: OpCode + 'static> Future for Submit<T, Extra> {
143158 type Output = ( BufResult < usize , T > , Extra ) ;
144159
145160 fn poll ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
146- let this = unsafe { self . get_unchecked_mut ( ) } ;
161+ let this = self . project ( ) ;
162+
147163 loop {
148164 match this. state . take ( ) . expect ( "Cannot poll after ready" ) {
149165 State :: Submitted { key, .. } => {
150166 match this. runtime . poll_task_with_extra ( cx. waker ( ) , key) {
151167 PushEntry :: Pending ( key) => {
152- this. state = Some ( State :: submitted ( key) ) ;
168+ * this. state = Some ( State :: submitted ( key) ) ;
153169 return Poll :: Pending ;
154170 }
155171 PushEntry :: Ready ( res) => return Poll :: Ready ( res) ,
@@ -163,7 +179,7 @@ impl<T: OpCode + 'static> Future for Submit<T, Extra> {
163179 cancel. register ( & key) ;
164180 }
165181
166- this. state = Some ( State :: submitted ( key) )
182+ * this. state = Some ( State :: submitted ( key) )
167183 }
168184 PushEntry :: Ready ( res) => {
169185 return Poll :: Ready ( ( res, this. runtime . default_extra ( ) ) ) ;
@@ -183,11 +199,3 @@ where
183199 self . state . is_none ( )
184200 }
185201}
186-
187- impl < T : OpCode , E > Drop for Submit < T , E > {
188- fn drop ( & mut self ) {
189- if let Some ( State :: Submitted { key, .. } ) = self . state . take ( ) {
190- self . runtime . cancel ( key) ;
191- }
192- }
193- }
0 commit comments