o3 0.60.0

shared-nothing primitives
Documentation
use std::{collections::TryReserveError, marker::PhantomData};

use crate::buffer::{self, write, write::consume};

/// An empty vector suffix whose capacity remains admitted until staging begins.
#[derive(Debug)]
#[must_use = "consume the reservation to stage its initialized suffix"]
pub struct Reserved<'a, L = usize> {
    out: &'a mut Vec<u8>,
    end: usize,
    length: PhantomData<L>,
}

impl<'a, L: buffer::raw::Length> Reserved<'a, L> {
    pub fn try_grow(out: &'a mut Vec<u8>, limit: L) -> Result<Self, crate::alloc::Error> {
        crate::alloc::VecExt::try_grow(out, limit.into_usize())?;
        Ok(Self {
            end: out.len() + limit.into_usize(),
            out,
            length: PhantomData,
        })
    }

    pub fn try_reserve(out: &'a mut Vec<u8>, limit: L) -> Result<Self, crate::alloc::Error> {
        crate::alloc::VecExt::try_reserve(out, limit.into_usize())?;
        Ok(Self {
            end: out.len() + limit.into_usize(),
            out,
            length: PhantomData,
        })
    }

    pub fn try_new(out: &'a mut Vec<u8>, limit: L) -> Result<Self, consume::Error> {
        consume::Error::check(limit.into_usize(), out.capacity() - out.len())?;
        Ok(Self {
            end: out.len() + limit.into_usize(),
            out,
            length: PhantomData,
        })
    }

    pub fn stage<S: buffer::raw::Length>(&mut self, limit: S) -> write::Staged<&mut Vec<u8>, S> {
        let capacity = limit.into_usize().min(self.end - self.out.len());
        write::Staged {
            out: &mut *self.out,
            len: S::ZERO,
            capacity: S::from_usize(capacity),
        }
    }

    /// Transfers the same exclusive suffix without repeating capacity admission.
    pub fn into_staged(self) -> write::Staged<&'a mut Vec<u8>, L> {
        let capacity = L::from_usize(self.end - self.out.len());
        write::Staged {
            out: self.out,
            len: L::ZERO,
            capacity,
        }
    }

    pub fn into_inner(self) -> &'a mut Vec<u8> {
        self.out
    }

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

    pub fn is_empty(&self) -> bool {
        self.out.is_empty()
    }
}

impl<'a> Reserved<'a> {
    /// Preserves the standard vector's exact reservation error and admitted owner.
    pub fn try_exact(out: &'a mut Vec<u8>, limit: usize) -> Result<Self, TryReserveError> {
        out.try_reserve_exact(limit)?;
        Ok(Self {
            end: out.len() + limit,
            out,
            length: PhantomData,
        })
    }
}

impl<'a> From<&'a mut Vec<u8>> for Reserved<'a> {
    /// Retains the vector's entire available suffix under its exclusive borrow.
    fn from(out: &'a mut Vec<u8>) -> Self {
        Self {
            end: out.capacity(),
            out,
            length: PhantomData,
        }
    }
}

const _: () = {
    assert!(size_of::<Reserved<'static>>() == 2 * size_of::<usize>());
    assert!(size_of::<Option<Reserved<'static>>>() == size_of::<Reserved<'static>>());
    assert!(!std::mem::needs_drop::<Reserved<'static>>());
};