Skip to main content

IsQuantityFunction

Trait IsQuantityFunction 

Source
pub trait IsQuantityFunction:
    DynClone
    + Sync
    + Send
    + Any
    + Serialize
    + Deserialize {
    // Required methods
    fn call(&self, conditions: &[DynQuantity<f64>]) -> DynQuantity<f64>;
    fn dyn_eq(&self, other: &(dyn IsQuantityFunction + 'static)) -> bool;
}
Expand description

Trait used to construct variable quantities whose value is a (pure) function of other quantities.

Implementing this trait for a type marks it as being a variable quantity, whose value can change under the influence of other quantities. For example, a resistance can be a function of temperature:

use var_quantity::{DynQuantity, PredefUnit, Unit, IsQuantityFunction};

// The serde annotations are just here because the doctests of this crate use
// the serde feature - they are not needed if the serde feature is disabled.
#[derive(Clone, serde::Deserialize, serde::Serialize, PartialEq)]
struct Resistance;

// Again, the macro annotation is just here because of the serde feature
#[typetag::serde]
impl IsQuantityFunction for Resistance {
    fn call(&self, conditions: &[DynQuantity<f64>]) -> DynQuantity<f64> {
        let mut temperature = 0.0;
        let temperature_unit: Unit = PredefUnit::Temperature.into();
        for f in conditions.iter() {
            if f.unit == temperature_unit {
                temperature = f.value;
                break;
            }
        }
        return DynQuantity::new(1.0 + temperature / 100.0, PredefUnit::ElectricResistance);
    }

    fn dyn_eq(&self, other: &dyn IsQuantityFunction) -> bool {
        (other as &dyn std::any::Any).downcast_ref::<Self>() == Some(self)
    }
}

// Influencing factors
let infl1 = &[DynQuantity::new(6.0, PredefUnit::ElectricCurrent)];
let infl2 = &[
    DynQuantity::new(6.0, PredefUnit::ElectricCurrent),
    DynQuantity::new(20.0, PredefUnit::Temperature),
];

let resistance = Resistance {};

assert_eq!(DynQuantity::new(1.0, PredefUnit::ElectricResistance), resistance.call(&[]));
assert_eq!(DynQuantity::new(1.0, PredefUnit::ElectricResistance), resistance.call(infl1));
assert_eq!(DynQuantity::new(1.2, PredefUnit::ElectricResistance), resistance.call(infl2));

An important constraint which unfortunately cannot be covered by the type system is that the DynQuantity<f64> returned by IsQuantityFunction::call must always have the same Unit field. See the Features section and the docstring of VarQuantity for details.

§Features

When the serde feature is enabled, any type implementing IsQuantityFunction can be serialized / deserialized as a trait object using the typetag crate. This has the following implications:

In turn, this feature enables serialization / deserialization of VarQuantity without the need to specify the underlying function type in advance.

Required Methods§

Source

fn call(&self, conditions: &[DynQuantity<f64>]) -> DynQuantity<f64>

Returns a quantity as a function of conditions. See the IsQuantityFunction trait docstring for examples.

Source

fn dyn_eq(&self, other: &(dyn IsQuantityFunction + 'static)) -> bool

Returns true if self and other are identical and false otherwise.

If the implementor cannot be compared, this function should simply return false.

Trait Implementations§

Source§

impl<T> AsRef<dyn IsQuantityFunction> for QuantityFunction<T>
where T: IsQuantity,

Source§

fn as_ref(&self) -> &(dyn IsQuantityFunction + 'static)

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<'de> Deserialize<'de> for Box<dyn IsQuantityFunction>

Source§

fn deserialize<D>( deserializer: D, ) -> Result<Box<dyn IsQuantityFunction>, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<'de> Deserialize<'de> for Box<dyn IsQuantityFunction + Send>

Source§

fn deserialize<D>( deserializer: D, ) -> Result<Box<dyn IsQuantityFunction + Send>, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<'de> Deserialize<'de> for Box<dyn IsQuantityFunction + Send + Sync>

Source§

fn deserialize<D>( deserializer: D, ) -> Result<Box<dyn IsQuantityFunction + Send + Sync>, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<'de> Deserialize<'de> for Box<dyn IsQuantityFunction + Sync>

Source§

fn deserialize<D>( deserializer: D, ) -> Result<Box<dyn IsQuantityFunction + Sync>, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<'typetag> Serialize for dyn IsQuantityFunction + 'typetag

Source§

fn serialize<S>( &self, serializer: S, ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<'typetag> Serialize for dyn IsQuantityFunction + Send + 'typetag

Source§

fn serialize<S>( &self, serializer: S, ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<'typetag> Serialize for dyn IsQuantityFunction + Send + Sync + 'typetag

Source§

fn serialize<S>( &self, serializer: S, ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<'typetag> Serialize for dyn IsQuantityFunction + Sync + 'typetag

Source§

fn serialize<S>( &self, serializer: S, ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more

Implementors§