Add fn into_raw_with_allocator
to Rc/Arc/Weak. by zachs18 · Pull Request #125093 · rust-lang/rust (original) (raw)
Split out from #119761
Add fn into_raw_with_allocator
for Rc
/rc::Weak
1/Arc
/sync::Weak
.
- Pairs with
from_raw_in
(which already exists on all 4 types). - Name matches
Box::into_raw_with_allocator
. - Associated fns on
Rc
/Arc
, methods onWeak
s. Future PR/ACP
As a follow-on to this PR, I plan to make a PR/ACP later to move into_raw(_parts)
from Container<_, A: Allocator>
to only Container<_, Global>
(where Container
= Vec
/Box
/Rc
/rc::Weak
/Arc
/sync::Weak
) so that users of non-Global
allocators have to explicitly handle the allocator when using into_raw
-like APIs.
The current behaviors of stdlib containers are inconsistent with respect to what happens to the allocator when into_raw
is called (which does not return the allocator)
Type | into_raw currently callable with | behavior of into_raw |
---|---|---|
Box | any allocator | allocator is dropped |
Vec | any allocator | allocator is forgotten |
Arc/Rc/Weak | any allocator | allocator is forgotten(Arc) (sync::Weak) (Rc) (rc::Weak) |
In my opinion, neither implicitly dropping nor implicitly forgetting the allocator is ideal; dropping it could immediately invalidate the returned pointer, and forgetting it could unintentionally leak memory. My (to-be) proposed solution is to just forbid calling into_raw(_parts)
on containers with non-Global
allocators, and require calling into_raw_with_allocator
(/Vec::into_raw_parts_with_alloc
)
Footnotes
- Technically,
rc::Weak::into_raw_with_allocator
is not newly added, as it was modified and renamed fromrc::Weak::into_raw_and_alloc
. ↩