unitforge 0.3.17

A library for unit and quantity consistent computations in Rust
Documentation
use crate::impl_macros::macros::*;
use crate::quantities::*;

#[cfg_attr(feature = "strum", derive(EnumIter))]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, PartialEq, Debug, Hash, Eq)]
#[cfg_attr(feature = "pyo3", pyclass(eq, eq_int))]
pub enum StressUnit {
    Pa,
    kPa,
    bar,
    MPa,
    GPa,
    psi,
}

impl PhysicsUnit for StressUnit {
    fn name(&self) -> &str {
        match &self {
            StressUnit::Pa => "Pa",
            StressUnit::kPa => "kPa",
            StressUnit::bar => "bar",
            StressUnit::MPa => "MPa",
            StressUnit::GPa => "GPa",
            StressUnit::psi => "psi",
        }
    }

    fn base_per_x(&self) -> (f64, i32) {
        match self {
            StressUnit::Pa => (1., 0),
            StressUnit::kPa => (1., 3),
            StressUnit::MPa => (1., 6),
            StressUnit::bar => (1., 5),
            StressUnit::GPa => (1., 9),
            StressUnit::psi => (6.895, 3),
        }
    }
}

impl_quantity!(Stress, StressUnit, [StressUnit::MPa]);
impl_div_with_self_to_f64!(Stress);

impl Div<Stress> for f64 {
    type Output = InverseStress;

    fn div(self, rhs: Stress) -> Self::Output {
        InverseStress::from_raw(self / rhs.as_f64())
    }
}

#[cfg(feature = "pyo3")]
#[pymethods]
impl Stress {
    fn __rtruediv__(rhs: PyRef<Self>, lhs: f64) -> PyResult<InverseStress> {
        Ok(lhs / *rhs)
    }
}