FeagiSignal

Struct FeagiSignal 

Source
pub struct FeagiSignal<T> { /* private fields */ }
Expand description

Event signal system similar to Godot signals.

Allows subscribing callbacks that will be invoked when events are emitted. Uses FnMut to allow closures to capture and modify external state via Arc<Mutex<T>>.

§Example with Shared State

use std::sync::{Arc, Mutex};
use feagi_data_structures::FeagiSignal;
 
struct MyHandler {
    count: i32,
}
 
impl MyHandler {
    fn handle_event(&mut self, data: &String) {
        self.count += 1;
        println!("Event {}: {}", self.count, data);
    }
}
 
let handler = Arc::new(Mutex::new(MyHandler { count: 0 }));
let mut signal = FeagiSignal::new();
 
// Clone Arc for the closure
let handler_clone = Arc::clone(&handler);
signal.connect(move |data| {
    handler_clone.lock().unwrap().handle_event(data);
});
 
signal.emit(&"Hello".to_string());
assert_eq!(handler.lock().unwrap().count, 1);

Implementations§

Source§

impl<T> FeagiSignal<T>

Source

pub fn new() -> Self

Creates a new empty signal.

Source

pub fn connect<F>(&mut self, f: F) -> FeagiSignalIndex
where F: FnMut(&T) + Send + 'static,

Connects a callback to this signal.

The callback will be invoked whenever emit() is called. Returns a handle that can be used to disconnect later.

§Example
use feagi_data_structures::FeagiSignal;
 
let mut signal = FeagiSignal::new();
let handle = signal.connect(|value: &i32| {
    println!("Received: {}", value);
});
 
signal.emit(&42);
signal.disconnect(handle).unwrap();
Source

pub fn disconnect( &mut self, index: FeagiSignalIndex, ) -> Result<(), FeagiDataError>

Disconnects a previously connected callback.

Returns an error if no callback with the given index exists.

Source

pub fn emit(&mut self, value: &T)

Emits an event to all connected callbacks.

Callbacks are invoked in arbitrary order.

Source

pub fn connect_with_shared_state<S, F>( &mut self, state: Arc<Mutex<S>>, callback: F, ) -> FeagiSignalIndex
where S: Send + 'static, F: FnMut(&mut S, &T) + Send + 'static,

Helper to connect a closure that captures an Arc<Mutex<S>>.

This is a convenience method for the common pattern of calling methods on shared mutable state from within the callback.

§Example
use std::sync::{Arc, Mutex};
use feagi_data_structures::FeagiSignal;
 
struct Counter { value: i32 }
impl Counter {
    fn increment(&mut self) { self.value += 1; }
}
 
let counter = Arc::new(Mutex::new(Counter { value: 0 }));
let mut signal = FeagiSignal::new();
 
signal.connect_with_shared_state(
    Arc::clone(&counter),
    |state, _event| state.increment()
);
 
signal.emit(&"event");
assert_eq!(counter.lock().unwrap().value, 1);
Source

pub fn listener_count(&self) -> usize

Returns the number of connected listeners.

Source

pub fn disconnect_all(&mut self)

Removes all connected listeners.

Trait Implementations§

Source§

impl<T> Debug for FeagiSignal<T>

Source§

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

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

impl<T> Default for FeagiSignal<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for FeagiSignal<T>

§

impl<T> !RefUnwindSafe for FeagiSignal<T>

§

impl<T> Send for FeagiSignal<T>

§

impl<T> !Sync for FeagiSignal<T>

§

impl<T> Unpin for FeagiSignal<T>

§

impl<T> !UnwindSafe for FeagiSignal<T>

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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.