Struct Frequency

Source
#[repr(transparent)]
pub struct Frequency(pub u64);
Expand description

Represents a frequency

This struct is a wrapper around a u64 value representing the frequency in hertz. It provides methods to convert between different frequency units (Hz, kHz, MHz, GHz) and to parse frequency strings.

§Units

  • Hertz (Hz)
  • Kilohertz (kHz)
  • Megahertz (MHz)
  • Gigahertz (GHz)

§Note

When converting to a string or using display, the frequency is formatted with two decimal places. This is done to provide a consistent representation of the frequency. However, this may lead to precision loss when converting back to a number.

§Examples

use parse_frequency::Frequency;

let freq = Frequency::from_hz(parse_frequency::GIGAHERTZ);
assert_eq!(freq.as_ghz(), 1);

let freq: Frequency = "2.5GHz".parse().unwrap();
assert_eq!(freq.as_hz(), 2_500_000_000);

let strfreq: String = freq.to_string();
assert_eq!(strfreq, "2.50 GHz");

println!("Frequency: {}", freq);

Tuple Fields§

§0: u64

Implementations§

Source§

impl Frequency

Source

pub fn as_chrono_duration(&self) -> Duration

Converts the frequency to a chrono::Duration.

§Examples
use parse_frequency::Frequency;

let freq = Frequency::from_ghz(1);
let duration = freq.as_chrono_duration();
assert_eq!(duration.num_nanoseconds(), Some(1));

let freq = Frequency::from_mhz(1);
let duration = freq.as_chrono_duration();
assert_eq!(duration.num_nanoseconds(), Some(1_000));
Source§

impl Frequency

Source

pub fn as_time_duration(&self) -> Duration

Converts the frequency to a time::Duration.

§Examples
use parse_frequency::Frequency;

let freq = Frequency::from_ghz(1);
let duration = freq.as_time_duration();
assert_eq!(duration.whole_nanoseconds(), 1);

let freq = Frequency::from_mhz(1);
let duration = freq.as_time_duration();
assert_eq!(duration.whole_nanoseconds(), 1_000);
Source§

impl Frequency

Source

pub const ZERO: Self

Equivalent to 0 Hz

assert_eq!(Frequency::ZERO, Frequency::from_hz(0));
Source

pub const HERTZ: Self

Equivalent to 1 Hz

assert_eq!(Frequency::HERTZ, Frequency::from_hz(1));
Source

pub const KILOHERTZ: Self

Equivalent to 1 kHz

assert_eq!(Frequency::KILOHERTZ, Frequency::from_khz(1));
Source

pub const MEGAHERTZ: Self

Equivalent to 1 MHz

assert_eq!(Frequency::MEGAHERTZ, Frequency::from_mhz(1));
Source

pub const GIGAHERTZ: Self

Equivalent to 1 GHz

assert_eq!(Frequency::GIGAHERTZ, Frequency::from_ghz(1));
Source

pub fn from_hz(hz: u64) -> Self

Source

pub fn from_khz(khz: u64) -> Self

Source

pub fn from_mhz(mhz: u64) -> Self

Source

pub fn from_ghz(ghz: u64) -> Self

Source

pub fn as_hz(&self) -> u64

Source

pub fn as_khz(&self) -> u64

Source

pub fn as_mhz(&self) -> u64

Source

pub fn as_ghz(&self) -> u64

Source

pub fn as_duration(&self) -> Duration

Converts the frequency to a std::time::Duration.

§Examples
use parse_frequency::Frequency;

let freq = Frequency::from_ghz(1);
let duration = freq.as_duration();
assert_eq!(duration.as_nanos(), 1);
§Returns

A std::time::Duration representing the frequency.

Trait Implementations§

Source§

impl Add for Frequency

Source§

type Output = Frequency

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl Clone for Frequency

Source§

fn clone(&self) -> Frequency

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 Frequency

Source§

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

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

impl Default for Frequency

Source§

fn default() -> Frequency

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

impl<'de> Deserialize<'de> for Frequency

Source§

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

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

impl Display for Frequency

Source§

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

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

impl Div<u64> for Frequency

Source§

type Output = Frequency

The resulting type after applying the / operator.
Source§

fn div(self, rhs: u64) -> Self::Output

Performs the / operation. Read more
Source§

impl Div for Frequency

Source§

type Output = Frequency

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Self) -> Self::Output

Performs the / operation. Read more
Source§

impl FromStr for Frequency

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for Frequency

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl JsonSchema for Frequency

Source§

