o3 0.62.0

shared-nothing primitives
Documentation
use std::ptr;

use crate::buffer;

pub trait Transfer {
    /// # Safety
    /// `target` must retain exclusive writable storage for the complete byte sum,
    /// which must fit `usize`. Every source must be disjoint from that storage.
    unsafe fn copy_to(&self, target: *mut u8);
}

impl<T: Transfer + ?Sized> Transfer for &T {
    unsafe fn copy_to(&self, target: *mut u8) {
        // SAFETY: borrowing preserves the original sources and their complete extent.
        unsafe { T::copy_to(self, target) };
    }
}

impl Transfer for [u8] {
    unsafe fn copy_to(&self, target: *mut u8) {
        // SAFETY: the caller retains this source and its complete disjoint target.
        unsafe { ptr::copy_nonoverlapping(self.as_ptr(), target, self.len()) };
    }
}

impl<const N: usize> Transfer for [&[u8]; N] {
    unsafe fn copy_to(&self, target: *mut u8) {
        // SAFETY: the caller retains every slice and its complete disjoint target.
        unsafe { buffer::raw::IteratorExt::copy_to(self.iter().copied(), target) };
    }
}

impl<I: Transfer> Transfer for (&[u8], I) {
    unsafe fn copy_to(&self, target: *mut u8) {
        // SAFETY: admission covers the prefix and remaining sources in order.
        unsafe {
            Transfer::copy_to(self.0, target);
            Transfer::copy_to(&self.1, target.add(self.0.len()));
        }
    }
}

impl<const P: usize, const N: usize> Transfer for ([u8; P], [&[u8]; N]) {
    unsafe fn copy_to(&self, target: *mut u8) {
        // SAFETY: admission includes the complete fixed prefix and every trailing source.
        unsafe { Transfer::copy_to(&(self.0.as_slice(), &self.1), target) };
    }
}