From 2c504e38711fc35568ad006f5c4893ba887aa4c4 Mon Sep 17 00:00:00 2001 From: Charlotte Pabst Date: Thu, 22 Feb 2024 22:38:13 +0100 Subject: --- src/main.rs | 107 +++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 59 insertions(+), 48 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index e29f2fe..bcd0dcf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,37 +47,57 @@ fn short_debug(ty: &'static str, id: usize) -> impl Debug { macro_rules! ptr_t { ($T:ty) => { - &'arena GhostCell<'brand, $T> + Ptr<'brand, 'arena, $T> } } macro_rules! ptr { ($T:ident) => { - &'arena GhostCell<'brand, $T<'brand, 'arena, V>> + Ptr<'brand, 'arena, $T<'brand, 'arena, V>> }; } -macro_rules! own_t { - ($T:ty) => { - Own<'brand, 'arena, $T> - } +pub struct Ptr<'brand, 'arena, T: ?Sized>(pub &'arena GhostCell<'brand, T>); + +impl<'brand, 'arena, T: ?Sized> Clone for ptr_t!(T) { + fn clone(&self) -> Self { + Self(self.0) + } } +impl<'brand, 'arena, T: ?Sized> Copy for ptr_t!(T) {} + +impl<'brand, 'arena, T: ?Sized> ptr_t!(T) { + pub fn borrow<'tok, 'out>(&self, token: &'tok impl ReflAsRef>) -> &'out T + where + 'brand: 'out, + 'arena: 'out, + 'tok: 'out, + { + self.0.borrow(token.as_ref()) + } -macro_rules! own { - ($T:ident) => { - Own<'brand, 'arena, $T<'brand, 'arena, V>> - }; + pub fn borrow_mut<'tok, 'out>( + &self, + token: &'tok mut impl ReflAsMut>, + ) -> &'out mut T + where + 'brand: 'out, + 'arena: 'out, + 'tok: 'out, + { + self.0.borrow_mut(token.as_mut()) + } } -macro_rules! lens_t { +macro_rules! own_t { ($T:ty) => { - Lens<'tok, 'brand, 'arena, $T> + Own<'brand, 'arena, $T> } } -macro_rules! lens { +macro_rules! own { ($T:ident) => { - Lens<'tok, 'brand, 'arena, $T<'brand, 'arena, V>> + Own<'brand, 'arena, $T<'brand, 'arena, V>> }; } @@ -90,7 +110,7 @@ impl<'brand, 'arena, T> Own<'brand, 'arena, T> { } } -impl<'brand, 'arena, T> Deref for Own<'brand, 'arena, T> { +impl<'brand, 'arena, T> Deref for own_t!(T) { type Target = ptr_t!(T); fn deref(&self) -> &Self::Target { @@ -98,18 +118,19 @@ impl<'brand, 'arena, T> Deref for Own<'brand, 'arena, T> { } } -/* -trait EntityFree<'brand, 'arena, V> { - fn free(self, dcel: &mut Dcel<'brand, 'arena, V>); -}*/ - pub type Id = Option; -/* -pub trait Lens<'tok, 'brand, 'arena, T> { - fn item(self) -> ptr_t!(T); - fn token(self) -> &'tok GhostToken<'brand>; -}*/ +macro_rules! lens_t { + ($T:ty) => { + Lens<'tok, 'brand, 'arena, $T> + } +} + +macro_rules! lens { + ($T:ident) => { + Lens<'tok, 'brand, 'arena, $T<'brand, 'arena, V>> + }; +} pub struct Lens<'tok, 'brand, 'arena, T> { pub item: ptr_t!(T), @@ -229,29 +250,16 @@ impl<'tok, 'brand, 'arena, T: Entity<'brand, 'arena>> DoubleEndedIterator } } -struct ShortDebugEntityIterator<'tok, 'brand, 'arena, T, I> +struct ShortDebugEntityIterator(I); +impl<'tok, 'brand, 'arena, T, I> Debug for ShortDebugEntityIterator where 'brand: 'tok + 'arena, - T: 'arena, - I: Iterator + Clone, -{ - type_name: &'static str, - iter: I, -} - -impl<'tok, 'brand, 'arena, T, I> Debug for ShortDebugEntityIterator<'tok, 'brand, 'arena, T, I> -where - 'brand: 'tok + 'arena, - T: Entity<'brand, 'arena>, + T: Entity<'brand, 'arena> + 'arena, I: Iterator + Clone, { fn fmt(&self, f: &mut Formatter) -> fmt::Result { f.debug_list() - .entries( - self.iter - .clone() - .map(|x| short_debug(self.type_name, x.id())), - ) + .entries(self.0.clone().map(|x| short_debug(T::type_name(), x.id()))) .finish() } } @@ -260,6 +268,8 @@ where trait Entity<'brand, 'arena>: Eq { type Init; + fn type_name() -> &'static str; + fn new(id: usize, init: Self::Init) -> Self; fn is_alive(&self) -> bool { @@ -343,6 +353,10 @@ macro_rules! entity { impl<'brand, 'arena, V> Entity<'brand, 'arena> for $T<'brand, 'arena, V> { type Init = $arg_ty; + fn type_name() -> &'static str { + stringify!($T) + } + fn get_id(&self) -> Id { self.id } @@ -617,7 +631,7 @@ impl<'brand, 'arena, T: Entity<'brand, 'arena>> Allocator<'brand, 'arena, T> { *ptr.borrow_mut(token.as_mut()) = t; ptr } else { - Own::unsafe_make_owned(GhostCell::from_mut(self.arena.alloc(t))) + Own::unsafe_make_owned(Ptr(GhostCell::from_mut(self.arena.alloc(t)))) } } @@ -828,7 +842,7 @@ impl<'brand, 'arena, V: Debug> Dcel<'brand, 'arena, V> { he1.set_edge(self, *edge); he2.set_edge(self, *edge); - (edge, [&he1, &he2]) + (edge, [he1, he2]) } #[inline(always)] @@ -1346,10 +1360,7 @@ fn main() { println!("{:?}", op.vertices[1].lens(&dcel)); println!("{:?}", op.loop_.lens(&dcel)); dbg!(op.loop_.iter_half_edges(&dcel).rev().collect::>()); - dbg!(ShortDebugEntityIterator { - type_name: "HalfEdge", - iter: op.loop_.iter_half_edges(&dcel), - }); + dbg!(ShortDebugEntityIterator(op.loop_.iter_half_edges(&dcel))); println!("{:?}", op.face.lens(&dcel)); println!("{:?}", op.shell.lens(&dcel)); -- cgit v1.2.3