signal

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 that returns a reference to the value, allowing read access.
    • This reference behaves like a normal immutable reference for most purposes.
  2. A _set(value) function to write the value (returns true if changed).
  3. A function with the same name as the variable that directly returns the value by dereferencing the underlying variable. This requires the type to implement Copy.

§Requirements

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

§Examples

use reactive_macros::{ref_signal, signal};

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

assert_eq!(A(), 10);          // returns value directly (requires Copy)
assert_eq!(*A_get(), 10);     // returns a reference to the value
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.