1use futures_util::future::Future;
7use pin_project::pin_project;
8use std::fmt::Debug;
9use std::fmt::Display;
10use std::pin::Pin;
11use std::task::Context;
12use std::task::Poll;
13
14#[derive(Debug)]
15#[pin_project]
16pub struct SendFuture<F> {
23 #[pin]
24 inner: F,
25}
26
27impl<F> SendFuture<F> {
28 pub fn new(inner: F) -> Self {
29 Self { inner }
30 }
31}
32
33unsafe impl<F> Send for SendFuture<F> {}
34unsafe impl<F> Sync for SendFuture<F> {}
35
36impl<F: Future> Future for SendFuture<F> {
37 type Output = F::Output;
38
39 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
40 let this = self.project();
41 this.inner.poll(cx)
42 }
43}
44
45pub trait IntoSendFuture {
52 type Output;
53 fn into_send(self) -> SendFuture<Self>
54 where
55 Self: Sized;
56}
57
58impl<F, T> IntoSendFuture for F
59where
60 F: Future<Output = T>,
61{
62 type Output = T;
63 fn into_send(self) -> SendFuture<Self> {
64 SendFuture::new(self)
65 }
66}
67
68pub struct SendWrapper<T>(pub T);
75
76unsafe impl<T> Send for SendWrapper<T> {}
77unsafe impl<T> Sync for SendWrapper<T> {}
78
79impl<T> SendWrapper<T> {
80 pub fn new(inner: T) -> Self {
81 Self(inner)
82 }
83}
84
85impl<T> std::ops::Deref for SendWrapper<T> {
86 type Target = T;
87
88 fn deref(&self) -> &Self::Target {
89 &self.0
90 }
91}
92
93impl<T> std::ops::DerefMut for SendWrapper<T> {
94 fn deref_mut(&mut self) -> &mut Self::Target {
95 &mut self.0
96 }
97}
98
99impl<T: Debug> Debug for SendWrapper<T> {
100 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
101 write!(f, "SendWrapper({:?})", self.0)
102 }
103}
104
105impl<T: Clone> Clone for SendWrapper<T> {
106 fn clone(&self) -> Self {
107 Self(self.0.clone())
108 }
109}
110
111impl<T: Default> Default for SendWrapper<T> {
112 fn default() -> Self {
113 Self(T::default())
114 }
115}
116
117impl<T: Display> Display for SendWrapper<T> {
118 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
119 write!(f, "SendWrapper({})", self.0)
120 }
121}