|
| 1 | +//! [`Future`] and [`Stream`] support for [`SendWrapper`]. |
| 2 | +//! |
| 3 | +//! [`Future`]: std::future::Future |
| 4 | +//! [`Stream`]: futures_core::Stream |
| 5 | +
|
| 6 | +use std::{ |
| 7 | + future::Future, |
| 8 | + ops::{Deref as _, DerefMut as _}, |
| 9 | + pin::Pin, |
| 10 | + task, |
| 11 | +}; |
| 12 | + |
| 13 | +use futures_core::Stream; |
| 14 | + |
| 15 | +use super::SendWrapper; |
| 16 | + |
| 17 | +const POLL_ERROR: &'static str = |
| 18 | + "Polling SendWrapper<T> variable from a thread different to the one it has been created with."; |
| 19 | + |
| 20 | +impl<F: Future> Future for SendWrapper<F> { |
| 21 | + type Output = F::Output; |
| 22 | + |
| 23 | + /// Polls this [`SendWrapper`] [`Future`]. |
| 24 | + /// |
| 25 | + /// # Panics |
| 26 | + /// Polling panics if it is done from a different thread than the one the [`SendWrapper`] |
| 27 | + /// instance has been created with. |
| 28 | + fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> task::Poll<Self::Output> { |
| 29 | + if !self.valid() { |
| 30 | + panic!(POLL_ERROR); |
| 31 | + } |
| 32 | + // This is safe as `SendWrapper` itself points to the inner `Future`. |
| 33 | + // So, as long as `SendWrapper` is pinned, the inner `Future` is pinned too. |
| 34 | + unsafe { self.map_unchecked_mut(Self::deref_mut) }.poll(cx) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl<S: Stream> Stream for SendWrapper<S> { |
| 39 | + type Item = S::Item; |
| 40 | + |
| 41 | + /// Polls this [`SendWrapper`] [`Stream`]. |
| 42 | + /// |
| 43 | + /// # Panics |
| 44 | + /// Polling panics if it is done from a different thread than the one the [`SendWrapper`] |
| 45 | + /// instance has been created with. |
| 46 | + fn poll_next( |
| 47 | + self: Pin<&mut Self>, |
| 48 | + cx: &mut task::Context<'_>, |
| 49 | + ) -> task::Poll<Option<Self::Item>> { |
| 50 | + if !self.valid() { |
| 51 | + panic!(POLL_ERROR); |
| 52 | + } |
| 53 | + // This is safe as `SendWrapper` itself points to the inner `Stream`. |
| 54 | + // So, as long as `SendWrapper` is pinned, the inner `Stream` is pinned too. |
| 55 | + unsafe { self.map_unchecked_mut(Self::deref_mut) }.poll_next(cx) |
| 56 | + } |
| 57 | + |
| 58 | + #[inline] |
| 59 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 60 | + self.deref().size_hint() |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +#[cfg(test)] |
| 65 | +mod tests { |
| 66 | + use std::thread; |
| 67 | + |
| 68 | + use futures_executor as executor; |
| 69 | + use futures_util::{future, stream, StreamExt}; |
| 70 | + |
| 71 | + use crate::SendWrapper; |
| 72 | + |
| 73 | + #[test] |
| 74 | + fn test_future() { |
| 75 | + let w1 = SendWrapper::new(future::ready(42)); |
| 76 | + let w2 = w1.clone(); |
| 77 | + assert_eq!( |
| 78 | + format!("{:?}", executor::block_on(w1)), |
| 79 | + format!("{:?}", executor::block_on(w2)), |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn test_future_panic() { |
| 85 | + let w = SendWrapper::new(future::ready(42)); |
| 86 | + let t = thread::spawn(move || executor::block_on(w)); |
| 87 | + assert!(t.join().is_err()); |
| 88 | + } |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn test_stream() { |
| 92 | + let mut w1 = SendWrapper::new(stream::once(future::ready(42))); |
| 93 | + let mut w2 = SendWrapper::new(stream::once(future::ready(42))); |
| 94 | + assert_eq!( |
| 95 | + format!("{:?}", executor::block_on(w1.next())), |
| 96 | + format!("{:?}", executor::block_on(w2.next())), |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + #[test] |
| 101 | + fn test_stream_panic() { |
| 102 | + let mut w = SendWrapper::new(stream::once(future::ready(42))); |
| 103 | + let t = thread::spawn(move || executor::block_on(w.next())); |
| 104 | + assert!(t.join().is_err()); |
| 105 | + } |
| 106 | +} |
0 commit comments