zegao
April 20, 2022, 6:56am
1
AFAIK, box_syntax is used to achieve direct heap allocation in a pluggable way thru lang items and thus with less compiler magic. so my naïve thought is why not another keyword for the opposite operation free to reveal more compiler implementation details to user? boxed.rs - source here only one explicit comment is confusing here.
alice
April 20, 2022, 7:00am
2
I would rather just have less magic. It can be implemented by calling the std::alloc::dealloc
method.
8 Likes
Note also that box syntax doesn't even guarantee on heap only (eg in debug), so you can still blow the stack: https://github.com/rust-lang/rust/issues/49733#issuecomment-621666613
(Oops, sorry, replied to the wrong comment!)
Note that these have all been unaccepted, and thus should not be treated as precedent:
rust-lang:master
← aidanhs:aphs-no-place-for-placement
opened 10:13PM - 18 Feb 18 UTC
Closes #22181, #27779. Effectively makes the assortment of placement RFCs (rust-… lang/rfcs#470, rust-lang/rfcs#809, rust-lang/rfcs#1228) 'unaccepted'. It leaves `box_syntax` and keeps the `<-` token as recognised by libsyntax.
------------------------
I don't know the correct process for unaccepting an unstable feature that was accepted as an RFC so...here's a PR.
Let me preface this by saying I'm not particularly happy about doing this (I know it'll be unpopular), but I think it's the most honest expression of how things stand today. I've been motivated by a [post on reddit](https://www.reddit.com/r/rust/comments/7wrqk2/when_will_box_and_placementin_syntax_be_stable/) which asks when these features will be stable - the features have received little RFC-style design work since the end of 2015 (~2 years ago) and leaving them in limbo confuses people who want to know where they're up to. Without additional design work that needs to happen (see the collection of unresolved questions later in this post) they can't really get stabilised, and I think that design work would be most suited to an RFC rather than (currently mostly unused) experimental features in Rust nightly.
I have my own motivations - it's very simple to 'defeat' placement in debug mode today and I don't want a placement in Rust that a) has no guarantees to work and b) has no plan for in-place serde deserialisation.
There's a quote in [1]: "Ordinarily these uncertainties might lead to the RFC being postponed. [The RFC seems like a promising direction hence we will accept since it] will thus give us immediate experience with the design and help in determining the best final solution.". I propose that there have been enough additional uncertainties raised since then that the original direction is less promising and we should be think about the problem anew.
(a historical note: the first mention of placement (under that name - uninit pointers were earlier) in an RFC AFAIK is [0] in late 2014 (pre-1.0). RFCs since then have built on this base - [1] is a comment in Feb 2015 accepting a more conservative design of the Place* traits - this is back when serde still required aster and seemed to break every other nightly! A lot has changed since then, perhaps placement should too)
------------------------
Edit: **you should read the [summary comment](https://github.com/rust-lang/rust/issues/27779#issuecomment-378416911) instead of the notes below.**
Concrete unresolved questions include:
- making placement work in debug mode [7]
- making placement work for serde/with fallible creation [5], [irlo2], [8]
- trait design:
- opting into not consuming the placer in `Placer::make_place` - [2]
- trait proliferation - [4] (+ others in that thread)
- fallible allocation - [3], [4] (+ others in that thread)
- support for DSTs/unsized structs (if at all) - [1], [6]
More speculative unresolved questions include:
- better trait design with in the context of future language features [irlo1] (Q11), [irlo3]
- interaction between custom allocators and placement [irlo3]
[0] https://github.com/rust-lang/rfcs/pull/470
[1] https://github.com/rust-lang/rfcs/pull/809#issuecomment-73910414
[2] https://github.com/rust-lang/rfcs/issues/1286
[3] https://github.com/rust-lang/rfcs/issues/1315
[4] https://github.com/rust-lang/rust/issues/27779#issuecomment-146711893
[5] https://github.com/rust-lang/rust/issues/27779#issuecomment-285562402
[6] https://github.com/rust-lang/rust/issues/27779#issuecomment-354464938
[7] https://github.com/rust-lang/rust/issues/27779#issuecomment-358025344
[8] https://github.com/rust-lang/rfcs/pull/1228#issuecomment-190825370
[irlo1] https://internals.rust-lang.org/t/placement-nwbi-faq-new-box-in-left-arrow/2789
[irlo2] https://internals.rust-lang.org/t/placement-nwbi-faq-new-box-in-left-arrow/2789/19
[irlo3] https://internals.rust-lang.org/t/lang-team-minutes-feature-status-report-placement-in-and-box/4646
2 Likes
Meanwhile, Box<T>
is still special in that you can dereference to move values out, even when they're !Copy
, and this will also free the allocation. This is what I would think of as "unboxing".
6 Likes
zegao
April 21, 2022, 6:26am
6
then I'm confused about the impl of Box::new here , why box is used here still?
jjpe
April 21, 2022, 6:46am
7
Because no maintenance has taken place yet to remove it.
zegao
April 21, 2022, 7:22am
8
H2CO3
April 21, 2022, 7:25am
10
Furthermore, Box
is not the only way to perform heap allocation in Rust. There are many other types (e.g. Rc
, String
, etc.) that also heap-allocate. Calling heap deallocation "unboxing" would thus be misleading.
Furthermore, there's no real justification for making heap allocation into a language feature even if Box
is special w.r.t. Deref
. It could just use Layout::allocate()
/Layout::deallocate()
and a raw pointer/NotNull<T>
– the allocation part is not part of the special behavior of Box.
5 Likes
system
Closed
July 20, 2022, 7:26am
11
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.