Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trait WithContext {
- fn with_context(&mut self, f: &mut FnMut(&mut Context<'_>));
- fn with_context_once<'b>(&mut self, f: Box<dyn 'b + FnOnce(&mut Context<'_>)>);
- }
- impl<'a> WithContext for Context<'a> {
- fn with_context(&mut self, f: &mut FnMut(&mut Context<'_>)) {
- f(self)
- }
- fn with_context_once<'b>(&mut self, f: Box<dyn 'b + FnOnce(&mut Context<'_>)>) {
- f(self)
- }
- }
- pub struct Context<'a> {
- parent: Option<&'a mut dyn WithContext>
- }
- impl<'a> Context<'a> {
- fn child(&mut self) -> Context<'_> {
- Context {
- parent: Some(self),
- }
- }
- pub fn with_parent<F: FnMut(&mut Context<'_>)>(&mut self, mut f: F) {
- if let Some(parent) = self.parent.as_mut() {
- parent.with_context(&mut f)
- }
- }
- pub fn with_parent_once<F: FnOnce(&mut Context<'_>)>(&mut self, f: F) {
- if let Some(parent) = self.parent.as_mut() {
- parent.with_context_once(Box::new(f))
- }
- }
- }
- fn this_doesnt_work(context: &mut Context) {
- let mut child = context.child();
- let mut x : u32 = 0;
- child.with_parent(|parent| {
- x = 0;
- });
- let x = x;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement