pub struct FromSync<H>(pub H);Expand description
A synchronous Handler used as an AsyncHandler.
Each method calls the wrapped handler’s method of the same name and
completes without suspending, so run_async(FromSync(h), m) yields exactly
what run(h, m) does: the same value, the
same trace, and the same draws from the handler’s RNG. Use it to hand the
built-in interpreters (PriorHandler,
ReplayHandler,
ScoreGivenTrace and the others) to code written
against AsyncHandler.
Example:
// Score a recorded trace from inside async code
let mut recorded = Trace::default();
recorded.insert_choice(addr!("x"), ChoiceValue::F64(0.5), 0.0);
let scorer = FromSync(ScoreGivenTrace {
base: recorded,
trace: Trace::default(),
});
let model = sample(addr!("x"), Normal::new(0.0, 1.0).unwrap());
let rt = tokio::runtime::Builder::new_current_thread().build().unwrap();
let (x, scored) = rt.block_on(run_async(scorer, model));
assert_eq!(x, 0.5);
assert_eq!(scored.log_prior, Normal::new(0.0, 1.0).unwrap().log_prob(&0.5));Tuple Fields§
§0: HTrait Implementations§
Source§impl<H: Handler> AsyncHandler for FromSync<H>
impl<H: Handler> AsyncHandler for FromSync<H>
Source§async fn on_sample_f64(
&mut self,
addr: &Address,
dist: &dyn Distribution<f64>,
) -> f64
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
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
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
async fn on_sample_usize( &mut self, addr: &Address, dist: &dyn Distribution<usize>, ) -> usize
Handle a usize sampling operation (Categorical).
Source§async fn on_sample_i64(
&mut self,
addr: &Address,
dist: &dyn Distribution<i64>,
) -> i64
async fn on_sample_i64( &mut self, addr: &Address, dist: &dyn Distribution<i64>, ) -> i64
Handle an i64 sampling operation (signed discrete distributions). Read more
Source§async fn on_observe_f64(
&mut self,
addr: &Address,
dist: &dyn Distribution<f64>,
value: f64,
)
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,
)
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,
)
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,
)
async fn on_observe_usize( &mut self, addr: &Address, dist: &dyn Distribution<usize>, value: usize, )
Handle a usize observation operation.
Source§async fn on_observe_i64(
&mut self,
addr: &Address,
dist: &dyn Distribution<i64>,
value: i64,
)
async fn on_observe_i64( &mut self, addr: &Address, dist: &dyn Distribution<i64>, value: i64, )
Handle an i64 observation operation. Read more
impl<H: Copy> Copy for FromSync<H>
Auto Trait Implementations§
impl<H> Freeze for FromSync<H>where
H: Freeze,
impl<H> RefUnwindSafe for FromSync<H>where
H: RefUnwindSafe,
impl<H> Send for FromSync<H>where
H: Send,
impl<H> Sync for FromSync<H>where
H: Sync,
impl<H> Unpin for FromSync<H>where
H: Unpin,
impl<H> UnsafeUnpin for FromSync<H>where
H: UnsafeUnpin,
impl<H> UnwindSafe for FromSync<H>where
H: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more