tokio/signal/
reusable_box.rs1use std::alloc::Layout;
2use std::future::Future;
3use std::panic::AssertUnwindSafe;
4use std::pin::Pin;
5use std::ptr::{self, NonNull};
6use std::task::{Context, Poll};
7use std::{fmt, panic};
8
9pub(crate) struct ReusableBoxFuture<T> {
14 boxed: NonNull<dyn Future<Output = T> + Send>,
15}
16
17impl<T> ReusableBoxFuture<T> {
18 pub(crate) fn new<F>(future: F) -> Self
20 where
21 F: Future<Output = T> + Send + 'static,
22 {
23 let boxed: Box<dyn Future<Output = T> + Send> = Box::new(future);
24
25 let boxed = Box::into_raw(boxed);
26
27 let boxed = unsafe { NonNull::new_unchecked(boxed) };
29
30 Self { boxed }
31 }
32
33 pub(crate) fn set<F>(&mut self, future: F)
38 where
39 F: Future<Output = T> + Send + 'static,
40 {
41 if let Err(future) = self.try_set(future) {
42 *self = Self::new(future);
43 }
44 }
45
46 pub(crate) fn try_set<F>(&mut self, future: F) -> Result<(), F>
52 where
53 F: Future<Output = T> + Send + 'static,
54 {
55 let self_layout = {
57 let dyn_future: &(dyn Future<Output = T> + Send) = unsafe { self.boxed.as_ref() };
58 Layout::for_value(dyn_future)
59 };
60
61 if Layout::new::<F>() == self_layout {
62 unsafe {
64 self.set_same_layout(future);
65 }
66
67 Ok(())
68 } else {
69 Err(future)
70 }
71 }
72
73 unsafe fn set_same_layout<F>(&mut self, future: F)
80 where
81 F: Future<Output = T> + Send + 'static,
82 {
83 let result = panic::catch_unwind(AssertUnwindSafe(|| unsafe {
85 ptr::drop_in_place(self.boxed.as_ptr());
86 }));
87
88 let self_ptr: *mut F = self.boxed.as_ptr() as *mut F;
91 unsafe {
93 ptr::write(self_ptr, future);
94 }
95
96 self.boxed = unsafe { NonNull::new_unchecked(self_ptr) };
99
100 match result {
102 Ok(()) => {}
103 Err(payload) => {
104 panic::resume_unwind(payload);
105 }
106 }
107 }
108
109 pub(crate) fn get_pin(&mut self) -> Pin<&mut (dyn Future<Output = T> + Send)> {
111 unsafe { Pin::new_unchecked(self.boxed.as_mut()) }
114 }
115
116 pub(crate) fn poll(&mut self, cx: &mut Context<'_>) -> Poll<T> {
118 self.get_pin().poll(cx)
119 }
120}
121
122impl<T> Future for ReusableBoxFuture<T> {
123 type Output = T;
124
125 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T> {
127 Pin::into_inner(self).get_pin().poll(cx)
128 }
129}
130
131unsafe impl<T> Send for ReusableBoxFuture<T> {}
133
134unsafe impl<T> Sync for ReusableBoxFuture<T> {}
138
139impl<T> Unpin for ReusableBoxFuture<T> {}
141
142impl<T> Drop for ReusableBoxFuture<T> {
143 fn drop(&mut self) {
144 unsafe {
145 drop(Box::from_raw(self.boxed.as_ptr()));
146 }
147 }
148}
149
150impl<T> fmt::Debug for ReusableBoxFuture<T> {
151 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
152 f.debug_struct("ReusableBoxFuture").finish()
153 }
154}
155
156#[cfg(test)]
157mod test {
158 use super::ReusableBoxFuture;
159 use futures::future::FutureExt;
160 use std::alloc::Layout;
161 use std::future::Future;
162 use std::pin::Pin;
163 use std::task::{Context, Poll};
164
165 #[test]
166 fn test_different_futures() {
167 let fut = async move { 10 };
168 assert_eq!(Layout::for_value(&fut).size(), 1);
170
171 let mut b = ReusableBoxFuture::new(fut);
172
173 assert_eq!(b.get_pin().now_or_never(), Some(10));
174
175 b.try_set(async move { 20 })
176 .unwrap_or_else(|_| panic!("incorrect size"));
177
178 assert_eq!(b.get_pin().now_or_never(), Some(20));
179
180 b.try_set(async move { 30 })
181 .unwrap_or_else(|_| panic!("incorrect size"));
182
183 assert_eq!(b.get_pin().now_or_never(), Some(30));
184 }
185
186 #[test]
187 fn test_different_sizes() {
188 let fut1 = async move { 10 };
189 let val = [0u32; 1000];
190 let fut2 = async move { val[0] };
191 let fut3 = ZeroSizedFuture {};
192
193 assert_eq!(Layout::for_value(&fut1).size(), 1);
194 assert_eq!(Layout::for_value(&fut2).size(), 4004);
195 assert_eq!(Layout::for_value(&fut3).size(), 0);
196
197 let mut b = ReusableBoxFuture::new(fut1);
198 assert_eq!(b.get_pin().now_or_never(), Some(10));
199 b.set(fut2);
200 assert_eq!(b.get_pin().now_or_never(), Some(0));
201 b.set(fut3);
202 assert_eq!(b.get_pin().now_or_never(), Some(5));
203 }
204
205 struct ZeroSizedFuture {}
206 impl Future for ZeroSizedFuture {
207 type Output = u32;
208 fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<u32> {
209 Poll::Ready(5)
210 }
211 }
212
213 #[test]
214 fn test_zero_sized() {
215 let fut = ZeroSizedFuture {};
216 assert_eq!(Layout::for_value(&fut).size(), 0);
218
219 let mut b = ReusableBoxFuture::new(fut);
220
221 assert_eq!(b.get_pin().now_or_never(), Some(5));
222 assert_eq!(b.get_pin().now_or_never(), Some(5));
223
224 b.try_set(ZeroSizedFuture {})
225 .unwrap_or_else(|_| panic!("incorrect size"));
226
227 assert_eq!(b.get_pin().now_or_never(), Some(5));
228 assert_eq!(b.get_pin().now_or_never(), Some(5));
229 }
230}