add AllocateInArena as a Pivot for arena_alloc!

so that the value type passed to `arena_alloc!` can differ from the `ArenaAllocated::Payload` type
This commit is contained in:
Bennet Bleßmann
2024-07-07 14:36:53 +02:00
parent 74720d4d2e
commit 213ee5ca45
2 changed files with 23 additions and 2 deletions

View File

@@ -21,6 +21,7 @@ use std::fmt;
use std::fmt::Debug; use std::fmt::Debug;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::mem; use std::mem;
use std::mem::ManuallyDrop;
use std::net::TcpListener; use std::net::TcpListener;
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
use std::ptr; use std::ptr;
@@ -32,7 +33,7 @@ use std::sync::RwLock;
macro_rules! arena_alloc { macro_rules! arena_alloc {
($e:expr, $arena:expr) => {{ ($e:expr, $arena:expr) => {{
let result = $e; 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 { pub trait ArenaAllocated {
type Payload: ?Sized; type Payload: ?Sized;

View File

@@ -1,6 +1,5 @@
use lexical::parse_lossy; use lexical::parse_lossy;
use crate::arena::ArenaAllocated;
use crate::atom_table::*; use crate::atom_table::*;
pub use crate::machine::machine_state::*; pub use crate::machine::machine_state::*;
use crate::parser::ast::*; use crate::parser::ast::*;