Skip to content

Commit 713074f

Browse files
committed
fix: move Box reexport to ngx::allocator
Protect allocator_api2::alloc::Global reexport with "alloc". Reimplement allocator_api2::unsize_box, as the original macro is not reexportable.
1 parent 718d9ca commit 713074f

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/allocator.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ use ::core::alloc::Layout;
1111
use ::core::mem;
1212
use ::core::ptr::{self, NonNull};
1313

14-
pub use allocator_api2::alloc::{AllocError, Allocator, Global};
14+
pub use allocator_api2::alloc::{AllocError, Allocator};
1515

1616
#[cfg(feature = "alloc")]
17-
pub use allocator_api2::boxed;
17+
pub use allocator_api2::{alloc::Global, boxed::Box};
1818

1919
/// Explicitly duplicate an object using the specified Allocator.
2020
pub trait TryCloneIn: Sized {
@@ -81,3 +81,24 @@ mod impls {
8181
}
8282
}
8383
}
84+
85+
/// Allows turning a [`Box<T: Sized, A>`][Box] into a [`Box<U: ?Sized, A>`][Box] where `T` can be
86+
/// unsizing-coerced into a `U`.
87+
///
88+
/// See [allocator_api2::unsize_box] for an explanation why this macro is necessary.
89+
#[cfg(feature = "alloc")]
90+
#[doc(inline)]
91+
pub use crate::__unsize_box as unsize_box;
92+
93+
// We have to reimplement this macro because the original implementation is not reexportable.
94+
// Macro definitions float to the top of the crate, thus we also mark it as hidden and reexport
95+
// again from the right namespace.
96+
#[cfg(feature = "alloc")]
97+
#[doc(hidden)]
98+
#[macro_export]
99+
macro_rules! __unsize_box {
100+
( $boxed:expr $(,)? ) => {{
101+
let (ptr, alloc) = $crate::allocator::Box::into_raw_with_allocator($boxed);
102+
unsafe { $crate::allocator::Box::from_raw_in(ptr as *mut _, alloc) }
103+
}};
104+
}

0 commit comments

Comments
 (0)