]> git.proxmox.com Git - rustc.git/blame - src/doc/book/listings/ch17-oop/listing-17-15/src/lib.rs
New upstream version 1.50.0+dfsg1
[rustc.git] / src / doc / book / listings / ch17-oop / listing-17-15 / src / lib.rs
CommitLineData
74b04a01
XL
1pub struct Post {
2 state: Option<Box<dyn State>>,
3 content: String,
4}
5
6// ANCHOR: here
7impl Post {
8 // --snip--
9 // ANCHOR_END: here
10 pub fn new() -> Post {
11 Post {
12 state: Some(Box::new(Draft {})),
13 content: String::new(),
14 }
15 }
16
17 pub fn add_text(&mut self, text: &str) {
18 self.content.push_str(text);
19 }
20
21 pub fn content(&self) -> &str {
22 ""
23 }
24
25 // ANCHOR: here
26 pub fn request_review(&mut self) {
27 if let Some(s) = self.state.take() {
28 self.state = Some(s.request_review())
29 }
30 }
31}
32
33trait State {
34 fn request_review(self: Box<Self>) -> Box<dyn State>;
35}
36
37struct Draft {}
38
39impl State for Draft {
40 fn request_review(self: Box<Self>) -> Box<dyn State> {
41 Box::new(PendingReview {})
42 }
43}
44
45struct PendingReview {}
46
47impl State for PendingReview {
48 fn request_review(self: Box<Self>) -> Box<dyn State> {
49 self
50 }
51}
52// ANCHOR_END: here