diff --git a/Cargo.toml b/Cargo.toml index 8f1277b..bf0772b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,6 +30,9 @@ name = "try_alloc" path = "tests/try_alloc.rs" harness = false +[dependencies] +allocator-api2 = { version = "0.2.6" } + [dev-dependencies] quickcheck = "1.0.3" criterion = "0.3.6" @@ -39,7 +42,7 @@ rand = "0.8.5" default = [] collections = [] boxed = [] -allocator_api = [] +allocator_api = ["allocator-api2/nightly"] # [profile.bench] # debug = true diff --git a/src/lib.rs b/src/lib.rs index db5d41b..0ff07cf 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,10 +2,7 @@ #![deny(missing_debug_implementations)] #![deny(missing_docs)] #![no_std] -#![cfg_attr( - feature = "allocator_api", - feature(allocator_api, nonnull_slice_from_raw_parts) -)] +#![cfg_attr(feature = "allocator_api", feature(allocator_api))] #[doc(hidden)] pub extern crate alloc as core_alloc; @@ -25,9 +22,8 @@ use core::mem; use core::ptr::{self, NonNull}; use core::slice; use core::str; -use core_alloc::alloc::{alloc, dealloc, Layout}; -#[cfg(feature = "allocator_api")] -use core_alloc::alloc::{AllocError, Allocator}; + +use allocator_api2::alloc::{alloc, dealloc, AllocError, Allocator, Layout}; pub use alloc::AllocErr; @@ -1886,11 +1882,12 @@ unsafe impl<'a> alloc::Alloc for &'a Bump { } } -#[cfg(feature = "allocator_api")] unsafe impl<'a> Allocator for &'a Bump { fn allocate(&self, layout: Layout) -> Result, AllocError> { self.try_alloc_layout(layout) - .map(|p| NonNull::slice_from_raw_parts(p, layout.size())) + .map(|p| unsafe { + NonNull::new_unchecked(ptr::slice_from_raw_parts_mut(p.as_ptr(), layout.size())) + }) .map_err(|_| AllocError) } @@ -1905,7 +1902,9 @@ unsafe impl<'a> Allocator for &'a Bump { new_layout: Layout, ) -> Result, AllocError> { Bump::shrink(self, ptr, old_layout, new_layout) - .map(|p| NonNull::slice_from_raw_parts(p, new_layout.size())) + .map(|p| unsafe { + NonNull::new_unchecked(ptr::slice_from_raw_parts_mut(p.as_ptr(), new_layout.size())) + }) .map_err(|_| AllocError) } @@ -1916,7 +1915,9 @@ unsafe impl<'a> Allocator for &'a Bump { new_layout: Layout, ) -> Result, AllocError> { Bump::grow(self, ptr, old_layout, new_layout) - .map(|p| NonNull::slice_from_raw_parts(p, new_layout.size())) + .map(|p| unsafe { + NonNull::new_unchecked(ptr::slice_from_raw_parts_mut(p.as_ptr(), new_layout.size())) + }) .map_err(|_| AllocError) }