Skip to main content

FromSync

Struct FromSync 

Source
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: H

Trait Implementations§

Source§

impl<H: Handler> AsyncHandler for FromSync<H>

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_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, )

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_observe_i64( &mut self, addr: &Address, dist: &dyn Distribution<i64>, value: i64, )

Handle an i64 observation operation. Read more
Source§

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

Handle a factor operation. Read more
Source§

async fn finish(self) -> Trace

Finalize the handler and return the accumulated trace. Read more
Source§

impl<H: Clone> Clone for FromSync<H>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<H: Copy> Copy for FromSync<H>

Source§

impl<H: Debug> Debug for FromSync<H>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<H: Default> Default for FromSync<H>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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