message_to_parent

Crates.iomessage_to_parent
lib.rsmessage_to_parent
version1.0.0
sourcesrc
created_at2023-12-28 17:31:50.670896
updated_at2023-12-28 17:31:50.670896
descriptionA simple library for child structs to talk to and interact with their parent without violating the borrow checker.
homepage
repository
max_upload_size
id1082554
size50,625
(jordan4ibanez)

documentation

README

message_to_parent

A simple library for child structs to talk to and interact with their parent without violating the borrow checker.

This is a hyperactive alternative to message passing and verbose returns.

Please see the /examples/ folder for a tutorial on how to use this!

A small example, taken from the basic tutorial:

extern crate message_to_parent;

use message_to_parent::MessageToParent;

struct Parent {
  thing_was_done: bool,
  child: Child,
}

impl Parent {
  pub fn new() -> Self {
    Parent {
      thing_was_done: false,
      child: Child::new(),
    }
  }

  pub fn main(&mut self) {
    println!("Parent: Thing was done? {}", self.thing_was_done);

    let mut child_message = self.child.do_thing();

    child_message.run_side_effects(self);

    println!("Parent: Thing was done? {}", self.thing_was_done);
  }
}

struct Child {}

impl Child {
  pub fn new() -> Self {
    Child {}
  }

  pub fn do_thing(&self) -> MessageToParent<Parent, ()> {
    let mut message = MessageToParent::<Parent, ()>::new();

    println!("Child: Doing thing!");

    message.add_side_effect(|parent| parent.thing_was_done = true);

    message
  }
}

///
/// This is literally the most basic example I could come up with.
///
fn main() {
  Parent::new().main();
}
Commit count: 0

cargo fmt