|
| 1 | +use futures_io::AsyncWrite; |
| 2 | +use futures_sink::Sink; |
| 3 | +use std::{ |
| 4 | + io::{self, IoSlice}, |
| 5 | + pin::Pin, |
| 6 | + task::{Context, Poll}, |
| 7 | +}; |
| 8 | + |
| 9 | +/// Async wrapper that tracks whether it has been closed. |
| 10 | +/// |
| 11 | +/// See the `track_closed` methods on: |
| 12 | +/// * [`SinkTestExt`](crate::sink::SinkTestExt::track_closed) |
| 13 | +/// * [`AsyncWriteTestExt`](crate::io::AsyncWriteTestExt::track_closed) |
| 14 | +#[pin_project::pin_project] |
| 15 | +#[derive(Debug)] |
| 16 | +pub struct TrackClosed<T> { |
| 17 | + #[pin] |
| 18 | + inner: T, |
| 19 | + closed: bool, |
| 20 | +} |
| 21 | + |
| 22 | +impl<T> TrackClosed<T> { |
| 23 | + pub(crate) fn new(inner: T) -> TrackClosed<T> { |
| 24 | + TrackClosed { |
| 25 | + inner, |
| 26 | + closed: false, |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + /// Check whether this object has been closed. |
| 31 | + pub fn is_closed(&self) -> bool { |
| 32 | + self.closed |
| 33 | + } |
| 34 | + |
| 35 | + /// Acquires a reference to the underlying object that this adaptor is |
| 36 | + /// wrapping. |
| 37 | + pub fn get_ref(&self) -> &T { |
| 38 | + &self.inner |
| 39 | + } |
| 40 | + |
| 41 | + /// Acquires a mutable reference to the underlying object that this |
| 42 | + /// adaptor is wrapping. |
| 43 | + pub fn get_mut(&mut self) -> &mut T { |
| 44 | + &mut self.inner |
| 45 | + } |
| 46 | + |
| 47 | + /// Acquires a pinned mutable reference to the underlying object that |
| 48 | + /// this adaptor is wrapping. |
| 49 | + pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut T> { |
| 50 | + self.project().inner |
| 51 | + } |
| 52 | + |
| 53 | + /// Consumes this adaptor returning the underlying object. |
| 54 | + pub fn into_inner(self) -> T { |
| 55 | + self.inner |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +impl<T: AsyncWrite> AsyncWrite for TrackClosed<T> { |
| 60 | + fn poll_write( |
| 61 | + self: Pin<&mut Self>, |
| 62 | + cx: &mut Context<'_>, |
| 63 | + buf: &[u8], |
| 64 | + ) -> Poll<io::Result<usize>> { |
| 65 | + if self.is_closed() { |
| 66 | + return Poll::Ready(Err(io::Error::new( |
| 67 | + io::ErrorKind::Other, |
| 68 | + "Attempted to write after stream was closed", |
| 69 | + ))); |
| 70 | + } |
| 71 | + self.project().inner.poll_write(cx, buf) |
| 72 | + } |
| 73 | + |
| 74 | + fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { |
| 75 | + if self.is_closed() { |
| 76 | + return Poll::Ready(Err(io::Error::new( |
| 77 | + io::ErrorKind::Other, |
| 78 | + "Attempted to flush after stream was closed", |
| 79 | + ))); |
| 80 | + } |
| 81 | + assert!(!self.is_closed()); |
| 82 | + self.project().inner.poll_flush(cx) |
| 83 | + } |
| 84 | + |
| 85 | + fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { |
| 86 | + if self.is_closed() { |
| 87 | + return Poll::Ready(Err(io::Error::new( |
| 88 | + io::ErrorKind::Other, |
| 89 | + "Attempted to close after stream was closed", |
| 90 | + ))); |
| 91 | + } |
| 92 | + let this = self.project(); |
| 93 | + match this.inner.poll_close(cx) { |
| 94 | + Poll::Ready(Ok(())) => { |
| 95 | + *this.closed = true; |
| 96 | + Poll::Ready(Ok(())) |
| 97 | + } |
| 98 | + other => other, |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + fn poll_write_vectored( |
| 103 | + self: Pin<&mut Self>, |
| 104 | + cx: &mut Context<'_>, |
| 105 | + bufs: &[IoSlice<'_>], |
| 106 | + ) -> Poll<io::Result<usize>> { |
| 107 | + if self.is_closed() { |
| 108 | + return Poll::Ready(Err(io::Error::new( |
| 109 | + io::ErrorKind::Other, |
| 110 | + "Attempted to write after stream was closed", |
| 111 | + ))); |
| 112 | + } |
| 113 | + self.project().inner.poll_write_vectored(cx, bufs) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +impl<Item, T: Sink<Item>> Sink<Item> for TrackClosed<T> { |
| 118 | + type Error = T::Error; |
| 119 | + |
| 120 | + fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { |
| 121 | + assert!(!self.is_closed()); |
| 122 | + self.project().inner.poll_ready(cx) |
| 123 | + } |
| 124 | + |
| 125 | + fn start_send(self: Pin<&mut Self>, item: Item) -> Result<(), Self::Error> { |
| 126 | + assert!(!self.is_closed()); |
| 127 | + self.project().inner.start_send(item) |
| 128 | + } |
| 129 | + |
| 130 | + fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { |
| 131 | + assert!(!self.is_closed()); |
| 132 | + self.project().inner.poll_flush(cx) |
| 133 | + } |
| 134 | + |
| 135 | + fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { |
| 136 | + assert!(!self.is_closed()); |
| 137 | + let this = self.project(); |
| 138 | + match this.inner.poll_close(cx) { |
| 139 | + Poll::Ready(Ok(())) => { |
| 140 | + *this.closed = true; |
| 141 | + Poll::Ready(Ok(())) |
| 142 | + } |
| 143 | + other => other, |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments