Struct ActivityId

Source
pub struct ActivityId<A> { /* private fields */ }
Expand description

Handle to an Activity that has been registered, with a type parameter to track the activity’s type. Can be used to add type-checked closures to the activity, which will be used as event listeners.

Implements Copy and Clone

Implementations§

Source§

impl<A: Activity> ActivityId<A>

Source

pub fn on_enter<F>(&self, f: F)
where F: Fn(&mut A) + 'static,

Registers a callback closure that is called when an activity changes from inactive to active. Multiple handlers can be registered.

Source

pub fn on_enter_domained<F>(&self, f: F)
where F: Fn(&mut A, &mut DomainState) + 'static,

Same as on_enter but with domain access in closure

Source

pub fn on_leave<F>(&self, f: F)
where F: Fn(&mut A) + 'static,

Registers a callback closure that is called when an activity changes from active to inactive. Multiple handlers can be registered.

Source

pub fn on_leave_domained<F>(&self, f: F)
where F: Fn(&mut A, &mut DomainState) + 'static,

Same as on_leave but with domain access in closure

Source

pub fn on_delete<F>(&self, f: F)
where F: FnOnce(A) + 'static,

Registers a callback closure that is called when an activity is deleted. Only one handler can be registered because it takes ownership of the data. A second registration will overwrite the first handler.

Source

pub fn on_delete_domained<F>(&self, f: F)
where F: FnOnce(A, &mut DomainState) + 'static,

Same as on_delete but with domain access in closure

Source

pub fn subscribe<F, MSG>(&self, f: F)
where F: Fn(&mut A, &MSG) + 'static, MSG: Any,

Registers a callback closure on an activity with a specific topic to listen to.

By default, the activity will only receive calls when it is active. Use subscribe_masked for more control over this behavior.

§Example
struct MyActivity { id: usize };
struct MyMessage { text: String };

pub fn main() {
    let activity = nuts::new_activity(MyActivity { id: 0 } );
    activity.subscribe(
        |activity: &mut MyActivity, message: &MyMessage|
        println!("Subscriber with ID {} received text: {}", activity.id, message.text)
    );
}

In the example above, a subscription is created that waits for messages of type MyMessage to be published. So far, the code inside the closure is not executed and nothing is printed to the console.

Note that the first argument of the closure is a mutable reference to the activity object. The second argument is a read-only reference to the published message. Both types must match exactly or otherwise the closure will not be accepted by the compiler.

A function with the correct argument types can also be used to subscribe.

struct MyActivity { id: usize };
struct MyMessage { text: String };

pub fn main() {
    let activity = nuts::new_activity(MyActivity { id: 0 } );
    activity.subscribe(MyActivity::print_text);
}

impl MyActivity {
    fn print_text(&mut self, message: &MyMessage) {
        println!("Subscriber with ID {} received text: {}", self.id, message.text)
    }
}
Source

pub fn subscribe_mut<F, MSG>(&self, f: F)
where F: Fn(&mut A, &mut MSG) + 'static, MSG: Any,

Same as subscribe but gives mutable access to the message object.

Make sure to use the correct signature for the function, the Rust compiler may give strange error messages otherwise. For example, the message must be borrowed by the subscription handler.

Source

pub fn subscribe_domained<F, MSG>(&self, f: F)
where F: Fn(&mut A, &mut DomainState, &MSG) + 'static, MSG: Any,

Registers a callback closure on an activity with a specific topic to listen to. Has mutable access to the DomainState object.

By default, the activity will only receive calls when it is active. Use subscribe_domained_masked for more control over this behavior.

Make sure to use the correct signature for the function, the Rust compiler may give strange error messages otherwise. For example, the message must be borrowed by the subscription handler.

§Panics

Panics if the activity has not been registered with a domain.

Source

pub fn subscribe_domained_mut<F, MSG>(&self, f: F)
where F: Fn(&mut A, &mut DomainState, &mut MSG) + 'static, MSG: Any,

Same as subscribe_domained but gives mutable access to the message object.

Source

pub fn private_channel<F, MSG>(&self, f: F)
where F: Fn(&mut A, MSG) + 'static, MSG: Any,

Registers a callback closure on an activity with a specific topic to listen to. Messages sent with nuts::publish() are NOT received, only messages sent with nuts::send_to().

