Skip to main content

AsyncHandler

Trait AsyncHandler 

Source
pub trait AsyncHandler {
    // Required methods
    async fn on_sample_f64(
        &mut self,
        addr: &Address,
        dist: &dyn Distribution<f64>,
    ) -> f64;
    async fn on_sample_bool(
        &mut self,
        addr: &Address,
        dist: &dyn Distribution<bool>,
    ) -> bool;
    async fn on_sample_u64(
        &mut self,
        addr: &Address,
        dist: &dyn Distribution<u64>,
    ) -> u64;
    async fn on_sample_usize(
        &mut self,
        addr: &Address,
        dist: &dyn Distribution<usize>,
    ) -> usize;
    async fn on_observe_f64(
        &mut self,
        addr: &Address,
        dist: &dyn Distribution<f64>,
        value: f64,
    );
    async fn on_observe_bool(
        &mut self,
        addr: &Address,
        dist: &dyn Distribution<bool>,
        value: bool,
    );
    async fn on_observe_u64(
        &mut self,
        addr: &Address,
        dist: &dyn Distribution<u64>,
        value: u64,
    );
    async fn on_observe_usize(
        &mut self,
        addr: &Address,
        dist: &dyn Distribution<usize>,
        value: usize,
    );
    async fn on_factor(&mut self, logw: f64);
    async fn finish(self) -> Trace
       where Self: Sized;

    // Provided methods
    async fn on_sample_i64(
        &mut self,
        addr: &Address,
        _dist: &dyn Distribution<i64>,
    ) -> i64 { ... }
    async fn on_observe_i64(
        &mut self,
        addr: &Address,
        _dist: &dyn Distribution<i64>,
        _value: i64,
    ) { ... }
}
Expand description

Asynchronous counterpart of Handler, for effects resolved by I/O.

Every method has the name, arguments and meaning of the Handler method it mirrors, but is an async fn, so a handler can resolve a site over the network (a tool call, a remote model, an LLM) and await the answer instead of blocking its thread. run_async calls the methods in the order run calls the synchronous ones and awaits each before the model continues. The addr and dist a method receives are borrowed from the model node, which run_async keeps alive until the method’s future completes, so both stay usable after an .await.

Any synchronous handler is an AsyncHandler through FromSync. A complete custom handler is in the module documentation.

§Send

The methods are declared with async fn, so the trait does not bound their futures by Send, and generic code over H: AsyncHandler cannot require that they are (stable Rust cannot yet name that bound; return-type notation, rust-lang/rust#109417, is unstable). For a concrete handler type the compiler sees the actual futures, because auto traits leak through async fn: if the handler is Send, none of its methods holds anything else !Send (an Rc, a RefCell borrow) across an .await, and the model’s value type is Send, then run_async(handler, model) is Send. A handler that also borrows nothing makes it 'static, so the run can be spawned on a multi-threaded executor. The trait stays usable by handlers that are not Send, which run on a single-threaded executor instead.

Example:


// Use a built-in handler, through the synchronous adapter
let mut rng = StdRng::seed_from_u64(42);
let handler = FromSync(PriorHandler {
    rng: &mut rng,
    trace: Trace::default(),
});
let model = sample(addr!("x"), Normal::new(0.0, 1.0).unwrap());

// Any executor can drive the future; this one is tokio's.
let rt = tokio::runtime::Builder::new_current_thread().build().unwrap();
let (x, trace) = rt.block_on(run_async(handler, model));
assert_eq!(trace.get_f64(&addr!("x")), Some(x));

Required Methods§

Source

async fn on_sample_f64( &mut self, addr: &Address, dist: &dyn Distribution<f64>, ) -> f64

Handle an f64 sampling operation (continuous distributions).

Source

async fn on_sample_bool( &mut self, addr: &Address, dist: &dyn Distribution<bool>, ) -> bool

Handle a bool sampling operation (Bernoulli).

Source

async fn on_sample_u64( &mut self, addr: &Address, dist: &dyn Distribution<u64>, ) -> u64

Handle a u64 sampling operation (Poisson, Binomial).

Source

async fn on_sample_usize( &mut self, addr: &Address, dist: &dyn Distribution<usize>, ) -> usize

Handle a usize sampling operation (Categorical).

Source

async fn on_observe_f64( &mut self, addr: &Address, dist: &dyn Distribution<f64>, value: f64, )

Handle an f64 observation operation.

Source

async fn on_observe_bool( &mut self, addr: &Address, dist: &dyn Distribution<bool>, value: bool, )

Handle a bool observation operation.

Source

async fn on_observe_u64( &mut self, addr: &Address, dist: &dyn Distribution<u64>, value: u64, )

Handle a u64 observation operation.

Source

async fn on_observe_usize( &mut self, addr: &Address, dist: &dyn Distribution<usize>, value: usize, )

Handle a usize observation operation.

Source

async fn on_factor(&mut self, logw: f64)

Handle a factor operation.

Called when the model reaches a factor statement; the handler typically adds logw to the trace’s log_factors.

§Arguments
  • logw - Log-weight to add to the model’s total weight
Source

async fn finish(self) -> Trace
where Self: Sized,

Finalize the handler and return the accumulated trace.

run_async awaits this once, after the model has returned its value. It is never called for a run whose future is dropped first.

Provided Methods§

Source

async fn on_sample_i64( &mut self, addr: &Address, _dist: &dyn Distribution<i64>, ) -> i64

Handle an i64 sampling operation (signed discrete distributions).

Like Handler::on_sample_i64, this defaults to a panic with the same message, handler does not implement on_sample_i64 (i64 sample site at {addr}), so a handler only for models without Model::SampleI64 nodes need not implement it. FromSync forwards to the wrapped handler’s method.

Source

async fn on_observe_i64( &mut self, addr: &Address, _dist: &dyn Distribution<i64>, _value: i64, )

Handle an i64 observation operation.

Like Handler::on_observe_i64, this defaults to a panic with the same message, handler does not implement on_observe_i64 (i64 observe site at {addr}). FromSync forwards to the wrapped handler’s method.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§