pub trait Tester {
// Required method
fn test(&self) -> bool;
// Provided methods
fn into_box(self) -> BoxTester
where Self: Sized + 'static { ... }
fn into_rc(self) -> RcTester
where Self: Sized + 'static { ... }
fn into_arc(self) -> ArcTester
where Self: Sized + Send + Sync + 'static { ... }
fn into_fn(self) -> impl Fn() -> bool
where Self: Sized + 'static { ... }
fn to_box(&self) -> BoxTester
where Self: Clone + 'static { ... }
fn to_rc(&self) -> RcTester
where Self: Clone + 'static { ... }
fn to_arc(&self) -> ArcTester
where Self: Clone + Send + Sync + 'static { ... }
fn to_fn(&self) -> impl Fn() -> bool
where Self: Clone + 'static { ... }
}Expand description
Tests whether a state or condition holds
Tester is a functional abstraction for testing states or conditions. It accepts no parameters and returns a boolean value indicating the test result of some state or condition.
§Core Characteristics
- No input parameters: Captures context through closures
- Returns boolean: Indicates test results
- Uses
&self: Does not modify its own state, only reads external state - Repeatable calls: The same Tester can call
test()multiple times
§Use Cases
- State checking: Check system or service status
- Condition waiting: Repeatedly check until conditions are met
- Health monitoring: Check system health status
- Precondition validation: Verify conditions before operations
§Design Philosophy
Tester’s responsibility is “test judgment”, not “state management”. State management is the caller’s responsibility. Tester only reads state and returns judgment results.
§Examples
use prism3_function::{BoxTester, Tester};
use std::sync::{Arc, atomic::{AtomicBool, Ordering}};
// State managed externally
let ready = Arc::new(AtomicBool::new(false));
let ready_clone = Arc::clone(&ready);
// Tester only responsible for reading state
let tester = BoxTester::new(move || {
ready_clone.load(Ordering::Acquire)
});
// Can be called multiple times
assert!(!tester.test());
ready.store(true, Ordering::Release);
assert!(tester.test());§Author
Hu Haixing
Required Methods§
Sourcefn test(&self) -> bool
fn test(&self) -> bool
Executes the test and returns the test result
This method can be called multiple times without modifying the Tester’s own state.
§Return Value
Returns true if the condition holds, otherwise returns false
§Examples
use prism3_function::{BoxTester, Tester};
let tester = BoxTester::new(|| true);
assert!(tester.test());