impl<R: AsyncRead> Take<R> { /// Returns the remaining number of bytes that can be /// read before this instance will return EOF. /// /// # Note /// /// This instance may reach `EOF` after reading fewer bytes than indicated by /// this method if the underlying [`AsyncRead`] instance reaches EOF. pubfn limit(&self) -> u64 { self.limit_
}
/// Sets the number of bytes that can be read before this instance will /// return EOF. This is the same as constructing a new `Take` instance, so /// the amount of bytes read and the previous limit value don't matter when /// calling this method. pubfn set_limit(&mutself, limit: u64) { self.limit_ = limit;
}
/// Gets a reference to the underlying reader. pubfn get_ref(&self) -> &R {
&self.inner
}
/// Gets a mutable reference to the underlying reader. /// /// Care should be taken to avoid modifying the internal I/O state of the /// underlying reader as doing so may corrupt the internal limit of this /// `Take`. pubfn get_mut(&mutself) -> &mut R {
&mutself.inner
}
/// Gets a pinned mutable reference to the underlying reader. /// /// Care should be taken to avoid modifying the internal I/O state of the /// underlying reader as doing so may corrupt the internal limit of this /// `Take`. pubfn get_pin_mut(self: Pin<&mutSelf>) -> Pin<&mut R> { self.project().inner
}
/// Consumes the `Take`, returning the wrapped reader. pubfn into_inner(self) -> R { self.inner
}
}
let me = self.project(); letmut b = buf.take(usize::try_from(*me.limit_).unwrap_or(usize::MAX));
let buf_ptr = b.filled().as_ptr();
ready!(me.inner.poll_read(cx, &mut b))?;
assert_eq!(b.filled().as_ptr(), buf_ptr);
let n = b.filled().len();
// We need to update the original ReadBuf unsafe {
buf.assume_init(n);
}
buf.advance(n);
*me.limit_ -= n as u64;
Poll::Ready(Ok(()))
}
}
impl<R: AsyncBufRead> AsyncBufRead for Take<R> { fn poll_fill_buf(self: Pin<&mutSelf>, cx: &>mut Context<'_>) -> Poll<io::Result<&[u8]>> { let me = self.project();
// Don't call into inner reader at all at EOF because it may still block if *me.limit_ == 0 { return Poll::Ready(Ok(&[]));
}
let buf = ready!(me.inner.poll_fill_buf(cx)?); let cap = cmp::min(buf.len() as u64, *me.limit_) as usize;
Poll::Ready(Ok(&buf[..cap]))
}
fn consume(self: Pin<&mutSelf>, amt: usize) { let me = self.project(); // Don't let callers reset the limit by passing an overlarge value let amt = cmp::min(amt as u64, *me.limit_) as usize;
*me.limit_ -= amt as u64;
me.inner.consume(amt);
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.