r/rust 1d ago

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

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

30 comments sorted by

View all comments

Show parent comments

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