1use 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};
89/// A reusable `Pin<Box<dyn Future<Output = T> + Send>>`.
10///
11/// This type lets you replace the future stored in the box without
12/// reallocating when the size and alignment permits this.
13pub(crate) struct ReusableBoxFuture<T> {
14 boxed: NonNull<dyn Future<Output = T> + Send>,
15}
1617impl<T> ReusableBoxFuture<T> {
18/// Create a new `ReusableBoxFuture<T>` containing the provided future.
19pub(crate) fn new<F>(future: F) -> Self
20where
21F: Future<Output = T> + Send + 'static,
22 {
23let boxed: Box<dyn Future<Output = T> + Send> = Box::new(future);
2425let boxed = Box::into_raw(boxed);
2627// SAFETY: Box::into_raw does not return null pointers.
28let boxed = unsafe { NonNull::new_unchecked(boxed) };
2930Self { boxed }
31 }
3233/// Replaces the future currently stored in this box.
34 ///
35 /// This reallocates if and only if the layout of the provided future is
36 /// different from the layout of the currently stored future.
37pub(crate) fn set<F>(&mut self, future: F)
38where
39F: Future<Output = T> + Send + 'static,
40 {
41if let Err(future) = self.try_set(future) {
42*self = Self::new(future);
43 }
44 }
4546/// Replaces the future currently stored in this box.
47 ///
48 /// This function never reallocates, but returns an error if the provided
49 /// future has a different size or alignment from the currently stored
50 /// future.
51pub(crate) fn try_set<F>(&mut self, future: F) -> Result<(), F>
52where
53F: Future<Output = T> + Send + 'static,
54 {
55// SAFETY: The pointer is not dangling.
56let self_layout = {
57let dyn_future: &(dyn Future<Output = T> + Send) = unsafe { self.boxed.as_ref() };
58 Layout::for_value(dyn_future)
59 };
6061if Layout::new::<F>() == self_layout {
62// SAFETY: We just checked that the layout of F is correct.
63unsafe {
64self.set_same_layout(future);
65 }
6667Ok(())
68 } else {
69Err(future)
70 }
71 }
7273/// Sets the current future.
74 ///
75 /// # Safety
76 ///
77 /// This function requires that the layout of the provided future is the
78 /// same as `self.layout`.
79unsafe fn set_same_layout<F>(&mut self, future: F)
80where
81F: Future<Output = T> + Send + 'static,
82 {
83// Drop the existing future, catching any panics.
84let result = panic::catch_unwind(AssertUnwindSafe(|| {
85 ptr::drop_in_place(self.boxed.as_ptr());
86 }));
8788// Overwrite the future behind the pointer. This is safe because the
89 // allocation was allocated with the same size and alignment as the type F.
90let self_ptr: *mut F = self.boxed.as_ptr() as *mut F;
91 ptr::write(self_ptr, future);
9293// Update the vtable of self.boxed. The pointer is not null because we
94 // just got it from self.boxed, which is not null.
95self.boxed = NonNull::new_unchecked(self_ptr);
9697// If the old future's destructor panicked, resume unwinding.
98match result {
99Ok(()) => {}
100Err(payload) => {
101 panic::resume_unwind(payload);
102 }
103 }
104 }
105106/// Gets a pinned reference to the underlying future.
107pub(crate) fn get_pin(&mut self) -> Pin<&mut (dyn Future<Output = T> + Send)> {
108// SAFETY: The user of this box cannot move the box, and we do not move it
109 // either.
110unsafe { Pin::new_unchecked(self.boxed.as_mut()) }
111 }
112113/// Polls the future stored inside this box.
114pub(crate) fn poll(&mut self, cx: &mut Context<'_>) -> Poll<T> {
115self.get_pin().poll(cx)
116 }
117}
118119impl<T> Future for ReusableBoxFuture<T> {
120type Output = T;
121122/// Polls the future stored inside this box.
123fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T> {
124 Pin::into_inner(self).get_pin().poll(cx)
125 }
126}
127128// The future stored inside ReusableBoxFuture<T> must be Send.
129unsafe impl<T> Send for ReusableBoxFuture<T> {}
130131// The only method called on self.boxed is poll, which takes &mut self, so this
132// struct being Sync does not permit any invalid access to the Future, even if
133// the future is not Sync.
134unsafe impl<T> Sync for ReusableBoxFuture<T> {}
135136// Just like a Pin<Box<dyn Future>> is always Unpin, so is this type.
137impl<T> Unpin for ReusableBoxFuture<T> {}
138139impl<T> Drop for ReusableBoxFuture<T> {
140fn drop(&mut self) {
141unsafe {
142 drop(Box::from_raw(self.boxed.as_ptr()));
143 }
144 }
145}
146147impl<T> fmt::Debug for ReusableBoxFuture<T> {
148fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
149 f.debug_struct("ReusableBoxFuture").finish()
150 }
151}
152153#[cfg(test)]
154mod test {
155use super::ReusableBoxFuture;
156use futures::future::FutureExt;
157use std::alloc::Layout;
158use std::future::Future;
159use std::pin::Pin;
160use std::task::{Context, Poll};
161162#[test]
163fn test_different_futures() {
164let fut = async move { 10 };
165// Not zero sized!
166assert_eq!(Layout::for_value(&fut).size(), 1);
167168let mut b = ReusableBoxFuture::new(fut);
169170assert_eq!(b.get_pin().now_or_never(), Some(10));
171172 b.try_set(async move { 20 })
173 .unwrap_or_else(|_| panic!("incorrect size"));
174175assert_eq!(b.get_pin().now_or_never(), Some(20));
176177 b.try_set(async move { 30 })
178 .unwrap_or_else(|_| panic!("incorrect size"));
179180assert_eq!(b.get_pin().now_or_never(), Some(30));
181 }
182183#[test]
184fn test_different_sizes() {
185let fut1 = async move { 10 };
186let val = [0u32; 1000];
187let fut2 = async move { val[0] };
188let fut3 = ZeroSizedFuture {};
189190assert_eq!(Layout::for_value(&fut1).size(), 1);
191assert_eq!(Layout::for_value(&fut2).size(), 4004);
192assert_eq!(Layout::for_value(&fut3).size(), 0);
193194let mut b = ReusableBoxFuture::new(fut1);
195assert_eq!(b.get_pin().now_or_never(), Some(10));
196 b.set(fut2);
197assert_eq!(b.get_pin().now_or_never(), Some(0));
198 b.set(fut3);
199assert_eq!(b.get_pin().now_or_never(), Some(5));
200 }
201202struct ZeroSizedFuture {}
203impl Future for ZeroSizedFuture {
204type Output = u32;
205fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<u32> {
206 Poll::Ready(5)
207 }
208 }
209210#[test]
211fn test_zero_sized() {
212let fut = ZeroSizedFuture {};
213// Zero sized!
214assert_eq!(Layout::for_value(&fut).size(), 0);
215216let mut b = ReusableBoxFuture::new(fut);
217218assert_eq!(b.get_pin().now_or_never(), Some(5));
219assert_eq!(b.get_pin().now_or_never(), Some(5));
220221 b.try_set(ZeroSizedFuture {})
222 .unwrap_or_else(|_| panic!("incorrect size"));
223224assert_eq!(b.get_pin().now_or_never(), Some(5));
225assert_eq!(b.get_pin().now_or_never(), Some(5));
226 }
227}