@@ -4,6 +4,7 @@ use std::sync::Arc;
44use std:: task:: { Context , Poll } ;
55
66use kv_log_macro:: trace;
7+ use pin_project_lite:: pin_project;
78
89use crate :: io;
910use crate :: task:: { JoinHandle , Task , TaskLocalsWrapper } ;
@@ -42,10 +43,7 @@ impl Builder {
4243 let tag = TaskLocalsWrapper :: new ( task. clone ( ) ) ;
4344
4445 // FIXME: do not require all futures to be boxed.
45- SupportTaskLocals {
46- tag,
47- future : Box :: pin ( future) ,
48- }
46+ SupportTaskLocals { tag, future }
4947 }
5048
5149 /// Spawns a task with the configured settings.
@@ -56,12 +54,6 @@ impl Builder {
5654 {
5755 let wrapped = self . build ( future) ;
5856
59- // Log this `spawn` operation.
60- trace ! ( "spawn" , {
61- task_id: wrapped. tag. id( ) . 0 ,
62- parent_task_id: TaskLocalsWrapper :: get_current( |t| t. id( ) . 0 ) . unwrap_or( 0 ) ,
63- } ) ;
64-
6557 let task = wrapped. tag . task ( ) . clone ( ) ;
6658 let smol_task = smol:: Task :: spawn ( wrapped) . detach ( ) ;
6759
@@ -86,25 +78,24 @@ impl Builder {
8678 }
8779}
8880
89- /// Wrapper to add support for task locals.
90- struct SupportTaskLocals < F > {
91- tag : TaskLocalsWrapper ,
92- future : Pin < Box < F > > ,
93- }
94-
95- impl < F > Drop for SupportTaskLocals < F > {
96- fn drop ( & mut self ) {
97- // Log completion on exit.
98- trace ! ( "completed" , {
99- task_id: self . tag. id( ) . 0 ,
100- } ) ;
81+ pin_project ! {
82+ /// Wrapper to add support for task locals.
83+ struct SupportTaskLocals <F > {
84+ tag: TaskLocalsWrapper ,
85+ #[ pin]
86+ future: F ,
10187 }
10288}
10389
10490impl < F : Future > Future for SupportTaskLocals < F > {
10591 type Output = F :: Output ;
10692
107- fn poll ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
108- unsafe { TaskLocalsWrapper :: set_current ( & self . tag , || Pin :: new ( & mut self . future ) . poll ( cx) ) }
93+ fn poll ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
94+ unsafe {
95+ TaskLocalsWrapper :: set_current ( & self . tag , || {
96+ let this = self . project ( ) ;
97+ this. future . poll ( cx)
98+ } )
99+ }
109100 }
110101}
0 commit comments