Add Box::leak<'a>(Box<T>) -> &'a mut T where T: 'a#45881
Add Box::leak<'a>(Box<T>) -> &'a mut T where T: 'a#45881bors merged 7 commits intorust-lang:masterfrom
Conversation
|
N.B. this exists in the ecosystem: https://crates.io/crates/leak Is |
|
See also: rust-lang/rfcs#1233 |
|
It's not unsafe. There's a single mutable reference, the lifetime is irrelevant |
|
Hmm, so @alexcrichton said then in rust-lang/rfcs#1233:
Since then the |
And that crate is transitively depended on by |
|
@cramertj So I can't judge if this means that the feature is used very little or not... =) |
|
I think this is a nice addition. @rfcbot fcp merge |
|
Team member @sfackler has proposed to merge this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once these reviewers reach consensus, 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. |
|
AFAIK there's no soundness holes from this, but we can always double check during stabilization! |
|
I wonder if there is no risk of BC break in event this function would be replaced with this. It may turn out that the API may be expanded in the future to allow other lifetimes than fn leak<'a>(b: Box<T>) -> &'a mut T
where
T: 'a,
{
unsafe { &mut *Box::into_raw(b) }
} |
|
I have two points that people may be concerned about and it would be nice to have clear answers about them:
|
|
@xfix So; talked it over on #rust-lang and it seems strictly more powerful - so I'll change it to your proposal
Given this program: fn leak<T>(b: Box<T>) -> &'static mut T {
unsafe { &mut *Box::into_raw(b) }
}
fn main() {
let static_ref: &'static mut &mut usize = {
let mut local_variable = 41;
let boxed: Box<&mut usize> = Box::new(&mut local_variable);
leak(boxed)
};
**static_ref += 1;
assert_eq!(**static_ref, 42);
}we, rightly, get: So a bound
Well, then |
|
Updated to |
|
ping @BurntSushi for your ticky box here! |
| /// | ||
| /// # Examples | ||
| /// | ||
| /// Simple usage: |
There was a problem hiding this comment.
Is a documentation line simply saying "Simple usage" before an example needed?
There was a problem hiding this comment.
I used it to differentiate between "simple usage" and "unsized data" - I think it makes it a bit more readable - but I can always remove it.
|
If |
|
🔔 This is now entering its final comment period, as per the review above. 🔔 |
|
@BurntSushi I generalized it to any lifetime |
|
@Centril Ah great! |
|
@Centril mind updating the issue number reference in the unstable tag? After that this should be good to go! |
|
@alexcrichton Done, issue is #46179 . |
|
@bors r=alexcrichton |
|
📌 Commit bc18d99 has been approved by |
Add Box::leak<'a>(Box<T>) -> &'a mut T where T: 'a
Adds:
```rust
impl<T: ?Sized> Box<T> {
pub fn leak<'a>(b: Box<T>) -> &'a mut T where T: 'a {
unsafe { &mut *Box::into_raw(b) }
}
}
```
which is useful for when you just want to put some stuff on the heap and then have a reference to it for the remainder of the program.
r? @sfackler
cc @durka
|
☀️ Test successful - status-appveyor, status-travis |
Adds:
which is useful for when you just want to put some stuff on the heap and then have a reference to it for the remainder of the program.
r? @sfackler
cc @durka