Struct Context

Source
pub struct Context<'ctx, T = DefaultTypeSet>
where T: TypeSet, T::Integer: Load<T> + Store<T>, T::Float: Load<T> + Store<T>, T::SignedInteger: Load<T> + Store<T>,
{ /* private fields */ }
Expand description

The expression context, which holds variables, functions, and other state needed for evaluation.

Implementations§

Source§

impl<'ctx> Context<'ctx, DefaultTypeSet>

Source

pub fn new() -> Self

Creates a new context with default types.

Source§

impl<'ctx, T> Context<'ctx, T>
where T: TypeSet, T::Integer: Load<T> + Store<T> + Integer, T::Float: Load<T> + Store<T>, T::SignedInteger: Load<T> + Store<T>, <T::Integer as ValueType>::NegateOutput: Load<T> + Store<T>, <T::SignedInteger as ValueType>::NegateOutput: Load<T> + Store<T>, <T::Float as ValueType>::NegateOutput: Load<T> + Store<T>,

Source

pub fn new_with_types() -> Self

Creates a new context. The type set must be specified when using this function.

use somni_expr::{Context, TypeSet32};
let mut ctx = Context::<TypeSet32>::new_with_types();
Source

pub fn evaluate_any<'s>( &mut self, expression: &'s str, ) -> Result<TypedValue<T>, ExpressionError<'s>>

Evaluates an expression and returns the result as a TypedValue<T>.

Source

pub fn evaluate<'s, V>( &'s mut self, expression: &'s str, ) -> Result<V::Output<'s>, ExpressionError<'s>>
where V: Load<T>,

Evaluates an expression and returns the result as a specific value type.

This function will attempt to convert the result of the expression to the specified type V. If the conversion fails, it will return an ExpressionError.

Source

pub fn add_variable<V>(&mut self, name: &str, value: V)
where V: Store<T>,

Defines a new variable in the context.

Source

pub fn add_function<F, A>(&mut self, name: &str, func: F)
where F: DynFunction<A, T> + 'ctx,

Adds a new function to the context.

Trait Implementations§

Source§

impl<'ctx, T> Default for Context<'ctx, T>
where T: TypeSet + Default, T::Integer: Load<T> + Store<T>, T::Float: Load<T> + Store<T>, T::SignedInteger: Load<T> + Store<T>,

Source§

fn default() -> Context<'ctx, T>

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

impl<T> ExprContext<T> for Context<'_, T>
where T: TypeSet, T::Integer: Load<T> + Store<T> + Integer, T::Float: Load<T> + Store<T>, T::SignedInteger: Load<T> + Store<T>,

Source§

fn intern_string(&mut self, s: &str) -> StringIndex

Implements string interning.
Source§

fn load_interned_string(&self, idx: StringIndex) -> &str

Loads an interned string.
Source§

fn try_load_variable(&self, variable: &str) -> Option<TypedValue<T>>

Attempts to load a variable from the context.
Source§

fn address_of(&self, variable: &str) -> TypedValue<T>

Returns the address of a variable in the context.
Source§

fn call_function( &mut self, function_name: &str, args: &[TypedValue<T>], ) -> Result<TypedValue<T>, FunctionCallError>

Calls a function in the context.

Auto Trait Implementations§

§

impl<'ctx, T> Freeze for Context<'ctx, T>
where <T as TypeSet>::SignedInteger: Sized, <T as TypeSet>::Float: Sized, <T as TypeSet>::Integer: Sized,

§

impl<'ctx, T = DefaultTypeSet> !RefUnwindSafe for Context<'ctx, T>

§

impl<'ctx, T = DefaultTypeSet> !Send for Context<'ctx, T>

§

impl<'ctx, T = DefaultTypeSet> !Sync for Context<'ctx, T>

§

impl<'ctx, T> Unpin for Context<'ctx, T>
where <T as TypeSet>::SignedInteger: Sized + Unpin, <T as TypeSet>::Float: Sized + Unpin, <T as TypeSet>::Integer: Sized + Unpin, T: Unpin,

§

impl<'ctx, T = DefaultTypeSet> !UnwindSafe for Context<'ctx, T>

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.