Skip to main content

RxRef

Struct RxRef 

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

A read-write reactive value that holds a current state.

RxRef provides both read and write access to a reactive value. It exposes a read-only RxVal via the .val() method, and allows updating the value via the .set() method.

When the value is set, all subscribers to the RxVal are notified.

§Example

use rx_rs::core::{RxRef, DisposableTracker};

let mut tracker = DisposableTracker::new();
let client_id = RxRef::new(None);

// Subscribe to changes
client_id.val().subscribe(tracker.tracker(), |maybe_id| {
    match maybe_id {
        Some(id) => println!("Client ID assigned: {}", id),
        None => println!("No client ID yet"),
    }
}); // Prints "No client ID yet" immediately

// Update the value
client_id.set(Some(42)); // Prints "Client ID assigned: 42"

Implementations§

Source§

impl<T> RxRef<T>
where T: Clone + PartialEq + 'static,

Source

pub fn new(value: T) -> Self

Creates a new RxRef with the given initial value.

§Arguments
  • value - The initial value
§Example
use rx_rs::core::RxRef;

let connection_state = RxRef::new("Disconnected");
Source

pub fn set(&self, value: T)

Sets a new value and notifies all subscribers.

All subscribers to the RxVal obtained via .val() will be called with the new value.

§Arguments
  • value - The new value to set
§Example
use rx_rs::core::RxRef;

let state = RxRef::new(0);
state.set(42);
assert_eq!(state.get(), 42);
Source

pub fn get(&self) -> T

Gets the current value.

This is a convenience method that delegates to the underlying RxVal.

§Example
use rx_rs::core::RxRef;

let counter = RxRef::new(5);
assert_eq!(counter.get(), 5);
Source

pub fn val(&self) -> RxVal<T>

Returns a read-only view of this reactive value.

The returned RxVal can be cloned and passed around, allowing multiple parts of the code to subscribe to changes without having write access.

§Example
use rx_rs::core::{RxRef, DisposableTracker};

let mut tracker = DisposableTracker::new();
let state = RxRef::new("initial");
let read_only = state.val();

read_only.subscribe(tracker.tracker(), |val| {
    println!("State: {}", val);
});
Source

pub fn modify<F>(&self, f: F)
where F: FnOnce(&mut T),

Modifies the value using a closure and notifies subscribers.

This is useful when you need to update the value based on its current state, without having to clone it first.

§Arguments
  • f - Function that receives a mutable reference to the current value
§Example
use rx_rs::core::RxRef;

let counter = RxRef::new(0);
counter.modify(|count| *count += 1);
assert_eq!(counter.get(), 1);
Source

pub fn subscriber_count(&self) -> usize

Returns the number of active subscribers.

Source

pub fn debug_ptr(&self) -> usize

Returns a pointer address for debugging identity. Use this to check if two RxRef instances share the same underlying data.

Source

pub fn stream(&self) -> RxObservable<T>

Converts this RxRef into a stream (RxObservable).

The returned observable emits the current value immediately on subscription, and then emits all future changes.

This is a convenience method that delegates to the underlying RxVal’s .stream().

§Example
use rx_rs::core::{RxRef, DisposableTracker};

let mut tracker = DisposableTracker::new();
let state = RxRef::new(0);
let stream = state.stream();

stream.subscribe(tracker.tracker(), |value| {
    println!("State: {}", value);
}); // Prints "State: 0" immediately

state.set(42); // Prints "State: 42"
Source

pub fn map<B, F>(&self, f: F) -> RxVal<B>
where B: Clone + PartialEq + 'static, F: Fn(&T) -> B + 'static,

Maps the values of this RxRef using a transformation function.

Returns a new RxVal that always contains the transformed value. When the source RxRef changes, the transformation is applied and the resulting RxVal is updated.

§Arguments
  • f - Function to transform values from A to B
§Example
use rx_rs::core::RxRef;

let number = RxRef::new(5);
let doubled = number.map(|x| x * 2);

assert_eq!(doubled.get(), 10);

number.set(10);
assert_eq!(doubled.get(), 20);
Source

pub fn flat_map<B, F>(&self, f: F) -> RxVal<B>
where B: Clone + PartialEq + 'static, F: Fn(&T) -> RxVal<B> + 'static,

Flat-maps the values of this RxRef using a function that returns RxVal.

When the source RxRef changes, the function is called to produce a new RxVal, and the result RxVal is updated to reflect the current value of that inner RxVal. The result also updates when the inner RxVal changes.

This is a convenience method that delegates to the underlying RxVal’s .flat_map().

§Arguments
  • f - Function to transform values from A to RxVal
§Example
use rx_rs::core::RxRef;

let outer = RxRef::new(1);
let inner1 = RxRef::new(10);
let inner2 = RxRef::new(20);

let inner1_clone = inner1.clone();
let inner2_clone = inner2.clone();

let flattened = outer.flat_map(move |&x| {
    if x == 1 { inner1_clone.val() } else { inner2_clone.val() }
});

assert_eq!(flattened.get(), 10);

outer.set(2);
assert_eq!(flattened.get(), 20);
Source

pub fn flat_map_ref<B, F>(&self, f: F) -> RxVal<B>
where B: Clone + PartialEq + 'static, F: Fn(&T) -> RxRef<B> + 'static,

Flat-maps using a function that returns RxRef.

Source

pub fn flat_map_observable<B, F>(&self, f: F) -> RxObservable<B>
where B: Clone + 'static, F: Fn(&T) -> RxObservable<B> + 'static,

Flat-maps using a function that returns RxObservable.

Source

pub fn flat_map_subject<B, F>(&self, f: F) -> RxObservable<B>
where B: Clone + 'static, F: Fn(&T) -> RxSubject<B> + 'static,

Flat-maps using a function that returns RxSubject.

Source

pub fn zip_val<U>(&self, other: RxVal<U>) -> RxVal<(T, U)>
where U: Clone + PartialEq + 'static,

Combines this RxRef with another RxVal.

Source

pub fn zip_ref<U>(&self, other: RxRef<U>) -> RxVal<(T, U)>
where U: Clone + PartialEq + 'static,

Combines this RxRef with another RxRef.

Trait Implementations§

Source§

impl<T: Clone> Clone for RxRef<T>

Source§

fn clone(&self) -> RxRef<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<T> Freeze for RxRef<T>

§

impl<T> !RefUnwindSafe for RxRef<T>

§

impl<T> !Send for RxRef<T>

§

impl<T> !Sync for RxRef<T>

§

impl<T> Unpin for RxRef<T>

§

impl<T> UnsafeUnpin for RxRef<T>

§

impl<T> !UnwindSafe for RxRef<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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.