Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. use std::ops::{Deref, DerefMut};
  2.  
  3. struct System1 {
  4.  
  5. }
  6.  
  7. impl System1 {
  8. fn mut_it(&mut self) { }
  9. }
  10.  
  11. struct System2 {
  12.  
  13. }
  14.  
  15. impl System2 {
  16. fn mut_it(&mut self) { }
  17. }
  18.  
  19. struct Context<'a> {
  20. s1: Option<&'a mut System1>,
  21. s2: Option<&'a mut System2>
  22. }
  23.  
  24. struct ContextRef<'a> {
  25. u: Context<'a>,
  26. }
  27.  
  28. impl<'a> Deref for ContextRef<'a> {
  29. type Target = Context<'a>;
  30.  
  31. fn deref(&self) -> &Self::Target { &self.u }
  32. }
  33.  
  34. impl<'a> DerefMut for ContextRef<'a> {
  35. fn deref_mut(&mut self) -> &mut Self::Target { &mut self.u }
  36. }
  37.  
  38. impl<'a> Context<'a> {
  39. fn s1<'b>(&'b mut self) -> (&'b mut System1, impl DerefMut<Target=Context> + 'b) { (self.s1.as_mut().unwrap(), ContextRef { u: Context::<'b> { s1: None, s2: if self.s2.is_none() { None } else { Some(self.s2.as_mut().unwrap()) } } }) }
  40. fn s2<'b>(&'b mut self) -> (&'b mut System2, ContextRef<'b>) { (self.s2.as_mut().unwrap(), ContextRef { u: Context::<'b> { s1: if self.s1.is_none() { None } else { Some(self.s1.as_mut().unwrap()) }, s2: None } }) }
  41. }
  42.  
  43. fn action<'a: 'b, 'b>(c: &'b mut Context<'a>) {
  44. let (a, mut d) = c.s1();
  45. let (b, _) = d.s2();
  46. a.mut_it();
  47. b.mut_it();
  48. }
  49.  
  50. fn main() {
  51. action(&mut Context { s1: Some(&mut System1 { }), s2: Some(&mut System2 { }) })
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement