diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 1990b5dd..11f2a526 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -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) \ No newline at end of file diff --git a/src/rust-next/no-more-fnbox.md b/src/rust-next/no-more-fnbox.md new file mode 100644 index 00000000..c5e18f44 --- /dev/null +++ b/src/rust-next/no-more-fnbox.md @@ -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); +} + +impl FnBox for F { + fn call_box(self: Box) { + (*self)() + } +} + +type Job = Box; +``` + +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` didn't implement +`FnOnce`. This was true for all three posibilities: + +* `Box` and `Fn` +* `Box` and `FnMut` +* `Box` 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; +``` + +No need for all that other code.