forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#59500 - crlf0710:boxed-closure-impls, r=cramertj
Unsized rvalues: implement boxed closure impls. (2nd try) This is a rebase of S-blocked-closed PR rust-lang#55431 to current master. LLVM has moved forward since then, so maybe we can check whether the new LLVM 8.0 version unblocked this work.
- Loading branch information
Showing
12 changed files
with
119 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# `fnbox` | ||
|
||
The tracking issue for this feature is [#28796] | ||
|
||
[#28796]: https://github.com/rust-lang/rust/issues/28796 | ||
|
||
------------------------ | ||
|
||
This had been a temporary alternative to the following impls: | ||
|
||
```rust,ignore | ||
impl<A, F> FnOnce for Box<F> where F: FnOnce<A> + ?Sized {} | ||
impl<A, F> FnMut for Box<F> where F: FnMut<A> + ?Sized {} | ||
impl<A, F> Fn for Box<F> where F: Fn<A> + ?Sized {} | ||
``` | ||
|
||
The impls are parallel to these (relatively old) impls: | ||
|
||
```rust,ignore | ||
impl<A, F> FnOnce for &mut F where F: FnMut<A> + ?Sized {} | ||
impl<A, F> FnMut for &mut F where F: FnMut<A> + ?Sized {} | ||
impl<A, F> Fn for &mut F where F: Fn<A> + ?Sized {} | ||
impl<A, F> FnOnce for &F where F: Fn<A> + ?Sized {} | ||
impl<A, F> FnMut for &F where F: Fn<A> + ?Sized {} | ||
impl<A, F> Fn for &F where F: Fn<A> + ?Sized {} | ||
``` | ||
|
||
Before the introduction of [`unsized_locals`][unsized_locals], we had been unable to provide the former impls. That means, unlike `&dyn Fn()` or `&mut dyn FnMut()` we could not use `Box<dyn FnOnce()>` at that time. | ||
|
||
[unsized_locals]: language-features/unsized-locals.html | ||
|
||
`FnBox()` is an alternative approach to `Box<dyn FnBox()>` is delegated to `FnBox::call_box` which doesn't need unsized locals. As we now have `Box<dyn FnOnce()>` working, the `fnbox` feature is going to be removed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
fn call_it<T>(f: Box<dyn FnOnce() -> T>) -> T { | ||
f() | ||
} | ||
|
||
fn main() { | ||
let s = "hello".to_owned(); | ||
assert_eq!(&call_it(Box::new(|| s)) as &str, "hello"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#![feature(fnbox)] | ||
|
||
use std::boxed::FnBox; | ||
|
||
fn call_it<T>(f: Box<dyn FnBox() -> T>) -> T { | ||
f() | ||
} | ||
|
||
fn main() { | ||
let s = "hello".to_owned(); | ||
assert_eq!(&call_it(Box::new(|| s)) as &str, "hello"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.