From 07310e5fcdc862efd1acde98f84293e4e7c20933 Mon Sep 17 00:00:00 2001 From: "Carol (Nichols || Goulding)" Date: Thu, 2 Mar 2017 18:12:30 -0500 Subject: [PATCH] Add more figures --- dot/trpl15-03.dot | 50 +++++++++++++++++++++++ src/ch15-02-deref.md | 4 +- src/ch15-03-drop.md | 10 ++--- src/ch15-04-rc.md | 31 ++++++++------ src/img/trpl15-03.svg | 94 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 169 insertions(+), 20 deletions(-) create mode 100644 dot/trpl15-03.dot create mode 100644 src/img/trpl15-03.svg diff --git a/dot/trpl15-03.dot b/dot/trpl15-03.dot new file mode 100644 index 000000000..5626de248 --- /dev/null +++ b/dot/trpl15-03.dot @@ -0,0 +1,50 @@ +digraph { + rankdir=LR; + dpi=300.0; + node [shape="plaintext"]; + + table4[label=< + +
b
>]; + + table5[label=< + +
3
>]; + + + table0[label=< + +
a
>]; + + table1[label=< + +
5
>]; + + table2[label=< + +
10
>]; + + table3[label=< + +
Nil
>]; + + + table6[label=< + +
c
>]; + + table7[label=< + +
4
>]; + + + edge[tailclip="false"]; + table0:ptr0:c -> table1:pte0; + table1:ptr1:c -> table2:pte1; + table2:ptr2:c -> table3:pte2; + table4:ptr4:c -> table5:pte4; + table5:ptr5:c -> table1:pte0; + table6:ptr6:c -> table7:pte6; + table7:ptr7:c -> table1:pte0; +} + diff --git a/src/ch15-02-deref.md b/src/ch15-02-deref.md index 11a65157c..47b35ed48 100644 --- a/src/ch15-02-deref.md +++ b/src/ch15-02-deref.md @@ -36,7 +36,7 @@ to be able to use smart pointers in the same places that we can use regular references. To enable that, we can override the behavior of the `*` operator by implementing the `Deref` trait. -Listing 15-5 has an example of overriding `*` using `Deref` on a struct we've +Listing 15-7 has an example of overriding `*` using `Deref` on a struct we've defined to hold mp3 data and metadata. `Mp3` is, in a sense, a smart pointer: it owns the `Vec` data containing the audio. In addition, it holds some optional metadata, in this case the artist and title of the song in the audio @@ -79,7 +79,7 @@ fn main() {
-Listing 15-5: An implementation of the `Deref` trait on a struct that holds mp3 +Listing 15-7: An implementation of the `Deref` trait on a struct that holds mp3 file data and metadata
diff --git a/src/ch15-03-drop.md b/src/ch15-03-drop.md index 220e6a24c..1fd16eddb 100644 --- a/src/ch15-03-drop.md +++ b/src/ch15-03-drop.md @@ -21,7 +21,7 @@ The way we specify code should be run when a value goes out of scope is by implementing the `Drop` trait. The `Drop` trait requires us to implement one method named `drop` that takes a mutable reference to `self`. -Listing 15-6 shows a `CustomSmartPointer` struct that doesn't actually do +Listing 15-8 shows a `CustomSmartPointer` struct that doesn't actually do anything, but we're printing out `CustomSmartPointer created.` right after we create an instance of the struct and `Dropping CustomSmartPointer!` when the instance goes out of scope so that we can see when each piece of code gets run. @@ -51,7 +51,7 @@ fn main() {
-Listing 15-6: A `CustomSmartPointer` struct that implements the `Drop` trait, +Listing 15-8: A `CustomSmartPointer` struct that implements the `Drop` trait, where we could put code that would clean up after the `CustomSmartPointer`.
@@ -82,7 +82,7 @@ trait is that it's taken care of automatically for us. We'll see an example of a case when we'll need to drop a value earlier than when it goes out of scope in Chapter 16 when we're talking about concurrency. For now, let's just see that it's possible, and `std::mem::drop` is in the prelude so we can just call -`drop` as shown in Listing 15-7: +`drop` as shown in Listing 15-9:
@@ -97,7 +97,7 @@ fn main() {
-Listing 15-7: Calling `std::mem::drop` to explicitly drop a value before it +Listing 15-9: Calling `std::mem::drop` to explicitly drop a value before it goes out of scope
@@ -114,7 +114,7 @@ Wait for it... ``` Note that we aren't allowed to call the `drop` method that we defined directly: -if we replaced `drop(c)` in Listing 15-7 with `c.drop()`, we'll get a compiler +if we replaced `drop(c)` in Listing 15-9 with `c.drop()`, we'll get a compiler error that says `explicit destructor calls not allowed`. We're not allowed to call `Drop::drop` directly because when Rust inserts its call to `Drop::drop` automatically when the value goes out of scope, then the value would get diff --git a/src/ch15-04-rc.md b/src/ch15-04-rc.md index 743a7276b..8e3fcf4e8 100644 --- a/src/ch15-04-rc.md +++ b/src/ch15-04-rc.md @@ -29,23 +29,28 @@ you'll get a compile-time error. ### Using `Rc` to Share Data -Let's return to our cons list example from Listing 15-4. In Listing 15-8, we're +Let's return to our cons list example from Listing 15-5. In Listing 15-11, we're going to try to use `List` as we defined it using `Box`. First we'll create one list instance that contains 5 and then 10. Next, we want to create two more lists: one that starts with 3 and continues on to our first list containing 5 and 10, then another list that starts with 4 and *also* continues on to our first list containing 5 and 10. In other words, we want two lists that both share ownership of the third list, which conceptually will be something like -this: +Figure 15-10: -```text -b -> 3 ---v -a ------> 5 -> 10 -> Nil -c -> 4 ---^ -``` +
+ +Two lists that share ownership of a third list + +
+ +Figure 15-10: Two lists, `b` and `c`, sharing ownership of a third list, `a` + +
+
Trying to implement this using our definition of `List` with `Box` won't -work: +work, as shown in Listing 15-11:
Filename: src/main.rs @@ -69,7 +74,7 @@ fn main() {
-Listing 15-8: Having two lists using `Box` that try to share ownership of a +Listing 15-11: Having two lists using `Box` that try to share ownership of a third list won't work
@@ -100,7 +105,7 @@ a list such that every element lives at least as long as the list itself. Otherwise, the borrow checker won't even let us compile the code. Instead, we can change our definition of `List` to use `Rc` instead of -`Box` as shown here in Listing 15-9: +`Box` as shown here in Listing 15-12:
Filename: src/main.rs @@ -123,7 +128,7 @@ fn main() {
-Listing 15-9: A definition of `List` that uses `Rc` +Listing 15-12: A definition of `List` that uses `Rc`
@@ -137,7 +142,7 @@ prelude. In `main`, we create the list holding 5 and 10 and store it in a new We've seen the `clone` method previously, where we used it for making a complete copy of some data. With `Rc`, though, it doesn't make a full copy. `Rc` holds a *reference count*, that is, a count of how many clones exist. -Let's change `main` as shown in Listing 15-10 to have an inner scope around +Let's change `main` as shown in Listing 15-13 to have an inner scope around where we create `c`, and to print out the results of the `Rc::strong_count` associated function at various points. `Rc::strong_count` returns the reference count of the `Rc` value we pass to it, and we'll talk about why this function @@ -171,7 +176,7 @@ fn main() {
-Listing 15-10: Printing out the reference count +Listing 15-13: Printing out the reference count
diff --git a/src/img/trpl15-03.svg b/src/img/trpl15-03.svg new file mode 100644 index 000000000..e26024f6b --- /dev/null +++ b/src/img/trpl15-03.svg @@ -0,0 +1,94 @@ + + + + + + +%3 + + +table4 +b + + +table5 + +3 + +   + + +table4:ptr4:c->table5:pte4 + + + + +table1 + +5 + +   + + +table5:ptr5:c->table1:pte0 + + + + +table0 +a + + +table0:ptr0:c->table1:pte0 + + + + +table2 + +10 + +   + + +table1:ptr1:c->table2:pte1 + + + + +table3 + +Nil + + +table2:ptr2:c->table3:pte2 + + + + +table6 +c + + +table7 + +4 + +   + + +table6:ptr6:c->table7:pte6 + + + + +table7:ptr7:c->table1:pte0 + + + + +