]> Repositorios git - scryer-prolog.git/commitdiff
add `AllocateInArena` as a Pivot for `arena_alloc!`
authorBennet Bleßmann <[email protected]>
Sun, 7 Jul 2024 12:36:53 +0000 (14:36 +0200)
committerBennet Bleßmann <[email protected]>
Sun, 7 Jul 2024 12:36:53 +0000 (14:36 +0200)
so that the value type passed to `arena_alloc!` can differ from the `ArenaAllocated::Payload` type

src/arena.rs
src/parser/lexer.rs

index 75ac69f00a251203b37c19288117e3fc40b05097..bb346810ebe7ff341744b52637ea0bdd55d342d7 100644 (file)
@@ -21,6 +21,7 @@ use std::fmt;
 use std::fmt::Debug;
 use std::hash::{Hash, Hasher};
 use std::mem;
+use std::mem::ManuallyDrop;
 use std::net::TcpListener;
 use std::ops::{Deref, DerefMut};
 use std::ptr;
@@ -32,7 +33,7 @@ use std::sync::RwLock;
 macro_rules! arena_alloc {
     ($e:expr, $arena:expr) => {{
         let result = $e;
-        ArenaAllocated::alloc($arena, result)
+        $crate::arena::AllocateInArena::arena_allocate(result, $arena)
     }};
 }
 
@@ -355,6 +356,27 @@ where
     }
 }
 
+pub trait AllocateInArena<AllocFor>
+where
+    AllocFor: ArenaAllocated,
+{
+    fn arena_allocate(self, arena: &mut Arena) -> TypedArenaPtr<AllocFor>;
+}
+
+impl<P, T: ArenaAllocated<Payload = P>> AllocateInArena<T> for P {
+    fn arena_allocate(self, arena: &mut Arena) -> TypedArenaPtr<T> {
+        T::alloc(arena, self)
+    }
+}
+
+/* apparently this overlaps the planket impl above somehow
+impl<P, T: ArenaAllocated<Payload = ManuallyDrop<P>>> AllocateInArena<T> for P {
+    fn arena_allocate(self, arena: &mut Arena) -> TypedArenaPtr<T> {
+        T::alloc(arena, ManuallyDrop::new(self))
+    }
+}
+*/
+
 pub trait ArenaAllocated {
     type Payload: ?Sized;
 
index d783c5885b7cd65a616c5a576d19057ed8cd8a4e..03f472b94f69cba233e0cbdf2d9b2d6c1110290a 100644 (file)
@@ -1,6 +1,5 @@
 use lexical::parse_lossy;
 
-use crate::arena::ArenaAllocated;
 use crate::atom_table::*;
 pub use crate::machine::machine_state::*;
 use crate::parser::ast::*;