#[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
impl Frequency
Sourcepub fn as_chrono_duration(&self) -> Duration
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
impl Frequency
Sourcepub fn as_time_duration(&self) -> Duration
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
impl Frequency
Sourcepub const KILOHERTZ: Self
pub const KILOHERTZ: Self
Equivalent to 1 kHz
assert_eq!(Frequency::KILOHERTZ, Frequency::from_khz(1));
Sourcepub const MEGAHERTZ: Self
pub const MEGAHERTZ: Self
Equivalent to 1 MHz
assert_eq!(Frequency::MEGAHERTZ, Frequency::from_mhz(1));
Sourcepub const GIGAHERTZ: Self
pub const GIGAHERTZ: Self
Equivalent to 1 GHz
assert_eq!(Frequency::GIGAHERTZ, Frequency::from_ghz(1));
pub fn from_hz(hz: u64) -> Self
pub fn from_khz(khz: u64) -> Self
pub fn from_mhz(mhz: u64) -> Self
pub fn from_ghz(ghz: u64) -> Self
pub fn as_hz(&self) -> u64
pub fn as_khz(&self) -> u64
pub fn as_mhz(&self) -> u64
pub fn as_ghz(&self) -> u64
Sourcepub fn as_duration(&self) -> Duration
pub fn as_duration(&self) -> Duration
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Frequency
impl<'de> Deserialize<'de> for Frequency
Source§fn deserialize<D>(
deserializer: D,
) -> Result<Frequency, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
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 JsonSchema for Frequency
impl JsonSchema for Frequency
Source§fn schema_name() -> String
fn schema_name() -> String
The name of the generated JSON Schema. Read more
Source§fn json_schema(_gen: &mut SchemaGenerator) -> Schema
fn json_schema(_gen: &mut SchemaGenerator) -> Schema
Generates a JSON Schema for this type. Read more
Source§fn is_referenceable() -> bool
fn is_referenceable() -> bool
Whether JSON Schemas generated for this type should be re-used where possible using the
$ref
keyword. Read moreSource§impl Num for Frequency
impl Num for Frequency
type FromStrRadixErr = ParseIntError
Source§fn from_str_radix(s: &str, _radix: u32) -> Result<Self, Self::FromStrRadixErr>
fn from_str_radix(s: &str, _radix: u32) -> Result<Self, Self::FromStrRadixErr>
Convert from a string and radix (typically
2..=36
). Read moreSource§impl Ord for Frequency
impl Ord for Frequency
Source§impl PartialOrd for Frequency
impl PartialOrd for Frequency
Source§impl Serialize for Frequency
impl Serialize for Frequency
Source§fn serialize<S>(
&self,
serializer: S,
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
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 TypedValueParser for Frequency
impl TypedValueParser for Frequency
Source§fn parse_ref(
&self,
_cmd: &Command,
_arg: Option<&Arg>,
value: &OsStr,
) -> Result<Frequency, Error>
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>
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>
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>
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> + '_>>
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>
fn map<T, F>(self, func: F) -> MapValueParser<Self, F>
Adapt a
TypedValueParser
from one value to another Read moreimpl Copy for Frequency
impl Eq for Frequency
impl Send for Frequency
impl StructuralPartialEq for Frequency
impl Sync for Frequency
Auto Trait Implementations§
impl Freeze for Frequency
impl RefUnwindSafe for Frequency
impl Unpin for Frequency
impl UnwindSafe for Frequency
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<I> IntoResettable<ValueParser> for Iwhere
I: Into<ValueParser>,
impl<I> IntoResettable<ValueParser> for Iwhere
I: Into<ValueParser>,
Source§fn into_resettable(self) -> Resettable<ValueParser>
fn into_resettable(self) -> Resettable<ValueParser>
Convert to the intended resettable type