Skip to content

Commit

Permalink
Merge pull request #201 from zakarumych/main
Browse files Browse the repository at this point in the history
Use allocator-api through allocator-api2 crate
  • Loading branch information
fitzgen committed May 11, 2023
2 parents faac800 + 81f5b92 commit cfe944f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
build:
strategy:
matrix:
rust_channel: ["stable", "beta", "nightly", "1.60.0"]
rust_channel: ["stable", "beta", "nightly", "1.63.0"]
feature_set: ["--features collections,boxed"]
include:
- rust_channel: "nightly"
Expand Down
11 changes: 8 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ Released YYYY-MM-DD.

### Added

* TODO (or remove section if none)
* New `"allocator-api2"` feature enables use of allocator API on stable.
This feature uses a crate that mirrors the API of
unstable `allocator_api` feature.
If feature is enabled, references to `Bump` implement
`allocator_api2::Allocator`.
This allows `Bump` to be used as allocator for collection types from
`allocator-api2` and any other crates that support `allocator-api2`.

### Changed

* The minimum supported Rust version (MSRV) is now 1.60.0.
* TODO (or remove section if none)
* The minimum supported Rust version (MSRV) is now 1.63.0.

### Deprecated

Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ name = "try_alloc"
path = "tests/try_alloc.rs"
harness = false

[dependencies]
allocator-api2 = { version = "0.2.8", default-features = false, optional = true, features = ["alloc"] }

[dev-dependencies]
quickcheck = "1.0.3"
criterion = "0.3.6"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ v.push(2);

#### Minimum Supported Rust Version (MSRV)

This crate is guaranteed to compile on stable Rust **1.60** and up. It might
This crate is guaranteed to compile on stable Rust **1.63** and up. It might
compile with older versions but that may change in any new patch release.

We reserve the right to increment the MSRV on minor releases, however we will
Expand Down
23 changes: 15 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,9 +23,13 @@ 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};

#[cfg(all(feature = "allocator-api2", not(feature = "allocator_api")))]
use allocator_api2::alloc::{AllocError, Allocator};

pub use alloc::AllocErr;

/// An error returned from [`Bump::try_alloc_try_with`].
Expand Down Expand Up @@ -1886,11 +1887,13 @@ unsafe impl<'a> alloc::Alloc for &'a Bump {
}
}

#[cfg(feature = "allocator_api")]
#[cfg(any(feature = "allocator_api", feature = "allocator-api2"))]
unsafe impl<'a> Allocator for &'a Bump {
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, 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)
}

Expand All @@ -1905,7 +1908,9 @@ unsafe impl<'a> Allocator for &'a Bump {
new_layout: Layout,
) -> Result<NonNull<[u8]>, 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)
}

Expand All @@ -1916,7 +1921,9 @@ unsafe impl<'a> Allocator for &'a Bump {
new_layout: Layout,
) -> Result<NonNull<[u8]>, 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)
}

Expand Down

0 comments on commit cfe944f

Please sign in to comment.