feat(allocator): add From API (#1908)

closes #1701
This commit is contained in:
Boshen 2024-01-06 12:45:27 +08:00 committed by GitHub
parent 24d209cdd4
commit a6d9356ffa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,4 +1,4 @@
use std::ops::Deref;
use std::{convert::From, ops::Deref};
mod arena;
@ -10,6 +10,12 @@ pub struct Allocator {
bump: Bump,
}
impl From<Bump> for Allocator {
fn from(bump: Bump) -> Self {
Self { bump }
}
}
impl Deref for Allocator {
type Target = Bump;
@ -17,3 +23,21 @@ impl Deref for Allocator {
&self.bump
}
}
#[cfg(test)]
mod test {
use std::ops::Deref;
use crate::Allocator;
use bumpalo::Bump;
#[test]
fn test_api() {
let bump = Bump::new();
let allocator: Allocator = bump.into();
#[allow(clippy::explicit_deref_methods)]
{
_ = allocator.deref();
}
}
}