Skip to content

Generate transparent `Struct(Rc<RefCell<Struct>>)` newtypes

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

felixrabe/rcstruct

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rcstruct

Example without rcstruct

See examples/01a.rs for the full example.

struct GUIInternal {
    running: bool,
    event_recv: Receiver<Event>,
    action_send: Sender<Action>,
}

impl GUIInternal {
    fn send_action(&self, action: Action) -> Rt {
        Ok(self.action_send.send(action)?)
    }

    fn running(&self) -> Rt<bool> {
        Ok(self.running)
    }

    fn quit(&mut self) -> Rt {
        self.running = false;
        Ok(())
    }

    fn events(&self) -> Rt<impl IntoIterator<Item = Event>> {
        let events = Vec::new();
        Ok(events)
    }
}

pub struct GUI(Rc<RefCell<GUIInternal>>);

impl GUI {
    pub fn new(event_recv: Receiver<Event>, action_send: Sender<Action>) -> Rt<Self> {
        let running = true;
        Ok(GUI(Rc::new(RefCell::new(GUIInternal { running, event_recv, action_send, }))))
    }

    pub fn send_action(&self, action: Action) -> Rt {
        self.0.borrow().send_action(action)
    }

    pub fn running(&self) -> Rt<bool> {
        self.0.borrow().running()
    }

    pub fn quit(&self) -> Rt {
        self.0.borrow_mut().quit()
    }

    pub fn events(&self) -> Rt<impl IntoIterator<Item = Event>> {
        self.0.borrow().events()
    }
}

Example with rcstruct

See examples/01b.rs for the full example.

rcstruct::rcstruct! {
    pub struct GUI {
        running: bool,
        event_recv: Receiver<Event>,
        action_send: Sender<Action>,
    }

    impl {
        pub new(event_recv: Receiver<Event>, action_send: Sender<Action>) -> Rt<Self> {
            let running = true;
            { running, event_recv, action_send, }
        }

        pub fn send_action(&self, action: Action) -> Rt {
            Ok(self.action_send.send(action)?)
        }

        pub fn running(&self) -> Rt<bool> {
            Ok(self.running)
        }

        pub fn quit(&mut self) -> Rt {
            self.running = false;
            Ok(())
        }

        pub fn events(&self) -> Rt<impl IntoIterator<Item = Event>> {
            let events = Vec::new();
            Ok(events)
        }
    }
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

About

Generate transparent `Struct(Rc<RefCell<Struct>>)` newtypes

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published

Languages