[][src]Struct revent::Slot

pub struct Slot<T: ?Sized> { /* fields omitted */ }

List of Nodes to a signal T.

A slot is a list in which one can store node handles.

use revent::{Anchor, Grapher, Manager, Node, Slot};
use std::{cell::RefCell, rc::Rc};

trait BasicSignal {
    fn basic(&mut self);
}

struct MyAnchor {
    basic_slot: Slot<dyn BasicSignal>,
    mng: Manager,
}
impl MyAnchor {
    fn new() -> Self {
        let mng = Manager::new();
        Self {
            basic_slot: Slot::new("basic_slot", &mng),
            mng,
        }
    }
}
impl Anchor for MyAnchor {
    fn manager(&self) -> &Manager {
        &self.mng
    }
}

// ---

struct MyNode;
impl Node<MyAnchor, ()> for MyNode {
    fn register_emits(_: &MyAnchor) -> () { () }

    fn register_listens(hub: &mut MyAnchor, item: Rc<RefCell<Self>>) {
        hub.basic_slot.register(item);
    }
    const NAME: &'static str = "MyNode";
}
impl BasicSignal for MyNode {
    fn basic(&mut self) {
        println!("Hello from MyNode::basic");
    }
}

// ---

let mut hub = MyAnchor::new();
let item = hub.subscribe(|_| MyNode);
hub.basic_slot.emit(|x| x.basic());
hub.unsubscribe(&item);

Grapher::new(hub.manager()).graph_to_file("target/slot-example.png").unwrap();

Methods

impl<T: ?Sized> Slot<T>[src]

pub fn new(name: &'static str, manager: &Manager) -> Self[src]

Create a new slot object.

The manager is used to organize multiple single/slot objects and to ensure that there are no recursive (double mutable borrow) signal chains.

name is used for error reporting and graph generation in Manager.

pub fn emit<F>(&mut self, caller: F) where
    F: FnMut(&mut T), 
[src]

Emit a signal on this signal slot.

pub fn emit_short<F>(&mut self, caller: F) where
    F: FnMut(&mut T) -> bool
[src]

Continue emitting only if the caller returns true. Stops if false.

pub fn sort_by<F>(&mut self, compare: F) where
    F: FnMut(&T, &T) -> Ordering
[src]

Sort the nodes to this slot.

pub fn register(&mut self, item: Rc<RefCell<T>>)[src]

Add or remove a subscriber object to this slot.

The action taken depends on whether Anchor::subscribe or Anchor::unsubscribe was called.

When adding: pushes the item to the end of the list. See sort_by if a different order is desired. When removing: finds the first matching instance and removes it.

Panics

Panics if called from Anchor::unsubscribe while not being registered.

Panics if called more than once for the same object from within Anchor::subscribe.

Trait Implementations

impl<T: ?Sized> Clone for Slot<T>[src]

fn clone(&self) -> Self[src]

Cloning is only valid from within an Anchor::subscribe context.

impl<T: ?Sized> Debug for Slot<T>[src]

Auto Trait Implementations

impl<T> !RefUnwindSafe for Slot<T>

impl<T> !Send for Slot<T>

impl<T> !Sync for Slot<T>

impl<T: ?Sized> Unpin for Slot<T>

impl<T> !UnwindSafe for Slot<T>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.