Sha256: 634531f47ebbb9e60cac801699e826d20dc495c50fd9d051e27dcff5d6b7cbcd

Contents?: true

Size: 1.07 KB

Versions: 8

Compression:

Stored size: 1.07 KB

Contents

use crate::{BufRead, ErrorType, Read};

impl ErrorType for &[u8] {
    type Error = core::convert::Infallible;
}

/// Read is implemented for `&[u8]` by copying from the slice.
///
/// Note that reading updates the slice to point to the yet unread part.
/// The slice will be empty when EOF is reached.
impl Read for &[u8] {
    #[inline]
    fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
        let amt = core::cmp::min(buf.len(), self.len());
        let (a, b) = self.split_at(amt);

        // First check if the amount of bytes we want to read is small:
        // `copy_from_slice` will generally expand to a call to `memcpy`, and
        // for a single byte the overhead is significant.
        if amt == 1 {
            buf[0] = a[0];
        } else {
            buf[..amt].copy_from_slice(a);
        }

        *self = b;
        Ok(amt)
    }
}

impl BufRead for &[u8] {
    #[inline]
    fn fill_buf(&mut self) -> Result<&[u8], Self::Error> {
        Ok(*self)
    }

    #[inline]
    fn consume(&mut self, amt: usize) {
        *self = &self[amt..];
    }
}

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
wasmtime-29.0.0 ./ext/cargo-vendor/embedded-io-0.6.1/src/impls/slice_ref.rs
wasmtime-28.0.0 ./ext/cargo-vendor/embedded-io-0.6.1/src/impls/slice_ref.rs
wasmtime-27.0.0 ./ext/cargo-vendor/embedded-io-0.6.1/src/impls/slice_ref.rs
wasmtime-26.0.0 ./ext/cargo-vendor/embedded-io-0.6.1/src/impls/slice_ref.rs
wasmtime-25.0.2 ./ext/cargo-vendor/embedded-io-0.6.1/src/impls/slice_ref.rs
wasmtime-25.0.1 ./ext/cargo-vendor/embedded-io-0.6.1/src/impls/slice_ref.rs
wasmtime-25.0.0 ./ext/cargo-vendor/embedded-io-0.6.1/src/impls/slice_ref.rs
wasmtime-24.0.0 ./ext/cargo-vendor/embedded-io-0.6.1/src/impls/slice_ref.rs