Struct fend_core::Context

source ·
pub struct Context { /* private fields */ }
Expand description

This struct contains fend’s current context, including some settings as well as stored variables.

If you’re writing an interpreter it’s recommended to only instantiate this struct once so that variables and settings are preserved, but you can also manually serialise all variables and recreate the context for every calculation, depending on which is easier.

Implementations§

source§

impl Context

source

pub fn new() -> Self

Create a new context instance.

source

pub fn set_current_time_v1(&mut self, _ms_since_1970: u64, _tz_offset_secs: i64)

This method currently has no effect!

Set the current time. This API will likely change in the future!

The first argument (ms_since_1970) must be the number of elapsed milliseconds since January 1, 1970 at midnight UTC, ignoring leap seconds in the same way as unix time.

The second argument (tz_offset_secs) is the current time zone offset to UTC, in seconds.

source

pub fn use_coulomb_and_farad(&mut self)

Define the units C and F as coulomb and farad instead of degrees celsius and degrees fahrenheit.

source

pub fn set_random_u32_fn(&mut self, random_u32: fn() -> u32)

Set a random number generator

source

pub fn disable_rng(&mut self)

Clear the random number generator after setting it with via Self::set_random_u32_fn

source

pub fn set_output_mode_terminal(&mut self)

Change the output mode to fixed-width terminal style. This enables ASCII graphs in the output.

source

pub fn serialize_variables(&self, write: &mut impl Write) -> Result<(), String>

Serializes all variables defined in this context to a stream of bytes. Note that the specific format is NOT stable, and can change with any minor update.

§Errors

This function returns an error if the input cannot be serialized.

source

pub fn deserialize_variables( &mut self, read: &mut impl Read ) -> Result<(), String>

Deserializes the given variables, replacing all prior variables in the given context.

§Errors

Returns an error if the input byte stream is invalid and cannot be deserialized.

source

pub fn set_exchange_rate_handler_v1<T: ExchangeRateFn + 'static + Send + Sync>( &mut self, get_exchange_rate: T )

Set a handler function for loading exchange rates.

source

pub fn define_custom_unit_v1( &mut self, singular: &str, plural: &str, definition: &str, attribute: &CustomUnitAttribute )

Trait Implementations§

source§

impl Clone for Context

source§

fn clone(&self) -> Context

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for Context

source§

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

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

impl Default for Context

source§

fn default() -> Self

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

Auto Trait Implementations§

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

§

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

§

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

§

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.