SHARE
TWEET

Untitled

a guest Apr 19th, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::borrow::Cow;
  2.  
  3. pub struct Controller<'a, I: Internals> {
  4.     state: ControllerState<'a>,
  5.     internals: I,
  6. }
  7.  
  8. impl<'a, I: Internals> Controller<'a, I> {
  9.     pub fn internals(&self) -> &I {
  10.         &self.internals
  11.     }
  12.  
  13.     pub fn do_something(&mut self) -> String {
  14.         self.internals.update_things(&mut UpdateThingsEvent {
  15.             state: &mut self.state,
  16.         });
  17.  
  18.         self.state.things.join(", ")
  19.     }
  20. }
  21.  
  22. pub struct ControllerState<'a> {
  23.     things: Vec<Cow<'a, str>>,
  24. }
  25.  
  26. impl<'a> ControllerState<'a> {
  27.     pub fn things_mut(&mut self) -> &mut Vec<Cow<'a, str>> {
  28.         &mut self.things
  29.     }
  30. }
  31.  
  32. pub trait Internals {
  33.     fn update_things(&mut self, state: &mut UpdateThingsEvent) {
  34.         state.state.things_mut().push("thing".into());
  35.     }
  36. }
  37.  
  38. pub struct UpdateThingsEvent<'a> {
  39.     state: &'a mut ControllerState<'a>,
  40. }
  41.  
  42. impl<'a> UpdateThingsEvent<'a> {
  43.     pub fn state(&mut self) -> &'a mut ControllerState {
  44.         self.state
  45.     }
  46. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top