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>
impl<T> FeagiSignal<T>
Sourcepub fn connect<F>(&mut self, f: F) -> FeagiSignalIndex
pub fn connect<F>(&mut self, f: F) -> FeagiSignalIndex
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();Sourcepub fn disconnect(
&mut self,
index: FeagiSignalIndex,
) -> Result<(), FeagiDataError>
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.
Sourcepub fn emit(&mut self, value: &T)
pub fn emit(&mut self, value: &T)
Emits an event to all connected callbacks.
Callbacks are invoked in arbitrary order.
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);Sourcepub fn listener_count(&self) -> usize
pub fn listener_count(&self) -> usize
Returns the number of connected listeners.
Sourcepub fn disconnect_all(&mut self)
pub fn disconnect_all(&mut self)
Removes all connected listeners.
Trait Implementations§
Source§impl<T> Debug for FeagiSignal<T>
impl<T> Debug for FeagiSignal<T>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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