From 5a716529eb279a2f4333fa0f742056eac192dc3b Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Thu, 14 Nov 2024 16:08:09 +0000 Subject: [PATCH] wip --- src/lib.rs | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 384474b..4055179 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,18 +1,21 @@ +use std::marker::PhantomData; +use std::ptr::NonNull; + // Not Clone or Copy pub struct NoAliasSingleton { _unsafe_to_construct: (), } -impl Deref for NoAliasSingleton { +/*impl Deref for NoAliasSingleton { type Target = NoAlias<'a>; fn deref( -} +} */ // Not Clone or Copy -pub struct NoAlias( - PhantomData(&mut NoAliasSingleton), +pub struct NoAlias<'a>( + PhantomData(&'a mut NoAliasSingleton), ); impl<'a> NoAlias<'a> { @@ -22,8 +25,9 @@ impl<'a> NoAlias<'a> { #[derive(Copy)] pub struct Ptr { - // invariant over T - ptr: NonZero, + // invariant over T? + // XXXX check variance + ptr: NonNull, } impl Ptr { @@ -42,10 +46,10 @@ TODO lifetime is hard */ #[inline] - pub fn borrow(self, g: &NoAlias) -> Ref { .. } + pub fn borrow<'na>(self, na: &'na NoAlias<'na>) -> &'na T { .. } #[inline] - pub fn borrow_mut(self, g: &mut NoAlias) -> RefMut { .. } + pub fn borrow_mut<'na>(self, g: &mut NoAlias<'na>) -> &'na mut T { .. } /// /// @@ -63,44 +67,38 @@ TODO lifetime is hard pub unsafe fn free(self, g: &mut NoAlias) -> T { .. } } -impl Deref for Ref { - type Target = T; -} -impl DerefMut for RefMut { - type Target = T; -} - struct MultiAccessor<'a, R, const N: usize> { } -impl NoAlias { +impl NoAliasSingleton { /// # SAFETY /// - /// There must be only one `NoAlias` used with each `Ptr`. + /// There must be only one `NoAliasSingleton` used with each `Ptr`. /// /// That is, it is unsound to use the same `Ptr` (or any copy of it) - /// with two (or more) different `NoAlias`es. + /// with `NoAlias`es derived from + /// two (or more) different `NoAliasSingleton`s. /// - /// The easiest way to do this is to have only one `NoAlias`. + /// The easiest way to do this is to have only one `NoAliasSingleton`. /// /// If this rule is violated, `borrow` and `borrow_mut` can be instant-UB. // // I haven't been able to think of a practical safe API, // but one only needs about 1 call to init_unchecked. pub unsafe fn init_unchecked() -> Self { .. } +} + +impl<'a> NoAlias<'a> { pub fn multi(&mut self) -> MultiAccessor<(), 0> { .. } } +/* impl MultiAccessor<'a, R, const N: usize> { pub fn finish(self) -> R { .. } pub fn borrow(self, p: &mut NoAlias - - -pub fn add(left: u64, right: u64) -> u64 { - left + right -} +*/ #[cfg(test)] mod tests { @@ -168,3 +166,5 @@ mod tests { drop(l); } } + + -- 2.30.2