&post.content
}
}
+
+// state encoded as types; this is the "approved" state
+pub struct TPost {
+ content: String,
+}
+
+impl TPost {
+ pub fn new() -> TPostDraft {
+ TPostDraft {content: String::new()}
+ }
+
+ pub fn content(&self) -> &str {
+ &self.content
+ }
+}
+
+pub struct TPostDraft {
+ content: String,
+}
+
+impl TPostDraft {
+ pub fn add_text(&mut self, text: &str) {
+ self.content.push_str(text);
+ }
+
+ pub fn request_review(self) -> TPostReview {
+ TPostReview {content: self.content}
+ }
+}
+
+pub struct TPostReview {
+ content: String,
+}
+
+impl TPostReview {
+ pub fn approve(self) -> TPost {
+ TPost {content: self.content}
+ }
+
+ pub fn reject(self) -> TPostDraft {
+ TPostDraft {content: self.content}
+ }
+}
assert_eq!(text, post.content());
}
+fn test_state_types() {
+ let mut post = TPost::new();
+ post.add_text("I ate a salad for lunch");
+ let post = post.request_review();
+ let mut post = post.reject();
+ post.add_text(" today");
+ let post = post.request_review();
+ let post = post.approve();
+ assert_eq!(post.content(), "I ate a salad for lunch today");
+}
+
fn main() {
test_strings();
test_vectors();
test_iterators();
test_threads();
test_dyn_traits();
+ test_state_types();
}