Skip to content

Commit b4a05d2

Browse files
committed
Ch. 18: use “dyn compatibility” instead of “object safety”
- Introduce the concept at the end of 18.2 when discussing the tradeoffs of dynamic dispatch, with a link to the reference. The link is not currently valid, because of the versioning timeline, but it will be when this stabilizes! - In 18.3, simply replace “object safe” with “dyn compatible”. There is still probably not enough discussion of it, but with the introduction and link in 18.2, it is *probably* enough for now?
1 parent d83137d commit b4a05d2

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

src/ch18-02-trait-objects.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,12 @@ so it doesn’t know which method implemented on which type to call. Instead, at
239239
runtime, Rust uses the pointers inside the trait object to know which method to
240240
call. This lookup incurs a runtime cost that doesn’t occur with static
241241
dispatch. Dynamic dispatch also prevents the compiler from choosing to inline a
242-
method’s code, which in turn prevents some optimizations. However, we did get
243-
extra flexibility in the code that we wrote in Listing 18-5 and were able to
244-
support in Listing 18-9, so it’s a trade-off to consider.
242+
method’s code, which in turn prevents some optimizations, and Rust has some
243+
rules about where you can and cannot use dynamic dispatch, called [_dyn
244+
compatibility_][dyn-compat]. However, we did get extra flexibility in the code
245+
that we wrote in Listing 18-5 and were able to support in Listing 18-9, so it’s
246+
a trade-off to consider.
245247

246248
[performance-of-code-using-generics]: ch10-01-syntax.html#performance-of-code-using-generics
247249
[dynamically-sized]: ch20-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait
250+
[dyn-compat]: https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility

src/ch18-03-oo-design-patterns.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,9 @@ another design pattern.
349349
Another downside is that we’ve duplicated some logic. To eliminate some of the
350350
duplication, we might try to make default implementations for the
351351
`request_review` and `approve` methods on the `State` trait that return `self`;
352-
however, this would violate object safety, because the trait doesn’t know what
352+
however, this would not be dyn compatible, because the trait doesn’t know what
353353
the concrete `self` will be exactly. We want to be able to use `State` as a
354-
trait object, so we need its methods to be object safe.
354+
trait object, so we need its methods to be dyn compatible.
355355

356356
Other duplication includes the similar implementations of the `request_review`
357357
and `approve` methods on `Post`. Both methods delegate to the implementation of

0 commit comments

Comments
 (0)