Skip to content

Conversation

@alextsugi
Copy link

@alextsugi alextsugi commented Nov 22, 2025

This adds support for custom allocators. Allows to control memory allocation for tasks by passing an allocator to new spawn_in, spawn_local_in, and spawn_unchecked_in functions and corresponding Builder methods hidden under the new feature flags.

It adds two different feature flags:

  • Nightly allocator API via the allocator_api feature.
  • Stable allocator API via the allocator-api2 feature.

The allocator is stored inline with the task and dropped when the raw task is destroyed.

Some concerns:

I saw this issue, and I understand the doubts about the API stability. However, I'd like to offer a few points to consider:

  • The allocator-api2 crate is designed for stable Rust and is already used by major crates (hashbrown, bumpalo, etc.).
  • The allocator_api feature only works on nightly builds, which makes it self-explanatory that it's unstable and that the
    API may change in the future. Alternatively, we could support only allocator-api2 and drop the nightly allocator_api feature entirely.

Additionally, these changes may require updates to some workflows, such as smol-rs/.github/.github/workflows/clippy.yml. If this PR is considered for merging, I'll make the required changes.

I should mention that I'm not an expert in low-level Rust, so there may be areas in the unsafe code or memory management that could be improved. And even if this PR isn't the right fit for the crate given the instability of allocator API, I hope it can at least serve as a reference for anyone who wants to support custom allocators in their own forks.

Adds support for custom allocators.
This enables users to control memory allocation for tasks by passing an allocator to new spawn_in, spawn_local_in, and spawn_unchecked_in functions.

Features:
  - Nightly allocator API via the `allocator_api` feature.
  - Stable allocator API via the `allocator-api2` feature.

The allocator is stored inline with the task and properly dropped when the task is destroyed.
@alextsugi alextsugi marked this pull request as draft November 22, 2025 18:36
@alextsugi alextsugi marked this pull request as ready for review November 28, 2025 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant