use futures_util::AsyncRead;
use super::{AsyncPeekable, Buffer, DefaultBuffer};
use crate::grow_peek_buffer;
use std::{
future::Future,
io,
pin::Pin,
task::{Context, Poll},
};
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct PeekToString<'a, P, B = DefaultBuffer> {
peekable: &'a mut AsyncPeekable<P, B>,
buf: &'a mut String,
started: bool,
}
impl<P: Unpin, B> Unpin for PeekToString<'_, P, B> {}
impl<'a, P: AsyncRead + Unpin, B: Buffer> PeekToString<'a, P, B> {
pub(super) fn new(peekable: &'a mut AsyncPeekable<P, B>, buf: &'a mut String) -> Self {
Self {
peekable,
buf,
started: false,
}
}
}
impl<A, B> Future for PeekToString<'_, A, B>
where
A: AsyncRead + Unpin,
B: Buffer,
{
type Output = io::Result<usize>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = &mut *self;
if !this.started {
if let Err(e) = core::str::from_utf8(this.peekable.buffer.as_slice()) {
if e.error_len().is_some() {
return Poll::Ready(Err(super::invalid_utf8_io_error(e)));
}
}
this.started = true;
}
loop {
let old_len = this.peekable.buffer.len();
let loop_result: io::Result<()> = match grow_peek_buffer(&mut this.peekable.buffer) {
Err(e) => Err(e),
Ok(growth) => match Pin::new(&mut this.peekable.reader).poll_read(
cx,
&mut this.peekable.buffer.as_mut_slice()[old_len..old_len + growth],
) {
Poll::Ready(Ok(0)) => {
this.peekable.buffer.truncate(old_len);
Ok(())
}
Poll::Ready(Ok(n)) => {
this.peekable.buffer.truncate(old_len + n);
continue;
}
Poll::Ready(Err(e)) if e.kind() == io::ErrorKind::Interrupted => {
this.peekable.buffer.truncate(old_len);
continue;
}
Poll::Ready(Err(e)) => {
this.peekable.buffer.truncate(old_len);
Err(e)
}
Poll::Pending => {
this.peekable.buffer.truncate(old_len);
return Poll::Pending;
}
},
};
return Poll::Ready(
match (
loop_result,
core::str::from_utf8(this.peekable.buffer.as_slice()),
) {
(Ok(()), Ok(s)) => {
this.buf.push_str(s);
Ok(this.peekable.buffer.len())
}
(Ok(()), Err(e)) => Err(super::invalid_utf8_io_error(e)),
(Err(io), Ok(s)) => {
this.buf.push_str(s);
Err(io)
}
(Err(io), Err(utf8_err)) => {
let vut = utf8_err.valid_up_to();
if vut != 0 {
let s = core::str::from_utf8(&this.peekable.buffer.as_slice()[..vut])
.expect("valid_up_to() must point to a valid UTF-8 prefix");
this.buf.push_str(s);
}
Err(io)
}
},
);
}
}
}