#"Encoding States and Behavior as Types"

7 messages · Page 1 of 1 (latest)

tame fable
#

In chapter 17 of the book there is an exercise to turn the verbose OOP program into one without encapsulation and with type checking at compile time

https://doc.rust-lang.org/nightly/book/ch17-03-oo-design-patterns.html

This is what I've done, but I get a warning. I can get rid of the warning by making request_review private, but I don't understand why the warning appears only in that function, when other functions also expose a private field.

warning: private type PendingReviewPost in public interface (error E0446)

    pub struct Post {
        content: String,
    }
    pub struct DraftPost {
        content: String,
    }
    struct PendingReviewPost {
        content: String,
    }

    impl Post {
        pub fn new() -> DraftPost {
            DraftPost {
                content: String::new(),
            }
        }
        pub fn content(&self) -> &str {
            &self.content
        }
    }

    impl DraftPost {
        pub fn add_text(&mut self, text: &str) {
            self.content.push_str(text);
        }
        pub fn request_review(self) -> PendingReviewPost {
            PendingReviewPost {
                content: self.content,
            }
        }
    }

    impl PendingReviewPost {
        pub fn approve(self) -> Post {
            Post {
                content: self.content,
            }
        }
    }

    let mut post = Post::new();
    post.add_text("I ate a salad for lunch today");
    let post = post.request_review();
    let post = post.approve();
    assert_eq!("I ate a salad for lunch today", post.content());

Edit: fixed code formatting

dense vector
#

By exposing a private type you make it very difficult for a user of the library/module to use the return value

#

Because they can't name the type

#

In this case it should probably be public

#

?play warn=true

    pub struct Post {
        content: String,
    }
    pub struct DraftPost {
        content: String,
    }
    pub struct PendingReviewPost {
        content: String,
    }

    impl Post {
        pub fn new() -> DraftPost {
            DraftPost {
                content: String::new(),
            }
        }
        pub fn content(&self) -> &str {
            &self.content
        }
    }

    impl DraftPost {
        pub fn add_text(&mut self, text: &str) {
            self.content.push_str(text);
        }
        pub fn request_review(self) -> PendingReviewPost {
            PendingReviewPost {
                content: self.content,
            }
        }
    }

    impl PendingReviewPost {
        pub fn approve(self) -> Post {
            Post {
                content: self.content,
            }
        }
    }

    let mut post = Post::new();
    post.add_text("I ate a salad for lunch today");
    let post = post.request_review();
    let post = post.approve();
    assert_eq!("I ate a salad for lunch today", post.content());
gusty sparrowBOT
tame fable
#

Oh it's talking about the whole struct, I thought it was referring to the content field. Totally missed that haha thanks