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

index 4055179c7c56684bf1d98cee8c2cb4058d1f86a6..c7d7b0d6f66a8c9cb47ac851d0ac1494865b5acb 100644 (file)
@@ -1,4 +1,6 @@
 
+#![allow(unused)]
+
 use std::marker::PhantomData;
 use std::ptr::NonNull;
 
@@ -7,7 +9,13 @@ pub struct NoAliasSingleton {
     _unsafe_to_construct: (),
 }
 
-/*impl Deref for NoAliasSingleton {
+impl NoAliasSingleton {
+    pub fn as_mut<'a>(&'a mut self) -> NoAlias<'a> {
+        NoAlias(PhantomData)
+    }
+}
+
+/*impl Deref for 
     type Target = NoAlias<'a>;
     fn deref(
 } */   
@@ -15,23 +23,28 @@ pub struct NoAliasSingleton {
 
 // Not Clone or Copy
 pub struct NoAlias<'a>(
-    PhantomData(&'a mut NoAliasSingleton),
+    PhantomData<&'a mut NoAliasSingleton>,
 );
 
 impl<'a> NoAlias<'a> {
     pub fn reborrow<'s: 'a>(&'s mut self) -> NoAlias<'a> {
+        todo!()
     }
 }
 
-#[derive(Copy)]
 pub struct Ptr<T> {
     // invariant over T?
     // XXXX check variance
     ptr: NonNull<T>,
 }
 
+impl<T> Clone for Ptr<T> {
+    fn clone(&self) -> Self { *self }
+}
+impl<T> Copy for Ptr<T> {}
+
 impl<T> Ptr<T> {
-    pub fn new(t: T) -> Self { .. }
+    pub fn new_heap(t: T) -> Self { todo!() }
 
 /*
     /// SAFETY
@@ -42,14 +55,21 @@ impl<T> Ptr<T> {
     ///
     /// You must not call `deallocate` on the return value.
 TODO lifetime is hard
-    pub unsafe fn new_borrowed<'t>(t: Pin<&'t mut T>) -> Self { .. }
+    pub unsafe fn new_borrowed<'t>(t: Pin<&'t mut T>) -> Self { todo!() }
 */
 
     #[inline]
-    pub fn borrow<'na>(self, na: &'na NoAlias<'na>) -> &'na T { .. }
+    pub fn borrow<'na>(self, na: &'na NoAlias<'na>) -> &'na T { todo!() }
 
     #[inline]
-    pub fn borrow_mut<'na>(self, g: &mut NoAlias<'na>) -> &'na mut T { .. }
+    pub fn borrow_mut<'na>(self, g: &mut NoAlias<'na>) -> &'na mut T {
+        todo!()
+    }
+
+    /// Useful if you want to compare pointers, or something
+    pub fn as_ptr(self) -> NonNull<T> {
+        self.ptr
+    }
 
     /// 
     ///
@@ -64,11 +84,11 @@ TODO lifetime is hard
     /// ("used" means passed to any method in this library).
     ///
     /// (The compiler will check that no borrows are live.)
-    pub unsafe fn free(self, g: &mut NoAlias) -> T { .. }
+    pub unsafe fn free_heap(self, g: &mut NoAlias) -> T { todo!() }
 }
 
-struct MultiAccessor<'a, R, const N: usize> {
-}
+/*struct MultiAccessor<'a, R, const N: usize> {
+}*/
 
 impl NoAliasSingleton {
     /// # SAFETY
@@ -85,17 +105,18 @@ impl NoAliasSingleton {
     //
     // 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 { .. }
+    pub unsafe fn init() -> Self { todo!() }
 }
 
 impl<'a> NoAlias<'a> {
-
-    pub fn multi(&mut self) -> MultiAccessor<(), 0> { .. }
+/*
+    pub fn multi(&mut self) -> MultiAccessor<(), 0> { todo!() }
+*/
 }
 
 /*
 impl MultiAccessor<'a, R, const N: usize> {
-    pub fn finish(self) -> R { .. }
+    pub fn finish(self) -> R { todo!() }
 
     pub fn borrow<T>(self, p: &mut NoAlias
 */
@@ -105,21 +126,23 @@ mod tests {
     use super::*;
 
     mod list {
+        use super::*;
+
         pub struct Node<T> {
-            back: Option<Ptr<Node>>,
-            next: Option<Ptr<Node>>,
+            back: Option<Ptr<Node<T>>>,
+            next: Option<Ptr<Node<T>>>,
             pub data: T,
         }
 
         pub struct List<T> {
-            head: Option<Ptr<Node>>,
-            tail: Option<Ptr<Node>>,
+            head: Option<Ptr<Node<T>>>,
+            tail: Option<Ptr<Node<T>>>,
             noalias: NoAliasSingleton,
         }
 
         impl<T> List<T> {
             pub fn new() -> Self {
-                let noalias = unsafe { NoAliasSingleton::new() };
+                let noalias = unsafe { NoAliasSingleton::init() };
                 List { head: None, tail: None, noalias }
             }
 
@@ -130,21 +153,24 @@ mod tests {
                     data,
                 };
                 let node = Ptr::new_heap(node);
-                self.tail = node;
-                if self.head == None { self.head = node };
+                self.tail = Some(node);
+                if self.head.is_none() { self.head = Some(node) };
             }
 
             pub fn pop_front(&mut self) -> Option<T> {
-                let noalias = &mut self.noalias;
+                let mut noalias = self.noalias.as_mut();
+                let noalias = &mut noalias;
+
                 let deleting = self.head?;
-                let new_head = node.borrow(noalias);
+                let new_head = deleting.borrow(noalias).next;
                 self.head = new_head;
                 if let Some(new_head) = new_head {
-                    new_head.borrow_mut(&mut self.noalias).back = None;
+                    new_head.borrow_mut(noalias).back = None;
                 } else {
                     self.tail = None;
                 }
-                deleting.free(noalias)
+                let deleted = unsafe { deleting.free_heap(noalias) };
+                Some(deleted.data)
             }
         }
 
@@ -157,7 +183,9 @@ mod tests {
 
     #[test]
     fn demo() {
-        let l = List::new();
+        use list::List;
+
+        let l = List::<String>::new();
         drop(l);
 
         let mut l = List::new();