fn schema_name() -> String

The name of the generated JSON Schema. Read more
Source§

fn json_schema(_gen: &mut SchemaGenerator) -> Schema

Generates a JSON Schema for this type. Read more
Source§

fn is_referenceable() -> bool

Whether JSON Schemas generated for this type should be re-used where possible using the $ref keyword. Read more
Source§

fn schema_id() -> Cow<'static, str>

Returns a string that uniquely identifies the schema produced by this type. Read more
Source§

impl Mul<u64> for Frequency

Source§

type Output = Frequency

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: u64) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul for Frequency

Source§

type Output = Frequency

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl Num for Frequency

Source§

type FromStrRadixErr = ParseIntError

Source§

fn from_str_radix(s: &str, _radix: u32) -> Result<Self, Self::FromStrRadixErr>

Convert from a string and radix (typically 2..=36). Read more
Source§

impl One for Frequency

Source§

fn one() -> Self

Returns the multiplicative identity element of Self, 1. Read more
Source§

fn set_one(&mut self)

Sets self to the multiplicative identity element of Self, 1.
Source§

fn is_one(&self) -> bool
where Self: PartialEq,

Returns true if self is equal to the multiplicative identity. Read more
Source§

impl Ord for Frequency

Source§

fn cmp(&self, other: &Frequency) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Frequency

Source§

fn eq(&self, other: &Frequency) -> 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 PartialOrd for Frequency

Source§

fn partial_cmp(&self, other: &Frequency) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Rem for Frequency

Source§

type Output = Frequency

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: Self) -> Self::Output

Performs the % operation. Read more
Source§

impl Serialize for Frequency

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 Sub for Frequency

Source§

type Output = Frequency

The resulting type after applying the - operator.
Source§

fn sub(self, other: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl TryFrom<&str> for Frequency

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(s: &str) -> Result<Self>

Performs the conversion.
Source§

impl TryFrom<String> for Frequency

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(s: String) -> Result<Self>

Performs the conversion.
Source§

impl TypedValueParser for Frequency

Source§

type Value = Frequency

Argument’s value type
Source§

fn parse_ref( &self, _cmd: &Command, _arg: Option<&Arg>, value: &OsStr, ) -> Result<Frequency, Error>

Parse the argument value Read more
Source§

fn parse_ref_( &self, cmd: &Command, arg: Option<&Arg>, value: &OsStr, _source: ValueSource, ) -> Result<Self::Value, Error>

Parse the argument value Read more
Source§

fn parse( &self, cmd: &Command, arg: Option<&Arg>, value: OsString, ) -> Result<Self::Value, Error>

Parse the argument value Read more
Source§

fn parse_( &self, cmd: &Command, arg: Option<&Arg>, value: OsString, _source: ValueSource, ) -> Result<Self::Value, Error>

Parse the argument value Read more
Source§

fn possible_values( &self, ) -> Option<Box<dyn Iterator<Item = PossibleValue> + '_>>

Reflect on enumerated value properties Read more
Source§

fn map<T, F>(self, func: F) -> MapValueParser<Self, F>
where T: Send + Sync + Clone, F: Fn(Self::Value) -> T + Clone,

Adapt a TypedValueParser from one value to another Read more
Source§

fn try_map<T, E, F>(self, func: F) -> TryMapValueParser<Self, F>
where F: Fn(Self::Value) -> Result<T, E> + Clone + Send + Sync + 'static, T: Send + Sync + Clone, E: Into<Box<dyn Error + Sync + Send>>,

Adapt a TypedValueParser from one value to another Read more
Source§

impl Zero for Frequency

Source§

fn zero() -> Self

Returns the additive identity element of Self, 0. Read more
Source§

fn is_zero(&self) -> bool

Returns true if self is equal to the additive identity.
Source§

fn set_zero(&mut self)

Sets self to the additive identity element of Self, 0.
Source§

impl Copy for Frequency

Source§

impl Eq for Frequency

Source§

impl Send for Frequency

Source§

impl StructuralPartialEq for Frequency

Source§

impl Sync for Frequency

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

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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<I> IntoResettable<ValueParser> for I
where I: Into<ValueParser>,

Source§

fn into_resettable(self) -> Resettable<ValueParser>

Convert to the intended resettable type
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T, Rhs, Output> NumOps<Rhs, Output> for T
where T: Sub<Rhs, Output = Output> + Mul<Rhs, Output = Output> + Div<Rhs, Output = Output> + Add<Rhs, Output = Output> + Rem<Rhs, Output = Output>,