chiark / gitweb /
wip
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 14 Nov 2024 16:08:09 +0000 (16:08 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 14 Nov 2024 21:49:15 +0000 (21:49 +0000)
src/lib.rs

index 384474bf3aa82e4c22e656cb6a0c4d242f733f94..4055179c7c56684bf1d98cee8c2cb4058d1f86a6 100644 (file)
@@ -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<a'>(
-    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<T> {
-    // invariant over T
-    ptr: NonZero<T>,
+    // invariant over T?
+    // XXXX check variance
+    ptr: NonNull<T>,
 }
 
 impl<T> Ptr<T> {
@@ -42,10 +46,10 @@ TODO lifetime is hard
 */
 
     #[inline]
-    pub fn borrow(self, g: &NoAlias) -> Ref<T> { .. }
+    pub fn borrow<'na>(self, na: &'na NoAlias<'na>) -> &'na T { .. }
 
     #[inline]
-    pub fn borrow_mut(self, g: &mut NoAlias) -> RefMut<T> { .. }
+    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<T> Deref for Ref<T> {
-    type Target = T;
-}
-impl<T> DerefMut for RefMut<T> {
-    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<T>(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);
     }
 }
+
+