From: Ian Jackson Date: Thu, 14 Nov 2024 16:02:09 +0000 (+0000) Subject: wip X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=304631dc4cf0577fab758c8be84344c85220b914;p=manually-boxed wip --- diff --git a/src/lib.rs b/src/lib.rs index 5c0a926..384474b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,9 +1,25 @@ // Not Clone or Copy -pub struct NoAlias { +pub struct NoAliasSingleton { _unsafe_to_construct: (), } +impl Deref for NoAliasSingleton { + type Target = NoAlias<'a>; + fn deref( +} + + +// Not Clone or Copy +pub struct NoAlias( + PhantomData(&mut NoAliasSingleton), +); + +impl<'a> NoAlias<'a> { + pub fn reborrow<'s: 'a>(&'s mut self) -> NoAlias<'a> { + } +} + #[derive(Copy)] pub struct Ptr { // invariant over T @@ -13,6 +29,7 @@ pub struct Ptr { impl Ptr { pub fn new(t: T) -> Self { .. } +/* /// SAFETY /// /// All copies of the returned `Ptr` become invalid @@ -20,15 +37,18 @@ impl Ptr { /// No-one must use them after that. /// /// You must not call `deallocate` on the return value. - pub unsafe fn new_stack<'t>( - t: Pin<&'t mut T>, - ) -> Self { .. } +TODO lifetime is hard + pub unsafe fn new_borrowed<'t>(t: Pin<&'t mut T>) -> Self { .. } +*/ + #[inline] pub fn borrow(self, g: &NoAlias) -> Ref { .. } - // TODO ZST + #[inline] pub fn borrow_mut(self, g: &mut NoAlias) -> RefMut { .. } + /// + /// /// # SAFETY /// /// All copies of `self` will be invalidated. @@ -40,7 +60,7 @@ impl Ptr { /// ("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) { .. } + pub unsafe fn free(self, g: &mut NoAlias) -> T { .. } } impl Deref for Ref { @@ -86,9 +106,65 @@ pub fn add(left: u64, right: u64) -> u64 { mod tests { use super::*; + mod list { + pub struct Node { + back: Option>, + next: Option>, + pub data: T, + } + + pub struct List { + head: Option>, + tail: Option>, + noalias: NoAliasSingleton, + } + + impl List { + pub fn new() -> Self { + let noalias = unsafe { NoAliasSingleton::new() }; + List { head: None, tail: None, noalias } + } + + pub fn append(&mut self, data: T) { + let node = Node { + back: self.head, + next: None, + data, + }; + let node = Ptr::new_heap(node); + self.tail = node; + if self.head == None { self.head = node }; + } + + pub fn pop_front(&mut self) -> Option { + let noalias = &mut self.noalias; + let deleting = self.head?; + let new_head = node.borrow(noalias); + self.head = new_head; + if let Some(new_head) = new_head { + new_head.borrow_mut(&mut self.noalias).back = None; + } else { + self.tail = None; + } + deleting.free(noalias) + } + } + + impl Drop for List { + fn drop(&mut self) { + while let Some(_) = self.pop_front() { } + } + } + } + #[test] - fn - let result = add(2, 2); - assert_eq!(result, 4); + fn demo() { + let l = List::new(); + drop(l); + + let mut l = List::new(); + l.append(format!("hi")); + l.append(format!("ho")); + drop(l); } }