daily pastebin goal
2%
SHARE
TWEET

Untitled

a guest Feb 20th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. trait WorkspaceLog {
  2.     fn get(&self) -> usize;
  3. }
  4.  
  5. struct TheLog<'a>(&'a FilesystemOverlay);
  6.  
  7. impl<'a> WorkspaceLog for TheLog<'a> {
  8.     fn get(&self) -> usize {
  9.         (self.0).0
  10.     }
  11. }
  12.  
  13. trait WorkspaceController<'a> {
  14.     type Log: WorkspaceLog;
  15.     fn get_log(&'a self) -> Self::Log;
  16. }
  17.  
  18. struct FilesystemOverlay(usize);
  19.  
  20. struct FSWorkspaceController<'a>(&'a mut FilesystemOverlay);
  21.  
  22. impl<'a> WorkspaceController<'a> for FSWorkspaceController<'a> {
  23.     type Log = TheLog<'a>;
  24.  
  25.     fn get_log(&'a self) -> Self::Log {
  26.         TheLog(&*self.0)
  27.     }
  28. }
  29.  
  30. trait AsWorkspaceController<'a> {
  31.     type Controller: WorkspaceController<'a>;
  32.  
  33.     fn get_controller(self) -> Self::Controller;
  34. }
  35.  
  36. impl<'a> AsWorkspaceController<'a> for &'a mut FilesystemOverlay {
  37.     type Controller = FSWorkspaceController<'a>;
  38.  
  39.     fn get_controller(self) -> FSWorkspaceController<'a> {
  40.         FSWorkspaceController(self)
  41.     }
  42. }
  43.  
  44. fn init1(control_dir: &mut FilesystemOverlay) -> usize {
  45.     let controller = control_dir.get_controller();
  46.     let log = controller.get_log();
  47.     log.get()
  48. }
  49.  
  50. fn init2<'a, O>(control_dir: O) -> usize
  51.     where O: AsWorkspaceController<'a> {
  52.     let controller = control_dir.get_controller();
  53.     let log = controller.get_log();
  54.     log.get()
  55. }
  56.  
  57. fn main() {
  58.     let mut control_dir = FilesystemOverlay(5);
  59.     dbg!(init1(&mut control_dir));
  60.     dbg!(init2(&mut control_dir));
  61. }
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
 
Top