use std::{
fmt::{Debug, Pointer},
io::Cursor,
ops::{Deref, DerefMut},
};
pub struct RawPtr<T: ?Sized> {
inner: *const T,
}
#[allow(dead_code)]
impl<T: ?Sized> RawPtr<T> {
pub fn new(ptr: *const T) -> Self {
Self { inner: ptr }
}
pub fn is_null(&self) -> bool {
self.inner.is_null()
}
}
impl<T: ?Sized> Clone for RawPtr<T> {
fn clone(&self) -> Self {
*self
}
}
impl<T: ?Sized> Copy for RawPtr<T> {}
impl<T: ?Sized> Deref for RawPtr<T> {
type Target = *const T;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<T: ?Sized> PartialEq for RawPtr<T> {
fn eq(&self, other: &Self) -> bool {
std::ptr::eq(self.inner, other.inner)
}
}
#[allow(dead_code)]
impl<T: Sized> RawPtr<T> {
pub fn null() -> Self {
Self {
inner: std::ptr::null(),
}
}
pub fn ptr_eq(&self, ptr: *const T) -> bool {
std::ptr::eq(self.inner, ptr)
}
}
impl<T: ?Sized> Debug for RawPtr<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Pointer::fmt(&self.inner, f)
}
}
unsafe impl<T: ?Sized> Send for RawPtr<T> {}
unsafe impl<T: ?Sized> Sync for RawPtr<T> {}
pub struct RawMutPtr<T: ?Sized> {
inner: *mut T,
}
impl<T: ?Sized> RawMutPtr<T> {
pub fn new(ptr: *mut T) -> Self {
Self { inner: ptr }
}
pub fn is_null(&self) -> bool {
self.inner.is_null()
}
}
impl<T: ?Sized> Clone for RawMutPtr<T> {
fn clone(&self) -> Self {
*self
}
}
impl<T: ?Sized> Copy for RawMutPtr<T> {}
impl<T: ?Sized> Deref for RawMutPtr<T> {
type Target = *mut T;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl<T: ?Sized> DerefMut for RawMutPtr<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
impl<T: ?Sized> PartialEq for RawMutPtr<T> {
fn eq(&self, other: &Self) -> bool {
std::ptr::eq(self.inner, other.inner)
}
}
impl<T: Sized> RawMutPtr<T> {
pub fn null() -> Self {
Self {
inner: std::ptr::null_mut(),
}
}
pub fn ptr_eq(&self, ptr: *mut T) -> bool {
std::ptr::eq(self.inner, ptr)
}
}
impl<T: ?Sized> Debug for RawMutPtr<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Pointer::fmt(&self.inner, f)
}
}
unsafe impl<T: ?Sized> Send for RawMutPtr<T> {}
unsafe impl<T: ?Sized> Sync for RawMutPtr<T> {}
use bytes::{Bytes, BytesMut};
#[derive(Debug)]
pub struct CursorBytesPtr(RawMutPtr<Cursor<Bytes>>);
impl Deref for CursorBytesPtr {
type Target = Cursor<Bytes>;
fn deref(&self) -> &Self::Target {
unsafe { &**self.0 }
}
}
impl DerefMut for CursorBytesPtr {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut **self.0 }
}
}
impl From<&mut Cursor<Bytes>> for CursorBytesPtr {
fn from(value: &mut Cursor<Bytes>) -> Self {
CursorBytesPtr(RawMutPtr::new(value as *const _ as *mut _))
}
}
#[derive(Debug)]
pub struct BytesMutPtr(RawMutPtr<BytesMut>);
impl Deref for BytesMutPtr {
type Target = BytesMut;
fn deref(&self) -> &Self::Target {
unsafe { &**self.0 }
}
}
impl DerefMut for BytesMutPtr {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut **self.0 }
}
}
impl From<&mut BytesMut> for BytesMutPtr {
fn from(value: &mut BytesMut) -> Self {
BytesMutPtr(RawMutPtr::new(value as *const _ as *mut _))
}
}