1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//! Traits Deep Dive, Part 3
//!
//!   - **Date:** July 4, 2018
//!   - **Subject:** Closure traits, `impl trait`, `dyn trait`, and object safety!
//!   - [**Script**][script]
//!   - [**Audio**][mp3]
//!
//! [script]: https://newrustacean.com/show_notes/e025/struct.script
//! [mp3]: https://www.podtrac.com/pts/redirect.mp3/cdn.newrustacean.com/file/newrustacean/e025.mp3
//!
//! <audio style="width: 100%" title="" controls preload=metadata><source src="https://www.podtrac.com/pts/redirect.mp3/cdn.newrustacean.com/file/newrustacean/e025.mp3"></audio>
//!
//!
//! Show Notes
//! ----------
//!
//! Sponsored by [Parity Technologies][parity]! Parity is hiring Rust developers
//! so if you're interested, you should check out their [job listings][jobs]!
//!
//! [parity]: https://paritytech.io
//! [jobs]: https://paritytech.io/jobs/
//!
//! ### Links
//!
//! - [RFC #1733: Trait Aliases][1733]
//! - [RFC #255: Object Safety][255]
//! - [Ch. 17 in the Second Edition of *The Rust Programming Language*][trpl]
//! - [Huon Wilson's post][huon]
//!
//! [1733]: https://github.com/rust-lang/rfcs/blob/master/text/1733-trait-alias.md
//! [255]: https://github.com/rust-lang/rfcs/blob/master/text/0255-object-safety.md
//! [trpl]: https://doc.rust-lang.org/book/second-edition/ch17-02-trait-objects.html
//! [huon]: https://huonw.github.io/blog/2015/05/where-self-meets-sized-revisiting-object-safety/ "Where Self Meets Sized: Revisiting Object Safety"
//!
//! ### Example
//!
//! You can see all of the pieces of the final example described in the show
//! here (and the module has the required definitions for `Point`).
//!
//! ```rust
//! # use show_notes::e025::*;
//! # fn main() {
//! let points = vec![
//!     Point { x: 1.0, y: 2.0 },
//!     Point { x: 12.0, y: 4.3 },
//!     Point { x: -5.4, y: 18.7 },
//! ];
//!
//! let origin = Point::default();
//!
//! // This is the version we start with. It works fine, but it's not elegant.
//! let distances_inline: Vec<f32> = points
//!     .iter()
//!     .map(|point| {
//!         let change = point - &origin;
//!         (change.x.powi(2) + change.y.powi(2)).sqrt()
//!     })
//!     .collect();
//!
//! // This version is *much* cleaner!
//! let distances_impl: Vec<f32> = points.iter().map(distance_from_impl(&origin)).collect();
//! # }
//! ```
//!
//! Sponsors
//! --------
//!
//!   - Aaron Turon
//!   - Alexander Kryvomaz
//!   - Alexander Payne
//!   - [Anthony Deschamps]
//!   - Anthony Scotti
//!   - Antonin Carette
//!   - Aleksey Pirogov
//!   - Andreas Fischer
//!   - Andrew Thompson
//!   - Austin LeSure
//!   - [Behnam Esfahbod]
//!   - Benjamin Wasty
//!   - Brent Vatne
//!   - Brian Casiello
//!   - Chap Lovejoy
//!   - [Charlie Egan]
//!   - Chris Jones
//!   - [Chris Palmer]
//!   - [Coleman McFarland]
//!   - Damien Stanton
//!   - Dan Abrams
//!   - [Daniel Collin]
//!   - Daniel Mason
//!   - [Daniel P. Clark]
//!   - [David W. Allen]
//!   - David Hewson
//!   - Derek Buckley
//!   - [Derek Morr]
//!   - Eugene Bulkin
//!   - [Hans Fjällemark]
//!   - [Henri Sivonen]
//!   - [Ian Jones]
//!   - [Jakub "Limeth" Hlusička]
//!   - James Cooper
//!   - Jerome Froelich
//!   - [John Rudnick]
//!   - Jon
//!   - Jonathan Turner
//!   - Joseph Hain
//!   - [Jupp Müller]
//!   - Justin Ossevoort
//!   - [Karl Hobley]
//!   - Keith Gray
//!   - Kilian Rault
//!   - Laurie Hedge
//!   - Luca Schmid
//!   - [Luiz Irber]
//!   - Mark LeMoine
//!   - Martin Heuschober
//!   - Masashi Fujita
//!   - Matt Rudder
//!   - Matthew Brenner
//!   - Matthias Ruszala
//!   - [Max Jacobson]
//!   - [Messense Lv]
//!   - Micael Bergeron
//!   - [Nathan Sculli]
//!   - [Nick Coish]
//!   - [Nick Stevens]
//!   - [Oluseyi Sonaiya]
//!   - Ovidiu Curcan
//!   - [Pascal Hertleif]
//!   - [Patrick O'Doherty]
//!   - [Paul Naranja]
//!   - Peter Tillemans
//!   - Ralph Giles ("rillian")
//!   - Raj Venkalil
//!   - [Ramon Buckland]
//!   - Randy MacLeod
//!   - Raph Levien
//!   - reddraggone9
//!   - [Robert Chrzanowski]
//!   - [Ryan Blecher]
//!   - Ryan Osial
//!   - [Sebastián Ramírez Magrí]
//!   - Shane Utt
//!   - Simon G.
//!   - Steve Jenson
//!   - Steven Knight
//!   - Steven Murawski
//!   - [Stuart Hinson]
//!   - Tim Brooks
//!   - Tom Prince
//!   - Ty Overby
//!   - Tyler Harper
//!   - Vesa Kaihlavirta
//!   - Victor Kruger
//!   - Will Greenberg
//!   - [William Roe]
//!   - Yaacov Finkelman
//!   - Zachary Snyder
//!   - Zaki
//!
//! [Anthony Deschamps]: https://github.com/adeschamps
//! [Behnam Esfahbod]: https://github.com/behnam
//! [Brent Vatne]: https://github.com/brentvatne
//! [Charlie Egan]: https://charlieegan3.com
//! [Chris Palmer]: http://red-oxide.org/
//! [Coleman McFarland]: http://github.com/anxiousmodernman
//! [Daniel Collin]: https://twitter.com/daniel_collin
//! [Daniel P. Clark]: https://6ftdan.com/
//! [David W. Allen]: http://GitHub.com/DataRiot
//! [Derek Morr]: https://twitter.com/derekmorr
//! [Fjällemark]: https://fjallemark.com/
//! [Henri Sivonen]: https://hsivonen.fi/
//! [Ian Jones]: https://www.ianmjones.com/
//! [Jakub "Limeth" Hlusička]: https://github.com/Limeth
//! [John Rudnick]: http://www.cindur.com/
//! [Jupp Müller]: https://de.linkedin.com/in/juppm
//! [Karl Hobley]: https://github.com/kaedroho/
//! [Luiz Irber]: http://luizirber.org/
//! [Max Jacobson]: https://twitter.com/maxjacobson
//! [Messense Lv]: https://github.com/messense
//! [Nathan Sculli]: http://influential.co/
//! [Nick Coish]: http://github.com/ncoish
//! [Nick Stevens]: https://github.com/nastevens
//! [Oluseyi Sonaiya]: http://oluseyi.info/
//! [Pascal Hertleif]: https://pascalhertleif.de/
//! [Patrick O'Doherty]: https://twitter.com/patrickod
//! [Philipp Keller]: https://twitter.com/hansapla
//! [Ramon Buckland]: http://www.inosion.com
//! [Robert Chrzanowski]: https://github.com/zirman
//! [Ryan Blecher]: http://notryanb.github.io/
//! [Sebastián Ramírez Magrí]: https://www.twitter.com/sebasmagri
//! [Stuart Hinson]: http://stuarth.github.io/
//! [William Roe]: http://willroe.me
//!
//! (Thanks to the couple people donating who opted out of the reward tier, as
//! well. You know who you are!)
//!
//! ### Become a sponsor
//!
//!   - <a href="https://www.patreon.com/newrustacean" rel="payment">Patreon</a>
//!   - [Venmo](https://venmo.com/chriskrycho)
//!   - [Dwolla](https://www.dwolla.com/hub/chriskrycho)
//!   - [Cash.me](https://cash.me/$chriskrycho)
//!   - [Flattr](https://flattr.com/profile/chriskrycho)
//!   - [PayPal.me](https://paypal.me/chriskrycho)
//!
//!
//! Contact
//! -------
//!
//!   - New Rustacean:
//!     + Twitter: [@newrustacean](https://www.twitter.com/newrustacean)
//!     + Email: [hello@newrustacean.com](mailto:hello@newrustacean.com)
//!   - Chris Krycho
//!     + GitHub: [chriskrycho](https://github.com/chriskrycho)
//!     + Twitter: [@chriskrycho](https://www.twitter.com/chriskrycho)

