reusable_box_future/
local_box_future.rs1use alloc::alloc::Layout;
2use alloc::boxed::Box;
3use core::fmt;
4use core::future::Future;
5use core::mem::ManuallyDrop;
6use core::pin::Pin;
7use core::ptr::{self, NonNull};
8use core::task::{Context, Poll};
9
10pub struct ReusableLocalBoxFuture<T> {
15 boxed: NonNull<dyn Future<Output = T>>,
16}
17
18impl<T> ReusableLocalBoxFuture<T> {
19 pub fn new<F>(future: F) -> Self
21 where
22 F: Future<Output = T> + 'static,
23 {
24 let boxed: Box<dyn Future<Output = T>> = Box::new(future);
25 let boxed = Box::into_raw(boxed);
26
27 let boxed = unsafe { NonNull::new_unchecked(boxed) };
29
30 Self { boxed }
31 }
32
33 pub fn set<F>(&mut self, future: F)
38 where
39 F: Future<Output = T> + 'static,
40 {
41 if let Err(future) = self.try_set(future) {
42 *self = Self::new(future);
43 }
44 }
45
46 pub fn try_set<F>(&mut self, future: F) -> Result<(), F>
52 where
53 F: Future<Output = T> + 'static,
54 {
55 let self_layout = {
57 let dyn_future: &(dyn Future<Output = T>) = 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> + 'static,
82 {
83 struct SetLayout<'a, F, T>
84 where
85 F: Future<Output = T> + 'static,
86 {
87 rbf: &'a mut ReusableLocalBoxFuture<T>,
88 new_future: ManuallyDrop<F>,
89 }
90
91 impl<'a, F, T> Drop for SetLayout<'a, F, T>
92 where
93 F: Future<Output = T> + 'static,
94 {
95 fn drop(&mut self) {
96 unsafe {
101 let fut_ptr: *mut F = self.rbf.boxed.as_ptr() as *mut F;
104 ptr::write(fut_ptr, ManuallyDrop::take(&mut self.new_future));
105
106 self.rbf.boxed = NonNull::new_unchecked(fut_ptr);
109 }
110 }
111 }
112
113 let set_layout = SetLayout {
114 rbf: self,
115 new_future: ManuallyDrop::new(future),
116 };
117
118 ptr::drop_in_place(set_layout.rbf.boxed.as_ptr());
120 }
122
123 pub fn get_pin(&mut self) -> Pin<&mut (dyn Future<Output = T>)> {
125 unsafe { Pin::new_unchecked(self.boxed.as_mut()) }
128 }
129
130 pub fn poll(&mut self, cx: &mut Context<'_>) -> Poll<T> {
132 self.get_pin().poll(cx)
133 }
134}
135
136impl<T> Future for ReusableLocalBoxFuture<T> {
137 type Output = T;
138
139 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<T> {
141 Pin::into_inner(self).get_pin().poll(cx)
142 }
143}
144
145impl<T> Unpin for ReusableLocalBoxFuture<T> {}
147
148impl<T> Drop for ReusableLocalBoxFuture<T> {
149 fn drop(&mut self) {
150 unsafe {
151 drop(Box::from_raw(self.boxed.as_ptr()));
152 }
153 }
154}
155
156impl<T> fmt::Debug for ReusableLocalBoxFuture<T> {
157 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
158 f.debug_struct("ReusableLocalBoxFuture").finish()
159 }
160}
161
162#[cfg(test)]
163mod tests {
164 use super::*;
165 use static_assertions::{assert_impl_all, assert_not_impl_all};
166
167 #[test]
168 fn static_assertion() {
169 assert_impl_all!(ReusableLocalBoxFuture<()>: Unpin);
170 assert_not_impl_all!(ReusableLocalBoxFuture<()>: Sync, Send);
171 }
172}