add Unique<T>

This commit is contained in:
Charlotte 🦝 Delenk 2022-07-08 19:26:04 +01:00
parent 67d7885ef3
commit b57c14d346
Signed by: darkkirb
GPG key ID: AB2BD8DAF2E37122
4 changed files with 78 additions and 2 deletions

View file

@ -6,6 +6,7 @@
//! It uses a const generic parameter to set the base address of the pointer. This allows multiple //! It uses a const generic parameter to set the base address of the pointer. This allows multiple
//! small memory pools to coexist. //! small memory pools to coexist.
#![feature(coerce_unsized)] #![feature(coerce_unsized)]
#![feature(const_trait_impl)]
#![feature(mixed_integer_ops)] #![feature(mixed_integer_ops)]
#![feature(never_type)] #![feature(never_type)]
#![feature(ptr_metadata)] #![feature(ptr_metadata)]

View file

@ -7,3 +7,5 @@ mod mut_ptr;
pub use mut_ptr::*; pub use mut_ptr::*;
mod non_null; mod non_null;
pub use non_null::*; pub use non_null::*;
mod unique;
pub use unique::*;

View file

@ -2,7 +2,7 @@ use core::{num::NonZeroU16, marker::{PhantomData, Unsize}, ops::CoerceUnsized, f
use crate::Pointable; use crate::Pointable;
use super::MutPtr; use super::{MutPtr, Unique};
/// `*mut T` but non-zero and covariant /// `*mut T` but non-zero and covariant
pub struct NonNull<T: Pointable + ?Sized, const BASE: usize> { pub struct NonNull<T: Pointable + ?Sized, const BASE: usize> {
@ -150,6 +150,10 @@ impl<T: Pointable + ?Sized, const BASE: usize> hash::Hash for NonNull<T, BASE> {
self.as_ptr().hash(state) self.as_ptr().hash(state)
} }
} }
// TODO: From<Unique<T>> impl<T: Pointable + ?Sized, const BASE: usize> From<Unique<T, BASE>> for NonNull<T, BASE> {
fn from(ptr: Unique<T, BASE>) -> Self {
ptr.pointer
}
}
// TODO: From<RefMut<T>> // TODO: From<RefMut<T>>
// TODO: From<Ref<T>> // TODO: From<Ref<T>>

View file

@ -0,0 +1,69 @@
use core::{marker::{PhantomData, Unsize}, ops::CoerceUnsized, fmt};
use crate::Pointable;
use super::{NonNull, MutPtr};
/// Unique pointer
#[repr(transparent)]
pub struct Unique<T: Pointable + ?Sized, const BASE: usize> {
pub(crate) pointer: NonNull<T, BASE>,
_marker: PhantomData<T>
}
unsafe impl<T: Pointable + Send + ?Sized, const BASE: usize> Send for Unique<T, BASE> {}
unsafe impl<T: Pointable + Sync + ?Sized, const BASE: usize> Sync for Unique<T, BASE> {}
impl<T: Pointable<PointerMetaTiny = ()> + Sized, const BASE: usize> Unique<T, BASE> {
pub const fn dangling() -> Self {
Self::from(NonNull::dangling())
}
}
impl<T: Pointable + ?Sized, const BASE: usize> Unique<T, BASE> {
pub const unsafe fn new_unchecked(ptr: MutPtr<T, BASE>) -> Self {
Self::from(NonNull::new_unchecked(ptr))
}
pub const fn new(ptr: MutPtr<T, BASE>) -> Option<Self> {
match NonNull::new(ptr) {
Some(v) => Some(Self::from(v)),
None => None
}
}
pub const fn as_ptr(self) -> MutPtr<T, BASE> {
self.pointer.as_ptr()
}
// TODO: as_ref
// TODO: as_mut
pub const fn cast<U>(self) -> Unique<U, BASE>
where U: Pointable<PointerMetaTiny = ()> + Sized
{
Unique::from(self.pointer.cast())
}
}
impl<T: Pointable + ?Sized, const BASE: usize> Clone for Unique<T, BASE> {
fn clone(&self) -> Self {
*self
}
}
impl<T: Pointable + ?Sized, const BASE: usize> Copy for Unique<T, BASE> {}
impl<T: Pointable + ?Sized, U: Pointable + ?Sized, const BASE: usize> CoerceUnsized<Unique<U, BASE>> for Unique<T, BASE> where T: Unsize<U>, <T as Pointable>::PointerMetaTiny: CoerceUnsized<<U as Pointable>::PointerMetaTiny> {}
impl<T: Pointable + ?Sized, const BASE: usize> fmt::Debug for Unique<T, BASE> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Pointer::fmt(&self.as_ptr(), f)
}
}
impl<T: Pointable + ?Sized, const BASE: usize> fmt::Pointer for Unique<T, BASE> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Pointer::fmt(&self.as_ptr(), f)
}
}
// TODO: From<RefMut<T>>
impl<T: Pointable + ?Sized, const BASE: usize> const From<NonNull<T, BASE>> for Unique<T, BASE> {
fn from(pointer: NonNull<T, BASE>) -> Self {
Unique { pointer, _marker: PhantomData }
}
}