Macro signal

Source
signal!() { /* proc-macro */ }
Expand description

Wraps a static mut variable as a reactive signal (similar to a property) with getter and setter functions.

The signal! macro transforms a static mut variable into a reactive_cache::Signal, and automatically generates:

  1. A _get() function to read the value.
  2. A _set(value) function to write the value (returns true if changed).
  3. A function with the same name as the variable to simplify access (calls _get()).

§Requirements

  • The macro currently supports only static mut variables.
  • The variable type must implement Eq + Default.

§Examples

use std::cell::Cell;
use reactive_macros::signal;

signal!(static mut A: i32 = 10;);

assert_eq!(A(), 10);
assert_eq!(A_get(), 10);
assert!(A_set(20));
assert_eq!(A(), 20);
assert!(!A_set(20)); // No change

§SAFETY

This macro wraps static mut variables internally, so it is not thread-safe. It should be used only in single-threaded contexts.

§Warning

Do not set any signal that is part of the same effect chain.

Effects automatically run whenever one of their dependent signals changes. If an effect modifies a signal that it (directly or indirectly) observes, it creates a circular dependency. This can lead to:

  • an infinite loop of updates, or
  • conflicting updates that the system cannot resolve.

In the general case, it is impossible to automatically determine whether such an effect will ever terminate—this is essentially a version of the halting problem. Therefore, you must ensure manually that effects do not update signals within their own dependency chain.