use std::mem::MaybeUninit;
#[cfg(unix)]
mod sys {
use std::mem::MaybeUninit;
#[repr(transparent)]
pub struct Inner(libc::iovec);
impl Inner {
pub fn new(ptr: *mut MaybeUninit<u8>, len: usize) -> Self {
Self(libc::iovec {
iov_base: ptr as *mut libc::c_void,
iov_len: len,
})
}
pub fn len(&self) -> usize {
self.0.iov_len
}
pub fn as_ptr(&self) -> *mut MaybeUninit<u8> {
self.0.iov_base as *mut MaybeUninit<u8>
}
}
}
#[cfg(windows)]
mod sys {
use std::mem::MaybeUninit;
#[repr(C)]
#[allow(clippy::upper_case_acronyms)]
struct WSABUF {
pub len: u32,
pub buf: *mut MaybeUninit<u8>,
}
#[repr(transparent)]
pub struct Inner(WSABUF);
impl Inner {
pub fn new(ptr: *mut MaybeUninit<u8>, len: usize) -> Self {
Self(WSABUF {
len: len as u32,
buf: ptr,
})
}
pub fn len(&self) -> usize {
self.0.len as _
}
pub fn as_ptr(&self) -> *mut MaybeUninit<u8> {
self.0.buf
}
}
}
#[cfg(not(any(unix, windows)))]
compile_error!("`IoSlice` only available on unix and windows");
#[repr(transparent)]
pub struct IoSlice(sys::Inner);
impl IoSlice {
pub unsafe fn new(ptr: *const u8, len: usize) -> Self {
Self(sys::Inner::new(ptr as _, len))
}
pub unsafe fn from_slice(slice: &[u8]) -> Self {
Self::new(slice.as_ptr() as _, slice.len())
}
pub fn as_ptr(&self) -> *const u8 {
self.0.as_ptr() as _
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
#[repr(transparent)]
pub struct IoSliceMut(sys::Inner);
impl IoSliceMut {
pub unsafe fn new(ptr: *mut MaybeUninit<u8>, len: usize) -> Self {
Self(sys::Inner::new(ptr, len))
}
pub unsafe fn from_slice(slice: &mut [u8]) -> Self {
Self::new(slice.as_mut_ptr() as _, slice.len())
}
pub unsafe fn from_uninit(slice: &mut [MaybeUninit<u8>]) -> Self {
Self::new(slice.as_mut_ptr(), slice.len())
}
pub fn as_ptr(&self) -> *mut MaybeUninit<u8> {
self.0.as_ptr()
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}