From a6d9356ffaf9680e588ce40e9f38720db523f207 Mon Sep 17 00:00:00 2001 From: Boshen Date: Sat, 6 Jan 2024 12:45:27 +0800 Subject: [PATCH] feat(allocator): add `From` API (#1908) closes #1701 --- crates/oxc_allocator/src/lib.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/crates/oxc_allocator/src/lib.rs b/crates/oxc_allocator/src/lib.rs index dd27f411c..da6f1c14b 100644 --- a/crates/oxc_allocator/src/lib.rs +++ b/crates/oxc_allocator/src/lib.rs @@ -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 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(); + } + } +}