-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Tracking Issue for box_into_inner
#80437
Comments
How come it's a function and not a method? |
When looking at how to turn a box back into a stack owned value, I find this function, but in my case the |
Somewhat minor, but I like that |
@rfcbot merge |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
Team member @m-ou-se has proposed to merge this. The next step is review by the rest of the tagged team members: Concerns:
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. 🔔 |
I think I would want to see better documentation of this before I would be comfortable stabilizing it. The current example code given for this function is: let c = Box::new(5);
assert_eq!(Box::into_inner(c), 5); which is not a thing that we want people to use this function for, right? We would prefer Ideally the documentation would mention As currently written, it's not clear whether this function has been added because we want people to stop writing @rfcbot concern convey the use case in documentation |
Not in the sense that it produces incorrect code. |
Why would |
The same argument could be made for number conversion. Do I use the |
@Aandreba |
Not always. If I want to cast from |
I wasn't making an argument though. You asked for a cost. I gave you one. Do we have other "decision paralysis" costs in the standard library? Yes! Absolutely! There's a difference between the questions "what are the downsides" and "should we do this." You said, "Also, what downside could stabilizing into_inner have?" Which is clearly an instance of the former, not the latter. Otherwise, this conversation is honestly getting pretty frustrating to participate in, so I'm going to bow out until there's something more significant to respond to. I'll say that I am not against this API landing, but I am not strongly in favor of it either. |
That's true. The point I'm trying to make is that I don't see how the decision paralysis cost outweighs the benefit in code readability. And it's also a very small decision for which you'll have a goto answer by the second time you encounter it. |
I also think that this function is better served in a more generic |
I see |
I'm curious what makes the move-on-derefence behavior bad for On the topic of this issue, calling let mut s = Box::new(String::new());
s.push('a'); // The boxed value is automatically dereferenced here with DerefMut's implementation |
Agreed. But the point was being made that with
Well, I don't think anybody is arguing we should remove the special behavior
The problem discussed here is not about taking references of the values inside a box. The But when time comes to take ownership of the boxed value in the stack, I (and many others) think |
This is just syntax. Syntax can be changed in editions, and I have heard people genuinely suggesting we should remove the
Logically, My personal issue is that this behaviour is currently special to Box. I don't mind language features, but we typically have a trait that encodes those behaviours:
|
Has there been any further discussion about this? I (and people I've taught Rust to) have gotten quite surprised more than once at |
Just to add my two cents, I just today found out about how "unbox" has to be spelled For some reason, in all my use cases, I've never had a reason to reach for a Given that prior context, my immediate thought was "oh, obviously I wouldn't consider myself a Rust "expert". But, my point is, I'm certainly not a noob at this point, either, and I found this behavior surprising. In fact, surprisingly surprising. One of my (many) favorite parts about Rust is the consistency, coherence, and wide-adoption of naming convention patterns. They're short, sweet, descriptive, readable, say-able, and practically self-documentation in many cases. That's probably the main reason why I expected Still anecdotal/personal-preference, but I figured it might be relevant that the issue isn't unique or specific to newcomers. |
I agree with all of what you said. I never thought unboxing a |
Currently, the If the intended way to convert a box into its inner value is Note that updating the documentation for |
@ais523 The second example in the |
@BurntSushi: The example given actually copies a value out of the box back to the stack, using the let boxed: Box<u8> = Box::new(5);
let val: u8 = *boxed;
println!("{}", val);
println!("{}", boxed); If the As such, the example isn't very instructive to people who are trying to learn how |
@ais523 Sorry, I'm not understanding the problem here. The code example in your comment doesn't match what's in the docs. The example in the docs works regardless of whether the boxed type is I think this sort of feedback is more helpful in the form of a PR with edits to the status quo. That will make your feedback extremely concrete, and there will be something specific to compare with the status quo. |
I wonder if we're looking at different documentation? I'm looking at https://doc.rust-lang.org/std/boxed/index.html in which the example in question is let boxed: Box<u8> = Box::new(5);
let val: u8 = *boxed; It is possible that this has already been fixed recently, and the fix simply hasn't reached the public documentation yet. (I also can't submit pull requests via Github because doing so would force my account to use two-factor authentication, and I don't have a smartphone or other similar device to use as the second factor.) |
Yes. That's the same example I'm looking at. You can replace |
So the problem is that the example doesn't convey the information that it would still work after replacing use std::rc::Rc;
let boxed: Rc<u8> = Rc::new(5);
let val: u8 = *boxed; As such, the example fails to inform readers that the special case exists – because it would work even without it (and in fact doesn't make use of it). |
OK, I'm not sure I really agree with that. But I don't really know how to engage with you further without looking at a concrete set of edits to the status quo. I don't know how to resolve your issue with PRs. If you can't create one, perhaps you can find someone to do it on your behalf. I also think this is meandering into off-topic territory for this specific issue. I might suggest creating a new one because "documentation for |
I am trying to think of a concrete set of fixes, but the problem is actually the same one that blocked stablisation of this issue earlier: In case it helps someone: the situation in which I came across this was that I was trying to convert a |
Also there's been |
We discussed this tracking issue in today's standard library API team meeting. The thing it's stuck on is still a PR for #80437 (comment). Some of the comments following that one have some additional suggestions. |
#[cfg(some_condition)]
/// Some custom box type that we're using. Maybe we're using a custom allocator or maybe we don't want noalias.
struct CustomBox<T>(/* TODO: Implement this. */ PhantomData<T>);
#[cfg(some_condition)]
impl<T> CustomBox<T> {
fn into_inner(this: Self<T>) -> T {
todo!();
}
}
#[cfg(some_condition)]
type MyBox<T> = CustomBox<T>;
#[cfg(not(some_condition))]
type MyBox<T> = Box<T>;
fn foo<T>(t: MyBox<T>) {
// Get inner value.
let inner = MyBox::into_inner(t);
} One use case I haven't seen mentioned is when you have a type alias that may or may not point to |
Feature gate:
#![feature(box_into_inner)]
This is a tracking issue for consuming a
Box
and returning its wrapped value.The language actually supports
*x
as a special case. However it can't be generalized to other types easily. It also doesn't consume theBox
value immediately ifT
isCopy
, which is quite a footgun.An explicit version is added here to make the API more consistent with
Pin
,Cell
,RefCell
etc.Public API
Steps / History
Box::into_inner
. #80438Box::into_inner
#98523Unresolved Questions
The text was updated successfully, but these errors were encountered: