Module factory

Module factory 

Source
Expand description

Observable factory pattern

This module introduces the ObservableFactory trait, which provides a convenient way to create observable sequences (e.g., via of, from_iter, timer).

The ObservableFactory trait is designed to be automatically implemented for any type that satisfies the necessary Context trait bounds. This means users typically do not implement ObservableFactory directly, but rather implement Context for their custom types, which then automatically gain factory capabilities.

This design eliminates boilerplate, reduces API duplication, and allows for flexible scheduler injection by relying on the Context to provide the execution environment.

§Usage

ObservableFactory methods are available on any type that implements Context and whose associated Scheduler type implements Default. The most common way to use this is through the default Local and Shared marker types provided in rxrust::prelude, or by defining your own custom Context types (see rxrust::context documentation for details on custom Context implementations).

§Examples

use rxrust::prelude::*;

// Create a local observable that emits 42 and completes.
// `Local` here refers to `rxrust::prelude::Local`,
// which implements `Context` with `LocalScheduler`.
Local::of(42).subscribe(|v| println!("Received local: {}", v));

// Create a shared observable that emits "hello" and completes.
// `Shared` here refers to `rxrust::prelude::Shared`,
// which implements `Context` with `SharedScheduler`.
Shared::of("hello").subscribe(|v| println!("Received shared: {}", v));

// Create a timer that fires after 500ms
Local::timer(Duration::from_millis(500)).subscribe(|_| println!("Timer fired!"));

§Trivial Observables

This factory provides several methods for creating “trivial” observables that emit limited or no values. These are particularly useful for testing, edge cases, and specific reactive patterns:

§Available Trivial Observables

MethodDescriptionCompletionValues EmittedError Emitted
empty()Completes immediately without emitting any values✅ YesNoneNone
never()Never emits any values and never completes❌ NoNoneNone
throw_err()Immediately emits an error without any values❌ NoNoneYes

§Use Cases for Trivial Observables

  • Testing: Create predictable streams for unit tests
  • Fallback scenarios: Provide default behavior when no data is available
  • Error handling: Simulate failure conditions
  • Infinite streams: Create streams that require manual termination
  • Base cases: Handle edge cases in conditional observable chains
§empty() - Immediate Completion
use rxrust::prelude::*;

// Useful for representing empty collections or completion signals
Local::empty()
  .on_complete(|| println!("Stream completed immediately"))
  .subscribe(|v| println!("This won't be called"));
§never() - Infinite Stream
use rxrust::prelude::*;

// Useful for testing timeout or cancellation behavior
let subscription = Local::never().subscribe(|v| println!("This won't be called"));

// Must manually unsubscribe to avoid memory leaks
drop(subscription);
§throw_err() - Immediate Error
use rxrust::prelude::*;

// Useful for testing error handling or representing failures
Local::throw_err("Network error".to_string())
  .on_error(|e| println!("Error occurred: {}", e))
  .subscribe(|v| println!("This won't be called"));

For detailed information about each observable implementation, see the trivial module.

Traits§

ObservableFactory
ObservableFactory trait for creating observable sequences.