File tree 2 files changed +24
-22
lines changed
2 files changed +24
-22
lines changed Original file line number Diff line number Diff line change @@ -43,8 +43,8 @@ impl Timer for TokioTimer {
43
43
}
44
44
45
45
fn reset ( & self , sleep : & mut Pin < Box < dyn Sleep > > , new_deadline : Instant ) {
46
- if sleep. downcast_ref :: < TokioSleep > ( ) . is_some ( ) {
47
- * sleep = self . sleep_until ( new_deadline) ;
46
+ if let Some ( sleep) = sleep . as_mut ( ) . downcast_mut_pin :: < TokioSleep > ( ) {
47
+ sleep. reset ( new_deadline. into ( ) )
48
48
}
49
49
}
50
50
}
@@ -81,7 +81,10 @@ impl Future for TokioSleep {
81
81
}
82
82
}
83
83
84
- // Use HasSleep to get tokio::time::Sleep to implement Unpin.
85
- // see https://docs.rs/tokio/latest/tokio/time/struct.Sleep.html
86
-
87
84
impl Sleep for TokioSleep { }
85
+
86
+ impl TokioSleep {
87
+ pub fn reset ( self : Pin < & mut Self > , deadline : Instant ) {
88
+ self . project ( ) . inner . as_mut ( ) . reset ( deadline. into ( ) ) ;
89
+ }
90
+ }
Original file line number Diff line number Diff line change 29
29
//! }
30
30
//!
31
31
//! fn reset(&self, sleep: &mut Pin<Box<dyn Sleep>>, new_deadline: Instant) {
32
- //! if sleep.downcast_ref ::<TokioSleep>().is_some () {
33
- //! * sleep = self.sleep_until (new_deadline);
32
+ //! if let Some( sleep) = sleep.as_mut().downcast_mut_pin ::<TokioSleep>() {
33
+ //! sleep.reset (new_deadline.into())
34
34
//! }
35
35
//! }
36
36
//! }
51
51
//! }
52
52
//!
53
53
//! impl Sleep for TokioSleep {}
54
+ //!
55
+ //! impl TokioSleep {
56
+ //! pub fn reset(self: Pin<&mut Self>, deadline: Instant) {
57
+ //! self.project().inner.as_mut().reset(deadline.into());
58
+ //! }
59
+ //! }
54
60
//! ````
55
61
56
62
use std:: {
@@ -97,25 +103,18 @@ impl dyn Sleep {
97
103
self . __type_id ( private:: Sealed { } ) == TypeId :: of :: < T > ( )
98
104
}
99
105
100
- /// Downcast the Sleep object to its original type
101
- pub fn downcast_ref < T > ( & self ) -> Option < & T >
102
- where
103
- T : Sleep + ' static ,
104
- {
105
- if self . is :: < T > ( ) {
106
- unsafe { Some ( & * ( self as * const dyn Sleep as * const T ) ) }
107
- } else {
108
- None
109
- }
110
- }
111
-
112
- /// Similar to `downcast_ref` but returns a mutable version instead
113
- pub fn downcast_mut < T > ( & mut self ) -> Option < & mut T >
106
+ /// Downcast a pinned &mut Sleep object to its original type
107
+ pub fn downcast_mut_pin < T > ( self : Pin < & mut Self > ) -> Option < Pin < & ' static mut T > >
114
108
where
115
109
T : Sleep + ' static ,
116
110
{
117
111
if self . is :: < T > ( ) {
118
- unsafe { Some ( & mut * ( self as * mut dyn Sleep as * mut T ) ) }
112
+ unsafe {
113
+ let inner = Pin :: into_inner_unchecked ( self ) ;
114
+ Some ( Pin :: new_unchecked (
115
+ & mut * ( & mut * inner as * mut dyn Sleep as * mut T ) ,
116
+ ) )
117
+ }
119
118
} else {
120
119
None
121
120
}
You can’t perform that action at this time.
0 commit comments