Skip to content

Commit

Permalink
Merge pull request #216 from rust-lang/rust-1.35
Browse files Browse the repository at this point in the history
Add contents for Rust 1.35
  • Loading branch information
steveklabnik authored Jul 10, 2020
2 parents c435b3d + fa817ea commit 2e2a8f3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,6 @@
- [`?` operator in macros](rust-next/qustion-mark-operator-in-macros.md)
- [const fn](rust-next/const-fn.md)
- [Pinning](rust-next/pin.md)
- [No more FnBox](rust-next/no-more-fnbox.md)
- [Alternative Cargo Registries](rust-next/alternative-cargo-registries.md)
- [TryFrom and TryInto](rust-next/tryfrom-and-tryinto.md)
- [TryFrom and TryInto](rust-next/tryfrom-and-tryinto.md)
38 changes: 38 additions & 0 deletions src/rust-next/no-more-fnbox.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# No more FnBox

![Minimum Rust version: 1.35](https://img.shields.io/badge/Minimum%20Rust%20Version-1.35-brightgreen.svg)

The book used to have this code in Chapter 20, section 2:

```rust
trait FnBox {
fn call_box(self: Box<Self>);
}

impl<F: FnOnce()> FnBox for F {
fn call_box(self: Box<F>) {
(*self)()
}
}

type Job = Box<dyn FnBox + Send + 'static>;
```

Here, we define a new trait called `FnBox`, and then implement it for all
`FnOnce` closures. All the implementation does is call the closure. These
sorts of hacks were needed because a `Box<dyn FnOnce>` didn't implement
`FnOnce`. This was true for all three posibilities:

* `Box<dyn Fn>` and `Fn`
* `Box<dyn FnMut>` and `FnMut`
* `Box<dyn FnOnce>` and `FnOnce`

However, as of Rust 1.35, these traits are implemented for these types,
and so the `FnBox` trick is no longer required. In the latest version of
the book, the `Job` type looks like this:

```rust
type Job = Box<dyn FnOnce() + Send + 'static>;
```

No need for all that other code.

0 comments on commit 2e2a8f3

Please sign in to comment.