]>
Commit | Line | Data |
---|---|---|
74b04a01 XL |
1 | pub struct Post { |
2 | state: Option<Box<dyn State>>, | |
3 | content: String, | |
4 | } | |
5 | ||
6 | // ANCHOR: here | |
7 | impl 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 | ||
33 | trait State { | |
34 | fn request_review(self: Box<Self>) -> Box<dyn State>; | |
35 | } | |
36 | ||
37 | struct Draft {} | |
38 | ||
39 | impl State for Draft { | |
40 | fn request_review(self: Box<Self>) -> Box<dyn State> { | |
41 | Box::new(PendingReview {}) | |
42 | } | |
43 | } | |
44 | ||
45 | struct PendingReview {} | |
46 | ||
47 | impl State for PendingReview { | |
48 | fn request_review(self: Box<Self>) -> Box<dyn State> { | |
49 | self | |
50 | } | |
51 | } | |
52 | // ANCHOR_END: here |