Skip to content

Commit df80779

Browse files
author
QuineDot
committed
Some brief additions to hopefully help drive home that dyn Trait is a concrete type
1 parent 47d7525 commit df80779

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/dyn-trait-impls.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,19 @@ something like the `non_dyn_dispatchable` override attempted above.
277277
See [issue 51402](https://github.com/rust-lang/rust/issues/51402) for more
278278
information.
279279

280+
Implementing methods on `dyn Trait` that don't attempt to shadow the
281+
methods of `Trait` does work, however.
282+
```rust
283+
#trait Trait {}
284+
impl dyn Trait + '_ {
285+
fn some_other_method(&self) {}
286+
}
287+
288+
fn bar(d: &dyn Trait) {
289+
d.some_other_method();
290+
}
291+
```
292+
280293
## A niche exception to `dyn Trait: Trait`
281294

282295
Some bounds on traits aren't checked until you try to utilize the trait,

src/dyn-trait-overview.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,13 @@ fn bar<T: Trait + ?Sized>(t: &T) {
8989
}
9090
```
9191

92-
9392
### `dyn Trait` is neither a generic nor dynamically typed
9493

9594
Given a concrete lifetime `'a`, `dyn Trait + 'a` is a statically known type.
95+
The *erased base type* is not statically known, but don't let this confuse
96+
you: the `dyn Trait` itself is its own distinct type and that type is known
97+
at compile time.
98+
9699
For example, consider these two function signatures:
97100
```rust
98101
# trait Trait {}
@@ -147,6 +150,11 @@ but deref coercion usually takes care of that case. For many `std` traits,
147150
the trait is explicitly implemented for `Box<dyn Trait>` as well;
148151
[we'll also explore what that can look like.](./dyn-trait-box-impl.md))
149152

153+
As a concrete type, you can also implement methods on `dyn Trait`
154+
(provided `Trait` is local to your crate), and even implement *other*
155+
traits for `dyn Trait`
156+
(as we will see in [some of the examples](./dyn-trait-examples.md)).
157+
150158
### `dyn Trait` is not a supertype
151159

152160
Because you can coerce base types into a `dyn Trait`, it is not uncommon for

0 commit comments

Comments
 (0)