azur 0.3.1

A no_std Rust crate that implements an executor/reactor and futures using io_uring
Documentation
use core::mem::MaybeUninit;

use super::{
    IoBuf,
    IoBufMut,
};

pub struct PtrBuf {
    ptr: *const [u8],
}

impl PtrBuf {
    /// Creates a new pointer based IO buffer.
    ///
    /// # Safety
    ///
    /// Until return object is dropped, the memory backing the slice that `ptr` points to must not
    /// be deallocated, and the slice that `ptr` points to must not have anymutable reference taken
    /// to it.
    pub unsafe fn new(ptr: *const [u8]) -> Self {
        Self { ptr }
    }
}

impl AsRef<[u8]> for PtrBuf {
    fn as_ref(&self) -> &[u8] {
        unsafe { &*self.ptr }
    }
}

unsafe impl IoBuf for PtrBuf {}

pub struct PtrBufMut {
    ptr: *mut [MaybeUninit<u8>],
    len: usize,
}

impl PtrBufMut {
    /// Creates a new mutable pointer based IO buffer.
    ///
    /// # Safety
    ///
    /// Until return object is dropped, the memory backing the slice that `ptr` points to must not
    /// be deallocated, and the slice that `ptr` points to must not have any reference taken to
    /// it.
    pub unsafe fn new(ptr: *mut [MaybeUninit<u8>]) -> Self {
        Self { ptr, len: 0 }
    }
}

impl AsRef<[u8]> for PtrBufMut {
    fn as_ref(&self) -> &[u8] {
        let slice = unsafe { &*(self.ptr as *const [MaybeUninit<u8>]) };
        unsafe { &*(&slice[..self.len] as *const [MaybeUninit<u8>] as *const [u8]) }
    }
}

unsafe impl IoBuf for PtrBufMut {}

unsafe impl IoBufMut for PtrBufMut {
    fn uninit_bytes(&mut self) -> &mut [MaybeUninit<u8>] {
        unsafe { &mut *self.ptr }
    }

    unsafe fn set_len(&mut self, new_len: usize) {
        debug_assert!(new_len <= self.ptr.len());
        self.len = new_len;
    }
}