use std::{f32, ops::Sub};

#[derive(Default)]
pub struct Point {
    pub x: f32,
    pub y: f32,
}

impl<'p> Sub for &'p Point {
    type Output = Point;

    fn sub(self, other: &Point) -> Point {
        Point {
            x: self.x - other.x,
            y: self.y - other.y,
        }
    }
}

pub fn distance_from_boxed<'a, 'b: 'a>(offset: &'b Point) -> Box<dyn FnMut(&'a Point) -> f32 + 'a> {
    Box::new(move |point| {
        let change = point - offset;
        (change.x.powi(2) + change.y.powi(2)).sqrt()
    })
}

pub type DistanceFrom<'a> = Box<dyn FnMut(&'a Point) -> f32 + 'a>;

pub fn distance_from_alias<'a, 'b: 'a>(offset: &'b Point) -> DistanceFrom<'a> {
    Box::new(move |point| {
        let change = point - offset;
        (change.x.powi(2) + change.y.powi(2)).sqrt()
    })
}

pub fn distance_from_impl<'a, 'b: 'a>(offset: &'b Point) -> impl FnMut(&'a Point) -> f32 {
    move |point| {
        let change = point - offset;
        (change.x.powi(2) + change.y.powi(2)).sqrt()
    }
}

#[doc = include_str!("../docs/e025-script.md")]
pub struct Script;