fitzgen / bumpalo

A fast bump allocation arena for Rust
https://docs.rs/bumpalo
Apache License 2.0
1.41k stars 111 forks source link

Use allocator-api through allocator-api2 crate #201

Closed zakarumych closed 1 year ago

zakarumych commented 1 year ago

This change allows to use mirror of the allocator-api on stable channel. Types imported from allocator-api2 are reexports from core and alloc crates when "nightly" feature is enabled. Otherwise allocator-api2 defines types to mimic unstable core and alloc types and functions, stable stuff is re-exported.

The goal is to add support for allocator-api2 into as many crates as possible. The bumpalo crate is first on the list.

zakarumych commented 1 year ago

Apparently this would require bumping MSRV to 1.63

zakarumych commented 1 year ago

How do you suggest enabling "nightly" in allocator-api2?

zakarumych commented 1 year ago

I found a problem with the approach in allocator-api2 - replacing stable mirror API with unstable API re-export with a feature makes all dependent crates manually enable unstable feature(allocator_api). If some other crate would enable "allocator-api2/nightly" feature and not enable "bumpalo/nightly" then bumpalo won't compile.

So I changed it. allocator-api2 is now always stable mirror and crates choose from using allocator-api2 and alloc crate, picking alloc when they enable nightly Rust feature allocator_api.

zakarumych commented 1 year ago

I tried to forward "nightly" feature back from allocator-api2 into bumpalo, but it requires other unstable features.

zakarumych commented 1 year ago

It was made like this originally.

However this doesn't work since user would have to enable unstable #![feature(allocator_api)] if any crate enables "nightly" in allocator-api2.

I found no way to enable #![feature(allocator_api)] conditionally based on allocator-api2 features. Tried using macro, but inner macro attributes are unstable. It's also impossible to get custom cfg flag from dependency.

I tried different route and make blanket impls of allocator_api2::Allocator for core::alloc::Allocator types. This way allocators that implement core::alloc::Allocator would be compatible. Allocators that use allocator-api2 would implement core::alloc::Allocator with "nightly" feature as well.

Unfortunately I got stuck on conflicting impls for references. Tried using specialization feature to resolve the conflict, but with no luck.

So currently PR is blocked on resolving this issue. I'll return to it later when I have free time again.

zakarumych commented 1 year ago

Ok. I think it should be consistent and usable now. allocator-api2 re-exports core, alloc and std with "nightly" feature. Other crates that use it should have means to enable #![feature(allocator_api)] to avoid compilation errors on nightly channel. bumpalo does. Even if direct dependent crates won't enable it since it doesn't care about nightly, indirect dependent crates would be able to enable it if they encounter compilation error when some other crate in the tree enable "allocator-api2/nightly".

fitzgen commented 1 year ago

It does look like this needs a little rebase to deal with CHANGELOG.md conflicts, and in that case can you also remove the now-obsolete bits in the changelog about the "nightly" feature? I was going to do that myself, but since it needs a rebase anyways...

zakarumych commented 1 year ago

It's ready for merge now

zakarumych commented 1 year ago

@fitzgen can you provide ETA for next release with this?

fitzgen commented 1 year ago

Published!