Skip to main content

RxVal

Struct RxVal 

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

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

When subscribed, the subscriber is called immediately with the current value, and then called again whenever the value changes.

This is useful for representing state that always has a current value, like connection status, client ID, or configuration values.

§Example

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

let mut tracker = DisposableTracker::new();
let rx_ref = RxRef::new(42);
let rx_val = rx_ref.val();

rx_val.subscribe(tracker.tracker(), |value| {
    println!("Value: {}", value);
}); // Prints "Value: 42" immediately

rx_ref.set(100); // Prints "Value: 100"

Implementations§

Source§

impl<T: 'static> RxVal<T>

Source

pub fn get(&self) -> T
where T: Clone,

Gets the current value.

Source

pub fn subscribe<F>(&self, tracker: &Tracker, f: F)
where F: FnMut(&T) + 'static, T: Clone,

Subscribes to value changes.

The subscriber function is called immediately with the current value, and then called again whenever the value changes.

The subscription is automatically cleaned up when the tracker is dropped.

§Arguments
  • tracker - Tracker that will manage this subscription’s lifetime
  • f - Function called with a reference to the value on each update
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 RxVal instances share the same underlying data.

Source

pub fn stream(&self) -> RxObservable<T>
where T: Clone,

Converts this RxVal into a stream (RxObservable).

The returned observable does NOT emit the current value immediately on subscription. It only emits when the RxVal changes to a new value.

Note: If you subscribe directly to the RxVal, it WILL emit immediately. But when converted to a stream, it behaves like a pure observable.

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

let mut tracker = DisposableTracker::new();
let rx_ref = RxRef::new(42);
let stream = rx_ref.val().stream();

stream.subscribe(tracker.tracker(), |value| {
    println!("Value: {}", value);
}); // Does NOT print immediately

rx_ref.set(100); // Prints "Value: 100"
Source

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

Maps the values of this RxVal using a transformation function.

Returns a new RxVal that always contains the transformed value. When the source RxVal 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.val().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 T: Clone + PartialEq, B: Clone + PartialEq + 'static, F: Fn(&T) -> RxVal<B> + 'static,

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

When the source RxVal 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.

§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.val().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 T: Clone + PartialEq, B: Clone + PartialEq + 'static, F: Fn(&T) -> RxRef<B> + 'static,

Flat-maps using a function that returns RxRef. Delegates to flat_map by converting the RxRef to RxVal.

Source

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

Flat-maps using a function that returns RxObservable. Returns an RxObservable that switches to the new observable on each change.

Source

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

Flat-maps using a function that returns RxSubject. Delegates to flat_map_observable by converting the RxSubject to RxObservable.

Source

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

Combines this RxVal with another RxVal, producing a new RxVal containing a tuple.

The resulting RxVal updates whenever either source changes.

§Arguments
  • other - Another RxVal to combine with
§Example
use rx_rs::core::RxRef;

let name = RxRef::new("Alice");
let age = RxRef::new(30);

let combined = name.val().zip_val(age.val());

assert_eq!(combined.get(), ("Alice", 30));

name.set("Bob");
assert_eq!(combined.get(), ("Bob", 30));
Source

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

Combines this RxVal with an RxRef. Delegates to zip_val by converting the RxRef to RxVal.

Trait Implementations§

Source§

impl<T: Clone> Clone for RxVal<T>

Source§

fn clone(&self) -> RxVal<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 RxVal<T>

§

impl<T> !RefUnwindSafe for RxVal<T>

§

impl<T> !Send for RxVal<T>

§

impl<T> !Sync for RxVal<T>

§

impl<T> Unpin for RxVal<T>

§

impl<T> UnsafeUnpin for RxVal<T>

§

impl<T> !UnwindSafe for RxVal<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.