-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the boxed!() macro to "de-magic" box syntax #3057
Conversation
Given that As somebody who is not part of the compiler team though, the difference between unstable syntax and macros is basically zero, so this just looks like churn for the sake of churn. |
Doing this as an RFC so that it has a clear path to stability is fine, in terms of process. However, I also don't see how this fixes much. |
I would prefer to add magic syntax and remove it in a future edition than add a macro with a magic implementation and be stuck with it in the std library forever. I do like the motivation of trying to hide some of the magic though. |
Personally, I thought that the main reason why The thing I do like about using a |
@clarfonthey |
Oh, I guess I should have clarified -- while |
Stabilizing |
The [`vec`](https://doc.rust-lang.org/src/alloc/macros.rs.html#37-47) macro is implemented in terms of it, | ||
and several other parts of the [standard library](https://doc.rust-lang.org/src/alloc/sync.rs.html#314-318) | ||
use it. | ||
[`Box::new`](https://doc.rust-lang.org/std/boxed/struct.Box.html#method.new) is the stable alternative to this; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we think of any real-world examples that aren't already served by the vec!
macro that are likely to be big enough to overflow the stack?
This function for example:
fn big_boxed_slice(init: u8) -> Box<[u8]> {
Box::new([init; 10485760])
}
allocates a 10MiB array, and (currently) overflows in both debug and release mode.
The alternative with box expr
(or boxed!
) would be:
fn big_boxed_syntax_slice(init: u8) -> Box<[u8]> {
boxed!([init; 10485760])
}
which doesn't overflow, but is equivalent to the already stable:
fn big_vec(init: u8) -> Box<[u8]> {
vec![init; 10485760].into_boxed_slice()
}
I think I follow this train of thought that I don't think an intentionally temporary Maybe this has come up before but have we ever thought about trying to make |
We do have #2884 for in-place construction at library level, at the cost of spec-guaranteed RVO. // the closure's return is guaranteed to be written to box,
// rather than written to stack then memcpy into the box
let mut hectapus = Box::with(|| Hectapus { arm_lengths: [0; 100] }); (I also think |
They are equivalent, aren't they? The source code of pub fn new(x: T) -> Self {
box x
} |
Nope, because what's special about Note that LLVM's optimizer will sometimes optimize away the stack object, making |
Ah, I see what you mean. Like in the sense of |
The @rfcbot close |
Team member @m-ou-se has proposed to close this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period, with a disposition to close, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. The RFC is now closed. |
Box syntax (e.g.
box x
) is controversial due to its "magic" status in the language. This RFC proposes the addition of aboxed
macro that would remove the magic syntax from this feature (e.g.boxed!(x)
).Rendered