use crate::{Future, FutureCancel, FutureCancellation, Sender};
use std::pin::Pin;
use std::task::{Context, Poll};
#[derive(Debug)]
pub struct SyncSender<R> {
inner: Sender<R>,
}
impl<R> SyncSender<R> {
pub fn new(sender: Sender<R>) -> Self {
Self { inner: sender }
}
pub fn send(self, data: R) {
self.inner.send(data);
}
pub fn is_cancelled(&mut self) -> bool {
self.inner.is_cancelled()
}
}
unsafe impl<R: Send> Sync for SyncSender<R> {}
unsafe impl<R: Send> Send for SyncSender<R> {}
#[derive(Debug)]
pub struct SyncFuture<R> {
inner: Future<R>,
}
impl<R> SyncFuture<R> {
pub fn new(future: Future<R>) -> Self {
Self { inner: future }
}
}
impl<R> std::future::Future for SyncFuture<R> {
type Output = R;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let inner = unsafe { self.as_mut().map_unchecked_mut(|s| &mut s.inner) };
inner.poll(cx)
}
}
unsafe impl<R: Send> Sync for SyncFuture<R> {}
unsafe impl<R: Send> Send for SyncFuture<R> {}
#[derive(Debug)]
pub struct SyncFutureCancel<R, C: FutureCancellation> {
inner: FutureCancel<R, C>,
}
impl<R, C: FutureCancellation> SyncFutureCancel<R, C> {
pub fn new(future: FutureCancel<R, C>) -> Self {
Self { inner: future }
}
}
impl<R, C: FutureCancellation> std::future::Future for SyncFutureCancel<R, C> {
type Output = R;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let inner = unsafe { self.as_mut().map_unchecked_mut(|s| &mut s.inner) };
inner.poll(cx)
}
}
unsafe impl<R: Send, C: Send + FutureCancellation> Sync for SyncFutureCancel<R, C> {}
unsafe impl<R: Send, C: Send + FutureCancellation> Send for SyncFutureCancel<R, C> {}