RelativeHumidity

Struct RelativeHumidity 

Source
pub struct RelativeHumidity(/* private fields */);
Expand description

A validated relative humidity.

It stores a PERCENT in [0, 100], and Self::fraction gives the same humidity in [0, 1]. The two constructors name the unit at the call site, and there is deliberately no From<f64>, so a bare number is never read in the wrong unit.

One rule, the sensor band (HUMIDITY_SENSOR_TOLERANCE_PERCENT): a reading up to 5 points outside the range is the saturated or dry air it measures, stored as exactly 100 or 0, so it flies exactly as that end does. Anything farther out, or not finite, is refused with an AtmosphereError that reports the value unchanged.

use ballistics_engine::RelativeHumidity;

let from_percent = RelativeHumidity::try_percent(50.0).unwrap();
let from_fraction = RelativeHumidity::try_fraction(0.5).unwrap();
assert_eq!(from_percent, from_fraction);
assert_eq!(from_percent, RelativeHumidity::DEFAULT);
assert_eq!(from_fraction.percent(), 50.0);
assert_eq!(from_percent.fraction(), 0.5);

// A sensor reading in fog, or in very dry air, is that air.
assert_eq!(RelativeHumidity::try_percent(101.7).unwrap().percent(), 100.0);
assert_eq!(RelativeHumidity::try_percent(-2.0).unwrap(), RelativeHumidity::DRY);
assert_eq!(RelativeHumidity::try_fraction(1.03).unwrap().percent(), 100.0);

assert!(RelativeHumidity::try_percent(150.0).is_err());
assert!(RelativeHumidity::try_percent(105.0000001).is_err());
// A percent sent where a fraction belongs is refused, not read as 50 %.
assert!(RelativeHumidity::try_fraction(50.0).is_err());

A bare f64 does not convert, because it does not say whether 0.5 means 50 % or 0.5 % (E0277: no From<f64>):

ⓘ
use ballistics_engine::RelativeHumidity;

let humidity: RelativeHumidity = 0.5.into();

Implementations§

Source§

impl RelativeHumidity

Source

pub const DEFAULT: Self

The default for an omitted humidity: DEFAULT_HUMIDITY_PERCENT, 50 %.

Source

pub const DRY: Self

Dry air, 0 %.

Source

pub const FRACTION_BAND: (f64, f64) = HUMIDITY_FRACTION_BAND

The lowest and the highest fraction Self::try_fraction accepts, both included: the sensor band (HUMIDITY_SENSOR_TOLERANCE_PERCENT) in fraction units, exactly (-0.05, 1.05).

For a schema that publishes the bounds the decoder enforces (the MCP server’s relative_humidity), so the band it states is derived from the one number rather than written out again.

Source

pub fn try_percent(percent: f64) -> Result<Self, AtmosphereError>

A relative humidity given in PERCENT.

Accepts a finite percent from -5 to 105, both ends included (the sensor band, HUMIDITY_SENSOR_TOLERANCE_PERCENT). A percent in 0.0..=100.0 is stored unchanged, one above 100 as exactly 100 and one below 0 (or -0.0) as 0.0. Anything else is refused, with field humidity_percent, the value unchanged, and reason AtmosphereErrorReason::NotFinite or AtmosphereErrorReason::HumidityPercentOutOfRange.

Source

pub fn try_fraction(fraction: f64) -> Result<Self, AtmosphereError>

A relative humidity given as a FRACTION, the SI wire’s relative_humidity.

Accepts a finite fraction from -0.05 to 1.05, both ends included (the sensor band in fraction units), and stores the fraction snapped into 0.0..=1.0 times 100: an in-range fraction is fraction * 100.0, exact at 0, 0.5 and 1 and within one ulp of the decimal percent elsewhere, one above 1 is exactly 100, one below 0 is 0. Anything else is refused, with field relative_humidity, the value unchanged, and reason AtmosphereErrorReason::NotFinite or AtmosphereErrorReason::HumidityFractionOutOfRange; so a percent sent by mistake (50.0) is an error rather than 5000 %.

Source

pub const fn percent(self) -> f64

The humidity in percent, 0.0..=100.0.

Source

pub const fn fraction(self) -> f64

The humidity as a fraction, 0.0..=1.0 (percent / 100).

A const fn, so a wire default can be written as the one default in the wire’s unit: RelativeHumidity::DEFAULT.fraction() is exactly 0.5.

Trait Implementations§

Source§

impl Clone for RelativeHumidity

Source§

fn clone(&self) -> RelativeHumidity

Returns a duplicate 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 RelativeHumidity

Source§

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

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

impl PartialEq for RelativeHumidity

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for RelativeHumidity

Source§

impl StructuralPartialEq for RelativeHumidity

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> 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> Same for T

Source§

type Output = T

Should always be Self
§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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, 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.
Source§

impl<T> Scalar for T
where T: 'static + Clone + PartialEq + Debug,