Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove buffer wrappers. #76

Merged
merged 7 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 0 additions & 123 deletions compio-buf/src/buf_wrapper.rs

This file was deleted.

136 changes: 107 additions & 29 deletions compio-buf/src/io_buf.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#[cfg(feature = "allocator_api")]
use std::alloc::Allocator;
use std::mem::MaybeUninit;
use std::{
io::{IoSlice, IoSliceMut},
mem::MaybeUninit,
};

use crate::*;

Expand All @@ -15,7 +18,7 @@ use crate::*;
/// While the runtime holds ownership to a buffer, the pointer returned
/// by `as_buf_ptr` must remain valid even if the `IoBuf` value is moved, i.e.,
/// the type implementing `IoBuf` should point to somewhere else.
pub unsafe trait IoBuf: 'static {
pub unsafe trait IoBuf: Unpin + 'static {
/// Returns a raw pointer to the vector’s buffer.
///
/// This method is to be used by the `compio` runtime and it is not
Expand Down Expand Up @@ -46,6 +49,11 @@ pub unsafe trait IoBuf: 'static {
unsafe { std::slice::from_raw_parts(self.as_buf_ptr(), self.buf_len()) }
}

#[doc(hidden)]
unsafe fn as_io_slice(&self) -> IoSlice<'static> {
IoSlice::new(std::mem::transmute(self.as_slice()))
}

/// Returns a view of the buffer with the specified range.
///
/// This method is similar to Rust's slicing (`&buf[..]`), but takes
Expand Down Expand Up @@ -86,7 +94,9 @@ pub unsafe trait IoBuf: 'static {
}
}

unsafe impl<#[cfg(feature = "allocator_api")] A: Allocator + 'static> IoBuf for vec_alloc!(u8, A) {
unsafe impl<#[cfg(feature = "allocator_api")] A: Allocator + Unpin + 'static> IoBuf
for vec_alloc!(u8, A)
{
fn as_buf_ptr(&self) -> *const u8 {
self.as_ptr()
}
Expand Down Expand Up @@ -260,68 +270,136 @@ pub unsafe trait IoBufMut: IoBuf {
}
}

/// Updates the number of initialized bytes.
///
/// The specified `len` plus [`IoBuf::buf_len`] becomes the new value
/// returned by [`IoBuf::buf_len`].
///
/// # Safety
///
/// `len` should be less or equal than `buf_capacity() - buf_len()`.
unsafe fn set_buf_init(&mut self, len: usize);
#[doc(hidden)]
unsafe fn as_io_slice_mut(&mut self) -> IoSliceMut<'static> {
IoSliceMut::new(std::mem::transmute(self.as_uninit_slice()))
}
}

unsafe impl<#[cfg(feature = "allocator_api")] A: Allocator + 'static> IoBufMut
unsafe impl<#[cfg(feature = "allocator_api")] A: Allocator + Unpin + 'static> IoBufMut
for vec_alloc!(u8, A)
{
fn as_buf_mut_ptr(&mut self) -> *mut u8 {
self.as_mut_ptr()
}

unsafe fn set_buf_init(&mut self, len: usize) {
self.set_len(len + self.buf_len());
}
}

unsafe impl IoBufMut for &'static mut [u8] {
fn as_buf_mut_ptr(&mut self) -> *mut u8 {
self.as_mut_ptr()
}

unsafe fn set_buf_init(&mut self, len: usize) {
debug_assert!(len == 0)
}
}

#[cfg(feature = "bytes")]
unsafe impl IoBufMut for bytes::BytesMut {
fn as_buf_mut_ptr(&mut self) -> *mut u8 {
self.as_mut_ptr()
}

unsafe fn set_buf_init(&mut self, len: usize) {
self.set_len(len + self.buf_len());
}
}

#[cfg(feature = "read_buf")]
unsafe impl IoBufMut for std::io::BorrowedBuf<'static> {
fn as_buf_mut_ptr(&mut self) -> *mut u8 {
self.filled().as_ptr() as _
}

unsafe fn set_buf_init(&mut self, len: usize) {
self.unfilled().advance(len);
}
}

#[cfg(feature = "arrayvec")]
unsafe impl<const N: usize> IoBufMut for arrayvec::ArrayVec<u8, N> {
fn as_buf_mut_ptr(&mut self) -> *mut u8 {
self.as_mut_ptr()
}
}

