Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::ops::{Deref, DerefMut};
- struct System1 {
- }
- impl System1 {
- fn mut_it(&mut self) { }
- }
- struct System2 {
- }
- impl System2 {
- fn mut_it(&mut self) { }
- }
- struct Context<'a> {
- s1: Option<&'a mut System1>,
- s2: Option<&'a mut System2>
- }
- struct ContextRef<'a> {
- u: Context<'a>,
- }
- impl<'a> Deref for ContextRef<'a> {
- type Target = Context<'a>;
- fn deref(&self) -> &Self::Target { &self.u }
- }
- impl<'a> DerefMut for ContextRef<'a> {
- fn deref_mut(&mut self) -> &mut Self::Target { &mut self.u }
- }
- impl<'a> Context<'a> {
- 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()) } } }) }
- 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 } }) }
- }
- fn action<'a: 'b, 'b>(c: &'b mut Context<'a>) {
- let (a, mut d) = c.s1();
- let (b, _) = d.s2();
- a.mut_it();
- b.mut_it();
- }
- fn main() {
- action(&mut Context { s1: Some(&mut System1 { }), s2: Some(&mut System2 { }) })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement