From: Ian Jackson Date: Thu, 14 Nov 2024 16:20:15 +0000 (+0000) Subject: compiles? X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=55c0e68072bd2a2c28637e6b06b3104405083e9a;p=manually-boxed compiles? --- diff --git a/src/lib.rs b/src/lib.rs index 4055179..c7d7b0d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 { // invariant over T? // XXXX check variance ptr: NonNull, } +impl Clone for Ptr { + fn clone(&self) -> Self { *self } +} +impl Copy for Ptr {} + impl Ptr { - pub fn new(t: T) -> Self { .. } + pub fn new_heap(t: T) -> Self { todo!() } /* /// SAFETY @@ -42,14 +55,21 @@ impl Ptr { /// /// 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 { + 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(self, p: &mut NoAlias */ @@ -105,21 +126,23 @@ mod tests { use super::*; mod list { + use super::*; + pub struct Node { - back: Option>, - next: Option>, + back: Option>>, + next: Option>>, pub data: T, } pub struct List { - head: Option>, - tail: Option>, + head: Option>>, + tail: Option>>, noalias: NoAliasSingleton, } impl List { 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 { - 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::::new(); drop(l); let mut l = List::new();