#[cfg(feature = "allocator_api")]
use std::alloc::Allocator;
use std::mem::MaybeUninit;
use crate::{buf::*, vec_alloc};
pub unsafe trait IoBuf<'arena>: Unpin + 'arena {
fn as_buf_ptr(&self) -> *const u8;
fn buf_len(&self) -> usize;
fn buf_capacity(&self) -> usize;
fn as_slice(&self) -> &[u8] {
unsafe { std::slice::from_raw_parts(self.as_buf_ptr(), self.buf_len()) }
}
fn slice(self, range: impl std::ops::RangeBounds<usize>) -> Slice<Self>
where
Self: Sized,
{
use std::ops::Bound;
let begin = match range.start_bound() {
Bound::Included(&n) => n,
Bound::Excluded(&n) => n + 1,
Bound::Unbounded => 0,
};
assert!(begin < self.buf_capacity());
let end = match range.end_bound() {
Bound::Included(&n) => n.checked_add(1).expect("out of range"),
Bound::Excluded(&n) => n,
Bound::Unbounded => self.buf_capacity(),
};
assert!(end <= self.buf_capacity());
assert!(begin <= self.buf_len());
Slice::new(self, begin, end)
}
}
unsafe impl<#[cfg(feature = "allocator_api")] A: Allocator + Unpin + 'static> IoBuf<'static>
for vec_alloc!(u8, A)
{
fn as_buf_ptr(&self) -> *const u8 {
self.as_ptr()
}
fn buf_len(&self) -> usize {
self.len()
}
fn buf_capacity(&self) -> usize {
self.capacity()
}
}
unsafe impl<'a> IoBuf<'a> for &'a mut [u8] {
fn as_buf_ptr(&self) -> *const u8 {
self.as_ptr()
}
fn buf_len(&self) -> usize {
self.len()
}
fn buf_capacity(&self) -> usize {
self.len()
}
}
unsafe impl<'a> IoBuf<'a> for &'a [u8] {
fn as_buf_ptr(&self) -> *const u8 {
self.as_ptr()
}
fn buf_len(&self) -> usize {
self.len()
}
fn buf_capacity(&self) -> usize {
self.len()
}
}
unsafe impl IoBuf<'static> for String {
fn as_buf_ptr(&self) -> *const u8 {
self.as_ptr()
}
fn buf_len(&self) -> usize {
self.len()
}
fn buf_capacity(&self) -> usize {
self.capacity()
}
}
unsafe impl<'a> IoBuf<'a> for &'a mut str {
fn as_buf_ptr(&self) -> *const u8 {
self.as_ptr()
}
fn buf_len(&self) -> usize {
self.len()
}
fn buf_capacity(&self) -> usize {
self.len()
}
}
unsafe impl<'a> IoBuf<'a> for &'a str {
fn as_buf_ptr(&self) -> *const u8 {
self.as_ptr()
}
fn buf_len(&self) -> usize {
self.len()
}
fn buf_capacity(&self) -> usize {
self.len()
}
}
#[cfg(feature = "bytes")]
unsafe impl IoBuf<'static> for bytes::Bytes {
fn as_buf_ptr(&self) -> *const u8 {
self.as_ptr()
}
fn buf_len(&self) -> usize {
self.len()
}
fn buf_capacity(&self) -> usize {
self.len()
}
}
#[cfg(feature = "bytes")]
unsafe impl IoBuf<'static> for bytes::BytesMut {
fn as_buf_ptr(&self) -> *const u8 {
self.as_ptr()
}
fn buf_len(&self) -> usize {
self.len()
}
fn buf_capacity(&self) -> usize {
self.capacity()
}
}
#[cfg(feature = "read_buf")]
unsafe impl<'arena> IoBuf<'arena> for std::io::BorrowedBuf<'arena> {
fn as_buf_ptr(&self) -> *const u8 {
self.filled().as_ptr()
}
fn buf_len(&self) -> usize {
self.len()
}
fn buf_capacity(&self) -> usize {
self.capacity()
}
}
#[cfg(feature = "arrayvec")]
unsafe impl<const N: usize> IoBuf<'static> for arrayvec::ArrayVec<u8, N> {
fn as_buf_ptr(&self) -> *const u8 {
self.as_ptr()
}
fn buf_len(&self) -> usize {
self.len()
}
fn buf_capacity(&self) -> usize {
self.capacity()
}
}
pub unsafe trait IoBufMut<'arena>: IoBuf<'arena> {
fn as_buf_mut_ptr(&mut self) -> *mut u8;
fn as_uninit_slice(&mut self) -> &mut [MaybeUninit<u8>] {
unsafe {
std::slice::from_raw_parts_mut(
self.as_buf_mut_ptr().add(self.buf_len()) as _,
self.buf_capacity() - self.buf_len(),
)
}
}
fn set_buf_init(&mut self, len: usize);
}
unsafe impl<#[cfg(feature = "allocator_api")] A: Allocator + Unpin + 'static> IoBufMut<'static>
for vec_alloc!(u8, A)
{
fn as_buf_mut_ptr(&mut self) -> *mut u8 {
self.as_mut_ptr()
}
fn set_buf_init(&mut self, len: usize) {
unsafe { self.set_len(len + self.buf_len()) };
}
}
unsafe impl<'a> IoBufMut<'a> for &'a mut [u8] {
fn as_buf_mut_ptr(&mut self) -> *mut u8 {
self.as_mut_ptr()
}
fn set_buf_init(&mut self, len: usize) {
debug_assert!(len == 0)
}
}
#[cfg(feature = "bytes")]
unsafe impl IoBufMut<'static> for bytes::BytesMut {
fn as_buf_mut_ptr(&mut self) -> *mut u8 {
self.as_mut_ptr()
}
fn set_buf_init(&mut self, len: usize) {
unsafe { self.set_len(len + self.buf_len()) };
}
}
#[cfg(feature = "read_buf")]
unsafe impl<'arena> IoBufMut<'arena> for std::io::BorrowedBuf<'arena> {
fn as_buf_mut_ptr(&mut self) -> *mut u8 {
self.filled().as_ptr() as _
}
fn set_buf_init(&mut self, len: usize) {
unsafe { self.unfilled().advance(len) };
}
}
#[cfg(feature = "arrayvec")]
unsafe impl<const N: usize> IoBufMut<'static> for arrayvec::ArrayVec<u8, N> {
fn as_buf_mut_ptr(&mut self) -> *mut u8 {
self.as_mut_ptr()
}
fn set_buf_init(&mut self, len: usize) {
unsafe { self.set_len(len + self.buf_len()) };
}
}