Skip to content

Commit 6156ff1

Browse files
authored
Merge pull request #4281 from rust-lang/state-option-etc
Ch. 18: correct discussion of delegation in `Post` methods
2 parents b660608 + 8904592 commit 6156ff1

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,12 @@ known at compile time. (This is one of the dyn compatibility rules mentioned
359359
earlier.)
360360

361361
Other duplication includes the similar implementations of the `request_review`
362-
and `approve` methods on `Post`. Both methods delegate to the implementation of
363-
the same method on the value in the `state` field of `Option` and set the new
364-
value of the `state` field to the result. If we had a lot of methods on `Post`
365-
that followed this pattern, we might consider defining a macro to eliminate the
366-
repetition (see [“Macros”][macros]<!-- ignore --> in Chapter 20).
362+
and `approve` methods on `Post`. Both methods use `Option::take` with the
363+
`state` field of `Post`, and if `state` is `Some`, they delegate to the wrapped
364+
value’s implementation of the same method and set the new value of the `state`
365+
field to the result. If we had a lot of methods on `Post` that followed this
366+
pattern, we might consider defining a macro to eliminate the repetition (see
367+
[“Macros”][macros]<!-- ignore --> in Chapter 20).
367368

368369
By implementing the state pattern exactly as it’s defined for object-oriented
369370
languages, we’re not taking as full advantage of Rust’s strengths as we could.

0 commit comments

Comments
 (0)