Skip to content
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

Add contents for Rust 1.35 #216

Merged
merged 2 commits into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.