Sha256: bef4cc292dd95fa9c850d0438ad0cf49a8cc4caf40a0384f763f8c9512ad9e79

Contents?: true

Size: 1.85 KB

Versions: 22

Compression:

Stored size: 1.85 KB

Contents

use super::read_to_end::read_to_end_internal;
use futures_core::future::Future;
use futures_core::ready;
use futures_core::task::{Context, Poll};
use futures_io::AsyncRead;
use std::pin::Pin;
use std::vec::Vec;
use std::{io, mem, str};

/// Future for the [`read_to_string`](super::AsyncReadExt::read_to_string) method.
#[derive(Debug)]
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct ReadToString<'a, R: ?Sized> {
    reader: &'a mut R,
    buf: &'a mut String,
    bytes: Vec<u8>,
    start_len: usize,
}

impl<R: ?Sized + Unpin> Unpin for ReadToString<'_, R> {}

impl<'a, R: AsyncRead + ?Sized + Unpin> ReadToString<'a, R> {
    pub(super) fn new(reader: &'a mut R, buf: &'a mut String) -> Self {
        let start_len = buf.len();
        Self { reader, bytes: mem::take(buf).into_bytes(), buf, start_len }
    }
}

fn read_to_string_internal<R: AsyncRead + ?Sized>(
    reader: Pin<&mut R>,
    cx: &mut Context<'_>,
    buf: &mut String,
    bytes: &mut Vec<u8>,
    start_len: usize,
) -> Poll<io::Result<usize>> {
    let ret = ready!(read_to_end_internal(reader, cx, bytes, start_len));
    if str::from_utf8(bytes).is_err() {
        Poll::Ready(ret.and_then(|_| {
            Err(io::Error::new(io::ErrorKind::InvalidData, "stream did not contain valid UTF-8"))
        }))
    } else {
        debug_assert!(buf.is_empty());
        // Safety: `bytes` is a valid UTF-8 because `str::from_utf8` returned `Ok`.
        mem::swap(unsafe { buf.as_mut_vec() }, bytes);
        Poll::Ready(ret)
    }
}

impl<A> Future for ReadToString<'_, A>
where
    A: AsyncRead + ?Sized + Unpin,
{
    type Output = io::Result<usize>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let Self { reader, buf, bytes, start_len } = &mut *self;
        read_to_string_internal(Pin::new(reader), cx, buf, bytes, *start_len)
    }
}

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
wasmtime-25.0.2 ./ext/cargo-vendor/futures-util-0.3.30/src/io/read_to_string.rs
wasmtime-25.0.1 ./ext/cargo-vendor/futures-util-0.3.30/src/io/read_to_string.rs
wasmtime-25.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/io/read_to_string.rs
wasmtime-24.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/io/read_to_string.rs
wasmtime-23.0.2 ./ext/cargo-vendor/futures-util-0.3.30/src/io/read_to_string.rs
wasmtime-22.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/io/read_to_string.rs
wasmtime-21.0.1 ./ext/cargo-vendor/futures-util-0.3.30/src/io/read_to_string.rs
wasmtime-20.0.2 ./ext/cargo-vendor/futures-util-0.3.30/src/io/read_to_string.rs
wasmtime-20.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/io/read_to_string.rs
wasmtime-18.0.3 ./ext/cargo-vendor/futures-util-0.3.30/src/io/read_to_string.rs
wasmtime-17.0.1 ./ext/cargo-vendor/futures-util-0.3.30/src/io/read_to_string.rs
wasmtime-17.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/io/read_to_string.rs
wasmtime-16.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/io/read_to_string.rs
wasmtime-15.0.1 ./ext/cargo-vendor/futures-util-0.3.30/src/io/read_to_string.rs
wasmtime-15.0.0 ./ext/cargo-vendor/futures-util-0.3.30/src/io/read_to_string.rs
wasmtime-14.0.4 ./ext/cargo-vendor/futures-util-0.3.28/src/io/read_to_string.rs
wasmtime-14.0.3 ./ext/cargo-vendor/futures-util-0.3.28/src/io/read_to_string.rs
wasmtime-14.0.1 ./ext/cargo-vendor/futures-util-0.3.28/src/io/read_to_string.rs
wasmtime-14.0.0 ./ext/cargo-vendor/futures-util-0.3.28/src/io/read_to_string.rs
wasmtime-13.0.0 ./ext/cargo-vendor/futures-util-0.3.28/src/io/read_to_string.rs