Function nuts::new_activity[][src]

pub fn new_activity<A>(activity: A) -> ActivityId<A> where
    A: Activity

Consumes a struct and registers it as an Activity.

nuts::new_activity(...) is the simplest method to create a new activity. It takes only a single argument, which can be any struct instance or primitive. This object will be the private data for the activity.

An ActivityId is returned, which is a handle to the newly registered activity. Use it to register callbacks on the activity.

Example:

#[derive(Default)]
struct MyActivity {
    round: usize
}
struct MyMessage {
    no: usize
}

// Create activity
let activity = MyActivity::default();
// Activity moves into globally managed state, ID to handle it is returned
let activity_id = nuts::new_activity(activity);

// Add event listener that listens to published `MyMessage` types
activity_id.subscribe(
    |my_activity, msg: &MyMessage| {
        println!("Round: {}, Message No: {}", my_activity.round, msg.no);
        my_activity.round += 1;
    }
);

// prints "Round: 0, Message No: 1"
nuts::publish( MyMessage { no: 1 } );
// prints "Round: 1, Message No: 2"
nuts::publish( MyMessage { no: 2 } );