rust-lang / wg-allocators

Home of the Allocators working group: Paving a path for a standard set of allocator traits to be used in collections!
http://bit.ly/hello-wg-allocators
203 stars 9 forks source link

Runtime allocators #83

Open ivannp opened 3 years ago

ivannp commented 3 years ago

How is the scenario where the allocator is, and can only be, determined at runtime supported? Not too advanced in Rust, so I might be missing something obvious.

I am kind of looking for the equivalent to the C++ 17 polymorphic allocators. In this scenario, the allocator can literally be dropped from the template definition, and it's determined at runtime, via the constructor. This is common scenario in systems using different heaps for different allocations. In these cases, the common scenario is to have a single allocator type, with different runtime configs:

    vector<int> v(my_polimorphic_allocator(theHeap));
RustyYato commented 3 years ago

Given that Allocator is almost object-safe, yes it should. Currently, the only thing holding it back is Allocator::by_ref which uses Self in its signature. If that is given a where Self: Sized bound, then Allocator will become object safe, so you can use it polymorphically.

Amanieu commented 3 years ago

Please do send a PR for this, otherwise we might forget about it before stabilizing Allocator.

chorman0773 commented 3 years ago

Would the allocator type in this case be &dyn Allocator, or something else?

Amanieu commented 3 years ago

&dyn Allocator, Box<dyn Allocator>, Arc<dyn Allocator>, etc.

RustyYato commented 3 years ago

Since rust-lang/rust#81730 was merged, can this issue be closed?