mirror of
https://github.com/async-rs/async-std.git
synced 2025-02-06 04:35:32 +00:00
feat(io): add stub for BufRead for Take
This commit is contained in:
parent
d9aec105a1
commit
dc6c8fb131
1 changed files with 36 additions and 1 deletions
|
@ -1,7 +1,7 @@
|
|||
use std::cmp;
|
||||
use std::pin::Pin;
|
||||
|
||||
use crate::io::{self, Read};
|
||||
use crate::io::{self, BufRead, Read};
|
||||
use crate::task::{Context, Poll};
|
||||
|
||||
/// Reader adaptor which limits the bytes read from an underlying reader.
|
||||
|
@ -186,6 +186,41 @@ pub fn take_read_internal<R: Read + ?Sized>(
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: BufRead + Unpin> BufRead for Take<T> {
|
||||
fn poll_fill_buf(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>> {
|
||||
// FIXME: how to get this to compile?
|
||||
unimplemented!();
|
||||
|
||||
// let Self {
|
||||
// inner,
|
||||
// limit,
|
||||
// } = &mut *self;
|
||||
|
||||
// if *limit == 0 {
|
||||
// return Poll::Ready(Ok(&[]));
|
||||
// }
|
||||
|
||||
// let rd = Pin::new(inner);
|
||||
|
||||
// match futures_core::ready!(rd.poll_fill_buf(cx)) {
|
||||
// Ok(buf) => {
|
||||
// let cap = cmp::min(buf.len() as u64, *limit) as usize;
|
||||
// Poll::Ready(Ok(&buf[..cap]))
|
||||
// }
|
||||
// Err(e) => Poll::Ready(Err(e)),
|
||||
// }
|
||||
}
|
||||
|
||||
fn consume(mut self: Pin<&mut Self>, amt: usize) {
|
||||
// Don't let callers reset the limit by passing an overlarge value
|
||||
let amt = cmp::min(amt as u64, self.limit) as usize;
|
||||
self.limit -= amt as u64;
|
||||
|
||||
let rd = Pin::new(&mut self.inner);
|
||||
rd.consume(amt);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::io;
|
||||
|
|
Loading…
Reference in a new issue