/// A helper trait for `set_len` like methods.
pub trait SetBufInit {
Berrysoft marked this conversation as resolved.
Show resolved Hide resolved
/// Updates the number of initialized bytes.
///
/// The specified `len` plus [`IoBuf::buf_len`] becomes the new value
/// returned by [`IoBuf::buf_len`].
///
/// # Safety
///
/// `len` should be less or equal than `buf_capacity() - buf_len()`.
unsafe fn set_buf_init(&mut self, len: usize);
}

impl<T: IoBuf + SetBufInit> SetBufInit for Vec<T> {
unsafe fn set_buf_init(&mut self, mut len: usize) {
for buf in self {
let capacity = buf.buf_capacity();
if len >= capacity {
buf.set_buf_init(capacity);
len -= capacity;
} else {
buf.set_buf_init(len);
len = 0;
}
}
}
}

impl<#[cfg(feature = "allocator_api")] A: Allocator + Unpin + 'static> SetBufInit
for vec_alloc!(u8, A)
{
unsafe fn set_buf_init(&mut self, len: usize) {
self.set_len(len + self.buf_len());
}
}

#[cfg(feature = "bytes")]
impl SetBufInit for bytes::BytesMut {
unsafe fn set_buf_init(&mut self, len: usize) {
self.set_len(len + self.buf_len());
}
}

#[cfg(feature = "read_buf")]
impl SetBufInit for std::io::BorrowedBuf<'static> {
unsafe fn set_buf_init(&mut self, len: usize) {
self.unfilled().advance(len);
}
}

#[cfg(feature = "arrayvec")]
impl<const N: usize> SetBufInit for arrayvec::ArrayVec<u8, N> {
unsafe fn set_buf_init(&mut self, len: usize) {
self.set_len(len + self.buf_len());
}
}

#[doc(hidden)]
pub unsafe trait AsIoSlices {
/// # Safety
///
/// The return slice will not live longer than self.
/// It is static to provide convenience from writing self-referenced
/// structure.
unsafe fn as_io_slices(&self) -> Vec<IoSlice<'static>>;
}

unsafe impl<T: IoBuf> AsIoSlices for Vec<T> {
unsafe fn as_io_slices(&self) -> Vec<IoSlice<'static>> {
self.iter()
.map(|buf| IoSlice::new(std::mem::transmute(buf.as_slice())))
.collect()
}
}

#[doc(hidden)]
pub unsafe trait AsIoSlicesMut: AsIoSlices {
/// # Safety
///
/// The return slice will not live longer than self.
/// It is static to provide convenience from writing self-referenced
/// structure.
unsafe fn as_io_slices_mut(&mut self) -> Vec<IoSliceMut<'static>>;
}

unsafe impl<T: IoBufMut> AsIoSlicesMut for Vec<T> {
unsafe fn as_io_slices_mut(&mut self) -> Vec<IoSliceMut<'static>> {
self.iter_mut()
.map(|buf| IoSliceMut::new(std::mem::transmute(buf.as_uninit_slice())))
.collect()
}
}
7 changes: 1 addition & 6 deletions compio-buf/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![cfg_attr(feature = "allocator_api", feature(allocator_api))]
#![cfg_attr(feature = "read_buf", feature(read_buf))]
#![warn(missing_docs)]

//! Utilities for working with buffers.
//!
Expand All @@ -20,12 +21,6 @@ pub use io_buf::*;
mod slice;
pub use slice::*;

mod with_buf;
pub use with_buf::*;

mod buf_wrapper;
pub use buf_wrapper::*;

/// Trait to get the inner buffer of an operation or a result.
pub trait IntoInner {
/// The inner type.
Expand Down
2 changes: 2 additions & 0 deletions compio-buf/src/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ unsafe impl<T: IoBufMut> IoBufMut for Slice<T> {
fn as_buf_mut_ptr(&mut self) -> *mut u8 {
deref_mut(&mut self.buffer)[self.begin..].as_mut_ptr()
}
}

impl<T: SetBufInit> SetBufInit for Slice<T> {
unsafe fn set_buf_init(&mut self, len: usize) {
self.buffer.set_buf_init(len)
}
Expand Down
Loading