From 7972633ae02220849b0dcf423e66a8289ee27cf1 Mon Sep 17 00:00:00 2001 From: bluurryy <164359728+bluurryy@users.noreply.github.com> Date: Thu, 31 Oct 2024 11:03:28 +0100 Subject: [PATCH] refactor: moved `BumpAllocator` into its own file --- src/bump_allocator.rs | 29 +++++++++++++++++++++++++++++ src/lib.rs | 28 ++-------------------------- 2 files changed, 31 insertions(+), 26 deletions(-) create mode 100644 src/bump_allocator.rs diff --git a/src/bump_allocator.rs b/src/bump_allocator.rs new file mode 100644 index 0000000..e1543c6 --- /dev/null +++ b/src/bump_allocator.rs @@ -0,0 +1,29 @@ +use allocator_api2::alloc::Allocator; + +use crate::{BaseAllocator, Bump, BumpScope, MinimumAlignment, SupportedMinimumAlignment}; + +/// An allocator that allows `grow(_zeroed)`, `shrink` and `deallocate` calls with pointers that were not allocated by this allocator. +/// +/// This trait is used for [`BumpBox::into_box`](BumpBox::into_box) to allow safely converting a `BumpBox` into a `Box`. +/// +/// # Safety +/// - `grow(_zeroed)`, `shrink` and `deallocate` must be ok to be called with a pointer that was not allocated by this Allocator +pub unsafe trait BumpAllocator: Allocator {} + +unsafe impl BumpAllocator for &A {} + +unsafe impl BumpAllocator + for BumpScope<'_, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED> +where + MinimumAlignment: SupportedMinimumAlignment, + A: BaseAllocator, +{ +} + +unsafe impl BumpAllocator + for Bump +where + MinimumAlignment: SupportedMinimumAlignment, + A: BaseAllocator, +{ +} diff --git a/src/lib.rs b/src/lib.rs index aa3f9ba..27df349 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -263,6 +263,7 @@ extern crate alloc; mod allocator; mod bump; mod bump_align_guard; +mod bump_allocator; /// Contains [`BumpBox`] and associated types. mod bump_box; #[cfg(feature = "std")] @@ -304,6 +305,7 @@ pub use allocator_api2; use allocator_api2::alloc::handle_alloc_error; use allocator_api2::alloc::{AllocError, Allocator}; pub use bump::Bump; +pub use bump_allocator::BumpAllocator; pub use bump_box::BumpBox; #[cfg(feature = "std")] pub use bump_pool::{BumpPool, BumpPoolGuard}; @@ -490,32 +492,6 @@ fn exact_size_iterator_bad_len() -> ! { panic!("ExactSizeIterator did not return as many items as promised") } -/// An allocator that allows `grow(_zeroed)`, `shrink` and `deallocate` calls with pointers that were not allocated by this allocator. -/// -/// This trait is used for [`BumpBox::into_box`](BumpBox::into_box) to allow safely converting a `BumpBox` into a `Box`. -/// -/// # Safety -/// - `grow(_zeroed)`, `shrink` and `deallocate` must be ok to be called with a pointer that was not allocated by this Allocator -pub unsafe trait BumpAllocator: Allocator {} - -unsafe impl BumpAllocator for &A {} - -unsafe impl BumpAllocator - for BumpScope<'_, A, MIN_ALIGN, UP, GUARANTEED_ALLOCATED> -where - MinimumAlignment: SupportedMinimumAlignment, - A: BaseAllocator, -{ -} - -unsafe impl BumpAllocator - for Bump -where - MinimumAlignment: SupportedMinimumAlignment, - A: BaseAllocator, -{ -} - /// Associates a lifetime to a wrapped type. /// /// This is used for [`BumpBox::into_box`] to attach a lifetime to the `Box`.