o3 0.62.0

shared-nothing primitives
Documentation
//! Allocation-free traversal over immutable scattered bytes.

use std::{io, marker, num::NonZero};

use crate::buffer;

pub mod raw;

/// An exclusively borrowed byte-bounded view of an exact cursor.
#[must_use = "a bounded scatter view does nothing until it is iterated"]
pub struct Take<'cursor, 'data, C, const MAX: usize = { usize::MAX }> {
    cursor: &'cursor mut C,
    remaining: usize,
    data: marker::PhantomData<&'data [u8]>,
}

const _: () = assert!(size_of::<Take<'static, 'static, &'static [u8]>>() == 2 * size_of::<usize>());

impl<'cursor, 'data, C, const MAX: usize> Take<'cursor, 'data, C, MAX> {
    pub fn new(cursor: &'cursor mut C, limit: usize) -> Self
    where
        C: buffer::raw::Scatter<'data>,
    {
        let len = limit.min(cursor.remaining());
        Self {
            cursor,
            remaining: len,
            data: marker::PhantomData,
        }
    }

    pub const fn len(&self) -> usize {
        self.remaining
    }

    pub const fn is_empty(&self) -> bool {
        self.remaining == 0
    }
}

impl<'data, C: buffer::raw::Scatter<'data>, const MAX: usize> Take<'_, 'data, C, MAX> {
    pub(in crate::buffer) fn finish(mut self) {
        while !self.is_empty() {
            let _ = self.next();
        }
    }
}

impl<'data, C: buffer::raw::Scatter<'data>, const MAX: usize> Iterator for Take<'_, 'data, C, MAX> {
    type Item = buffer::raw::Chunk<'data, MAX>;

    fn next(&mut self) -> Option<Self::Item> {
        const { assert!(MAX > 0) };
        let cap = NonZero::new(self.remaining.min(MAX))?;
        // SAFETY: the exclusive cursor borrow retains this admitted prefix.
        // Exact advancement consumes at most cap, which fits both remaining counts.
        let chunk = unsafe { self.cursor.advance(cap) };
        self.remaining -= chunk.len().get();
        Some(chunk)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        if self.is_empty() {
            (0, Some(0))
        } else {
            (1, None)
        }
    }
}

pub struct Reader<'data, I> {
    parts: I,
    current: &'data [u8],
}

impl<'data, I, T> Reader<'data, I>
where
    I: Iterator<Item = &'data T>,
    T: AsRef<[u8]> + ?Sized + 'data,
{
    pub fn new(parts: I) -> Self {
        Self {
            parts,
            current: &[],
        }
    }

    fn fill(&mut self) {
        while self.current.is_empty() {
            let Some(part) = self.parts.next() else {
                break;
            };
            self.current = part.as_ref();
        }
    }
}

impl<'data, I, T> io::Read for Reader<'data, I>
where
    I: Iterator<Item = &'data T>,
    T: AsRef<[u8]> + ?Sized + 'data,
{
    fn read(&mut self, output: &mut [u8]) -> io::Result<usize> {
        self.fill();
        let len = output.len().min(self.current.len());
        output[..len].copy_from_slice(&self.current[..len]);
        self.current = &self.current[len..];
        Ok(len)
    }
}

impl<'data, I, T> io::BufRead for Reader<'data, I>
where
    I: Iterator<Item = &'data T>,
    T: AsRef<[u8]> + ?Sized + 'data,
{
    fn fill_buf(&mut self) -> io::Result<&[u8]> {
        self.fill();
        Ok(self.current)
    }

    fn consume(&mut self, amount: usize) {
        let consumed = amount.min(self.current.len());
        self.current = &self.current[consumed..];
    }
}