Skip to content

Commit 08dc37a

Browse files
committed
Add contents for Rust 1.35
Fixes #204
1 parent 82bec58 commit 08dc37a

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/SUMMARY.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,5 @@
9696
- [`literal` macro matcher](rust-next/literal-macro-matcher.md)
9797
- [`?` operator in macros](rust-next/qustion-mark-operator-in-macros.md)
9898
- [const fn](rust-next/const-fn.md)
99-
- [Pinning](rust-next/pin.md)
99+
- [Pinning](rust-next/pin.md)
100+
- [No more FnBox](rust-next/no-more-fnbox.md)

src/rust-next/no-more-fnbox.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# No more FnBox
2+
3+
![Minimum Rust version: 1.35](https://img.shields.io/badge/Minimum%20Rust%20Version-1.35-brightgreen.svg)
4+
5+
The book used to have this code in Chapter 20, section 2:
6+
7+
```rust
8+
trait FnBox {
9+
fn call_box(self: Box<Self>);
10+
}
11+
12+
impl<F: FnOnce()> FnBox for F {
13+
fn call_box(self: Box<F>) {
14+
(*self)()
15+
}
16+
}
17+
18+
type Job = Box<dyn FnBox + Send + 'static>;
19+
```
20+
21+
Here, we define a new trait called `FnBox`, and then implement it for all
22+
`FnOnce` closures. All the implementation does is call the closure. These
23+
sorts of hacks were needed because a `Box<dyn FnOnce>` didn't implement
24+
`FnOnce`. This was true for all three posibilities:
25+
26+
* `Box<dyn Fn>` and `Fn`
27+
* `Box<dyn FnMut>` and `FnMut`
28+
* `Box<dyn FnOnce>` and `FnOnce`
29+
30+
However, as of Rust 1.35, these traits are implemented for these types,
31+
and so the `FnBox` trick is no longer required. In the latest version of
32+
the book, the `Job` type looks like this:
33+
34+
```rust
35+
type Job = Box<dyn FnOnce() + Send + 'static>;
36+
```
37+
38+
No need for all that other code.

0 commit comments

Comments
 (0)