Skip to main content

Value

Enum Value 

Source
pub enum Value {
    F64(f64),
    Int(i64),
    U64(u64),
    Usize(usize),
    Bool(bool),
    Arr(Arc<Vec<f64>>),
}
Expand description

A value computed by a Program.

Sample sites bind their natural type — Bernoulli binds Value::Bool, Poisson/Binomial bind Value::U64, Categorical binds Value::Usize, DiscreteUniform binds Value::Int, continuous distributions bind Value::F64 — and a compiled program builds a Model<Value>, so a program returns whichever of these it computes.

The language is dynamically typed with a few numeric coercions, and the accessors below expose exactly those coercions, so a host function reading its arguments sees the same conversions the built-ins do:

accessoraccepts
as_f64any number; true/false as 1.0/0.0
as_boola bool; a number as “is nonzero” (NaN has no truth value)
as_i64an integer in range, an integral finite F64, a bool as 0/1
as_u64 / as_usizeas as_i64, and non-negative
as_arrayan array
use fugue::program::Value;

assert_eq!(Value::Usize(2).as_f64(), Some(2.0));
assert_eq!(Value::F64(3.0).as_usize(), Some(3));
assert_eq!(Value::F64(2.5).as_i64(), None);
assert_eq!(Value::Int(-1).as_u64(), None);
assert_eq!(Value::Bool(true).as_f64(), Some(1.0));
assert_eq!(Value::F64(0.0).as_bool(), Some(false));
assert_eq!(Value::from(vec![0.5, 0.5]).as_array(), Some(&[0.5, 0.5][..]));

Variants§

§

F64(f64)

A float: 2.0-style literals, continuous sites, float arithmetic.

§

Int(i64)

A signed integer: 2-style literals, loop variables, .len(), integer arithmetic, and i64 sites (DiscreteUniform).

§

U64(u64)

An unsigned count: the value of a u64 site (Poisson, Binomial).

§

Usize(usize)

An index: the value of a usize site (Categorical).

§

Bool(bool)

A boolean: true/false, comparisons, &&/||/!, and bool sites (Bernoulli).

§

Arr(Arc<Vec<f64>>)

An array of numbers: a data array or an array literal [a, b, c].

Implementations§

Source§

impl Value

Source

pub fn type_name(&self) -> &'static str

The value’s type as the language names it in messages: "f64", "int", "u64", "usize", "bool" or "array".

Source

pub fn as_f64(&self) -> Option<f64>

The value as an f64: any number converts, and a bool is 1.0/0.0. None for an array.

Source

pub fn as_bool(&self) -> Option<bool>

The value’s truth: a bool is itself, a number is “nonzero”. None for NaN (which is neither) and for an array.

Source

pub fn as_i64(&self) -> Option<i64>

The value as an i64: an integer in range, a finite integral F64 in range, or a bool as 0/1. None otherwise.

Source

pub fn as_u64(&self) -> Option<u64>

The value as a u64: as as_i64, but non-negative (and a U64 above i64::MAX converts too).

Source

pub fn as_usize(&self) -> Option<usize>

The value as a usize: as as_u64, and in range for the platform’s usize.

Source

pub fn as_array(&self) -> Option<&[f64]>

The array’s elements, or None for a scalar.

Trait Implementations§

Source§

impl Clone for Value

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 Debug for Value

Source§

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

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

impl Display for Value

Formats the value the way Rust’s Display formats the corresponding Rust value, which is what makes addr!("x", i) in a program byte-identical to addr!("x", i) in compiled Rust: 3, 0.5, true. An array prints as [1, 2.5] (arrays are not valid address indices).

Source§

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

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

impl From<&[f64]> for Value

Source§

fn from(v: &[f64]) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<f64>> for Value

Source§

fn from(v: Vec<f64>) -> Self

Converts to this type from the input type.
Source§

impl From<bool> for Value

Source§

fn from(b: bool) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for Value

Source§

fn from(x: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for Value

Source§

fn from(i: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for Value

Source§

fn from(i: i64) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for Value

Source§

fn from(u: u64) -> Self

Converts to this type from the input type.
Source§

impl From<usize> for Value

Source§

fn from(u: usize) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Value

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Value

Auto Trait Implementations§

§

impl Freeze for Value

§

impl RefUnwindSafe for Value

§

impl Send for Value

§

impl Sync for Value

§

impl Unpin for Value

§

impl UnsafeUnpin for Value

§

impl UnwindSafe for Value

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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