Attention! The handler takes ownership of the message. It will compile if it is borrowed instead, but then it will also expect a reference to be published. (Which usually doesn’t work due to lifetimes) Then, it will not react to normally sent messages and can be difficult to debug.

Since the listener takes ownership, it is not possible to have more than one private channel active for the same activity at the same time. If multiple private channels are added to an activity, only the last listener is retained. (Older ones are replaced and deleted)

Source

pub fn private_domained_channel<F, MSG>(&self, f: F)
where F: Fn(&mut A, &mut DomainState, MSG) + 'static, MSG: Any,

Variant of private_channel with access to the domain state.

§Panics

Panics if the activity has not been registered with a domain.

Source

pub fn private_channel_masked<F, MSG>(&self, mask: SubscriptionFilter, f: F)
where F: Fn(&mut A, MSG) + 'static, MSG: Any,

Variant of ``private_channel` with subscription mask.

Source

pub fn private_domained_channel_masked<F, MSG>( &self, mask: SubscriptionFilter, f: F, )
where F: Fn(&mut A, &mut DomainState, MSG) + 'static, MSG: Any,

Variant of private_channel with access to the domain state and subscription mask.

§Panics

Panics if the activity has not been registered with a domain.

Source

pub fn subscribe_masked<F, MSG>(&self, mask: SubscriptionFilter, f: F)
where F: Fn(&mut A, &MSG) + 'static, MSG: Any,

Registers a callback closure on an activity with a specific topic to listen to with filtering options.

Source

pub fn subscribe_masked_mut<F, MSG>(&self, mask: SubscriptionFilter, f: F)
where F: Fn(&mut A, &mut MSG) + 'static, MSG: Any,

Same as subscribe_masked but gives mutable access to the message object.

Source

pub fn subscribe_domained_masked<F, MSG>(&self, mask: SubscriptionFilter, f: F)
where F: Fn(&mut A, &mut DomainState, &MSG) + 'static, MSG: Any,

Registers a callback closure on an activity with a specific topic to listen to with filtering options. Has mutable access to the DomainState object.

§Panics

Panics if the activity has not been registered with a domain.

Source

pub fn subscribe_domained_masked_mut<F, MSG>( &self, mask: SubscriptionFilter, f: F, )
where F: Fn(&mut A, &mut DomainState, &mut MSG) + 'static, MSG: Any,

Same as subscribe_domained_masked but gives mutable access to the message object.

Source

pub fn set_status(&self, status: LifecycleStatus)

Changes the lifecycle status of the activity

§Panics

If status is set to something other than Deleted after it has been Deleted

Source

pub fn private_message<MSG: Any>(&self, msg: MSG)

Publish a message to a specific activity.

If you lack access to an ActivityId, use nuts::send_to() or UncheckedActivityId::private_message. Both are equivalent.

Trait Implementations§

Source§

impl<A> Clone for ActivityId<A>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<A: Debug> Debug for ActivityId<A>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<A: Hash> Hash for ActivityId<A>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<A> Into<UncheckedActivityId> for ActivityId<A>

Source§

fn into(self) -> UncheckedActivityId

Converts this type into the (usually inferred) input type.
Source§

impl<A: Ord> Ord for ActivityId<A>

Source§

fn cmp(&self, other: &ActivityId<A>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<A: PartialEq> PartialEq for ActivityId<A>

Source§

fn eq(&self, other: &ActivityId<A>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<A: PartialOrd> PartialOrd for ActivityId<A>

Source§

fn partial_cmp(&self, other: &ActivityId<A>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<A> Copy for ActivityId<A>

Source§

impl<A: Eq> Eq for ActivityId<A>

Source§

impl<A> StructuralPartialEq for ActivityId<A>

Auto Trait Implementations§

§

impl<A> Freeze for ActivityId<A>

§

impl<A> RefUnwindSafe for ActivityId<A>
where A: RefUnwindSafe,

§

impl<A> Send for ActivityId<A>
where A: Send,

§

impl<A> Sync for ActivityId<A>
where A: Sync,

§

impl<A> Unpin for ActivityId<A>
where A: Unpin,

§

impl<A> UnwindSafe for ActivityId<A>
where A: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Activity for T
where T: Any,