ref_signal

Macro ref_signal 

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

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

The ref_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).

Unlike signal!, ref_signal! does not generate a same-named function that directly returns the value.

§Requirements

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

§Examples

use reactive_macros::ref_signal;

ref_signal!(static mut A: String = "hello".to_string(););

assert_eq!(&*A_get(), "hello");
assert!(A_set("signal".to_string()));
assert_eq!(&*A_get(), "signal");
assert!(!A_set("signal".to_string())); // 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.