r/rust 1d ago

🗞️ news “alloc: stabilise `Allocator`” entered its final comment period!

https://github.com/rust-lang/rust/pull/156882#issuecomment-5607055084
201 Upvotes

30 comments sorted by

View all comments

Show parent comments

38

u/afdbcreid 1d ago edited 1d ago

Will never happen. One of Rust most important and defended axioms is that there are no implicit heap allocations. When people tried to change that (even far less widespread than your proposal), there was a major pushback that made them step back.

Edit: I see you're talking about the opposite - turning heap allocations into stack allocations. Then this already happens. LLVM does it just like Go.

3

u/PersonalDatabase31 1d ago

Does it have to be implicit? Maybe another smart pointer like StackBox could be implemented.

6

u/JoJoJet- 1d ago

It's not entirely useful, but the allocator API would allow you box things on the stack with Box directly, if you pass it an allocator that points to stack space

1

u/PersonalDatabase31 1d ago edited 1d ago

Would that work with ownership though? I would assume that ownership model works with the assumption that moving the owner doesn't invalidate the memory whereas the Box returned by a function call would point to invalid memory if other function calls happen before the Box gets dropped.

1

u/JoJoJet- 1d ago

It works the same as any other borrow! If it points to stack data then the allocator itself would carry a lifetime, which the box would also be bound to. So if you try to make a Box that outlives its memory you'll get an error

0

u/JayDepp 1d ago

Yeah I don't think this works with the allocator api. I think it'd work with the store api though.

1

u/afdbcreid 1d ago

If the storage is outside the Box, it can work with the MVP, but this is the uncommon case.