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

Arbitrary self types v2: book changes. #4174

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
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
7 changes: 7 additions & 0 deletions listings/ch15-smart-pointers/listing-15-XX/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions listings/ch15-smart-pointers/listing-15-XX/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "deref-method-example"
version = "0.1.0"
edition = "2021"

[dependencies]
33 changes: 33 additions & 0 deletions listings/ch15-smart-pointers/listing-15-XX/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#![feature(arbitrary_self_types)]
// TODO remove before we land this

use std::ops::Deref;

struct MyBox<T>(T);

impl<T> MyBox<T> {
fn new(x: T) -> MyBox<T> {
MyBox(x)
}
}

impl<T> Deref for MyBox <T> {
type Target = T;

fn deref(&self) -> &Self::Target {
&self.0
}
}

struct Point(i32, i32, i32);

impl Point {
fn describe(self: &MyBox<Self>) {
println!("x: {} | y: {} | z : {}", self.0.0, self.0.1, self.0.2);
}
}

fn main() {
let point = MyBox::new(Point(1, -3, 1));
point.describe();
}
23 changes: 23 additions & 0 deletions src/ch15-02-deref.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,29 @@ match the parameter’s type. The number of times that `Deref::deref` needs to b
inserted is resolved at compile time, so there is no runtime penalty for taking
advantage of deref coercion!

### Calling methods on smart pointers

Because `MyBox` implements implements `Deref`, methods on a struct can also use
it as their `self` type, as shown in Listing XX.

<Listing number="15-XX" file-name="src/main.rs" caption="Calling `describe` on a reference to a `MyBox<Point>` value, which also works because of deref coercion">

```rust
{{#rustdoc_include ../listings/ch15-smart-pointers/listing-15-XX/src/main.rs:here}}
```

</Listing>

First, we introduce a `Point` type, representing x, y, and z coordinates. Then
we add a `describe` method. The `describe` method defines the type of `self` as
`&MyBox<Self>`. This enforces that this method can only be called on a reference
to `MyBox<Point>`. This is only allowed because `MyBox` implements `Deref` for
any type it wraps, including `Point`. If we tried to use a different wrapper
type that did *not* implement `Deref` for `Point`, the code would not compile.

This means that implementing `Deref` gives you many of the same abilities as the
smart pointer types that come in the standard library.

### How Deref Coercion Interacts with Mutability

Similar to how you use the `Deref` trait to override the `*` operator on
Expand Down
Loading