From 213ee5ca45d9bb93b73c28bf246d3026811fafc7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bennet=20Ble=C3=9Fmann?= Date: Sun, 7 Jul 2024 14:36:53 +0200 Subject: [PATCH] add `AllocateInArena` as a Pivot for `arena_alloc!` so that the value type passed to `arena_alloc!` can differ from the `ArenaAllocated::Payload` type --- src/arena.rs | 24 +++++++++++++++++++++++- src/parser/lexer.rs | 1 - 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/arena.rs b/src/arena.rs index 75ac69f0..bb346810 100644 --- a/src/arena.rs +++ b/src/arena.rs @@ -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 +where + AllocFor: ArenaAllocated, +{ + fn arena_allocate(self, arena: &mut Arena) -> TypedArenaPtr; +} + +impl> AllocateInArena for P { + fn arena_allocate(self, arena: &mut Arena) -> TypedArenaPtr { + T::alloc(arena, self) + } +} + +/* apparently this overlaps the planket impl above somehow +impl>> AllocateInArena for P { + fn arena_allocate(self, arena: &mut Arena) -> TypedArenaPtr { + T::alloc(arena, ManuallyDrop::new(self)) + } +} +*/ + pub trait ArenaAllocated { type Payload: ?Sized; diff --git a/src/parser/lexer.rs b/src/parser/lexer.rs index d783c588..03f472b9 100644 --- a/src/parser/lexer.rs +++ b/src/parser/lexer.rs @@ -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::*; -- 2.54.0