1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#![no_std]
extern crate alloc;
use alloc::alloc::Layout;
use alloc::boxed::Box;
use core::fmt;
use core::future::Future;
use core::mem::ManuallyDrop;
use core::pin::Pin;
use core::ptr::{self, NonNull};
use core::task::{Context, Poll};
pub struct ReusableBoxFuture<T> {
boxed: NonNull<dyn Future<Output = T> + Send>,
}
impl<T> ReusableBoxFuture<T> {
pub fn new<F>(future: F) -> Self
where
F: Future<Output = T> + Send + 'static,
{
let boxed: Box<dyn Future<Output = T> + Send> = Box::new(future);
let boxed = Box::into_raw(boxed);
let boxed = unsafe { NonNull::new_unchecked(boxed) };
Self { boxed }
}
pub fn set<F>(&mut self, future: F)
where
F: Future<Output = T> + Send + 'static,
{
if let Err(future) = self.try_set(future) {
*self = Self::new(future);
}
}
pub fn try_set<F>(&mut self, future: F) -> Result<(), F>
where
F: Future<Output = T> + Send + 'static,
{
let self_layout = {
let dyn_future: &(dyn Future<Output = T> + Send) = unsafe { self.boxed.as_ref() };
Layout::for_value(dyn_future)
};
if Layout::new::<F>() == self_layout {
unsafe {
self.set_same_layout(future);
}
Ok(())
} else {
Err(future)
}
}
unsafe fn set_same_layout<F>(&mut self, future: F)
where
F: Future<Output = T> + Send + 'static,
{
struct SetLayout<'a, F, T>
where
F: Future<Output = T> + Send + 'static,
{
rbf: &'a mut ReusableBoxFuture<T>,
new_future: ManuallyDrop<F>,
}
impl<'a, F, T> Drop for SetLayout<'a, F, T>
where
F: Future<Output = T> + Send + 'static,
{
fn drop(&mut self) {
unsafe {
let fut_ptr: *mut F = self.rbf.boxed.as_ptr() as *mut F;
ptr::write(fut_ptr, ManuallyDrop::take(&mut self.new_future));
self.rbf.boxed = NonNull::new_unchecked(fut_ptr);
}
}
}
let set_layout = SetLayout {
rbf: self,
new_future: ManuallyDrop::new(future),
};
ptr::drop_in_place(set_layout.rbf.boxed.as_ptr());
}
pub fn get_pin(&mut self) -> Pin<&mut (dyn Future<Output = T> + Send)> {
unsafe { Pin::new_unchecked(self.boxed.as_mut()) }
}
pub fn poll(&mut self, cx: &mut Context<'_>) -> Poll<T> {
self.get_pin().poll(cx)
}
}
impl<T> Future for ReusableBoxFuture<T> {
type Output = T;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T> {
Pin::into_inner(self).get_pin().poll(cx)
}
}
unsafe impl<T> Send for ReusableBoxFuture<T> {}
unsafe impl<T> Sync for ReusableBoxFuture<T> {}
impl<T> Unpin for ReusableBoxFuture<T> {}
impl<T> Drop for ReusableBoxFuture<T> {
fn drop(&mut self) {
unsafe {
drop(Box::from_raw(self.boxed.as_ptr()));
}
}
}
impl<T> fmt::Debug for ReusableBoxFuture<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ReusableBoxFuture").finish()
}
}