SafeReplayHandler

Struct SafeReplayHandler 

Source
pub struct SafeReplayHandler<'r, R: RngCore> {
    pub rng: &'r mut R,
    pub base: Trace,
    pub trace: Trace,
    pub warn_on_mismatch: bool,
}
Expand description

Safe version of ReplayHandler that gracefully handles trace inconsistencies.

SafeReplayHandler replays model execution like ReplayHandler, but handles type mismatches and missing addresses gracefully by logging warnings and sampling fresh values instead of panicking. Essential for production systems where trace consistency cannot be guaranteed.

Example:


// Create trace with potential inconsistencies
let mut rng = StdRng::seed_from_u64(42);
let (_, base_trace) = runtime::handler::run(
    PriorHandler { rng: &mut rng, trace: Trace::default() },
    sample(addr!("x"), Normal::new(0.0, 1.0).unwrap()) // f64 value
);

// Safe replay handles type mismatch gracefully
let (result, trace) = runtime::handler::run(
    SafeReplayHandler {
        rng: &mut rng,
        base: base_trace,
        trace: Trace::default(),
        warn_on_mismatch: true, // Enable warnings
    },
    sample(addr!("x"), Bernoulli::new(0.5).unwrap()) // Expects bool
);

assert!(trace.total_log_weight().is_finite()); // Continues execution

Fields§

§rng: &'r mut R

Random number generator for sampling at addresses not in base trace.

§base: Trace

Base trace containing values to replay.

§trace: Trace

New trace to accumulate the replay execution.

§warn_on_mismatch: bool

Whether to log warnings on type mismatches (useful for debugging).

Trait Implementations§

Source§

impl<'r, R: RngCore> Handler for SafeReplayHandler<'r, R>

Source§

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

Handle an f64 sampling operation (continuous distributions).
Source§

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

Handle a bool sampling operation (Bernoulli).
Source§

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

Handle a u64 sampling operation (Poisson, Binomial).
Source§

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

Handle a usize sampling operation (Categorical).
Source§

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

Handle an f64 observation operation.
Source§

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

Handle a bool observation operation.
Source§

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

Handle a u64 observation operation.
Source§

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

Handle a usize observation operation.
Source§

fn on_factor(&mut self, logw: f64)

Handle a factor operation. Read more
Source§

fn finish(self) -> Trace

Finalize the handler and return the accumulated trace. Read more

Auto Trait Implementations§

§

impl<'r, R> Freeze for SafeReplayHandler<'r, R>

§

impl<'r, R> RefUnwindSafe for SafeReplayHandler<'r, R>
where R: RefUnwindSafe,

§

impl<'r, R> Send for SafeReplayHandler<'r, R>
where R: Send,

§

impl<'r, R> Sync for SafeReplayHandler<'r, R>
where R: Sync,

§

impl<'r, R> Unpin for SafeReplayHandler<'r, R>

§

impl<'r, R> !UnwindSafe for SafeReplayHandler<'r, R>

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> 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, 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V