Struct avr_simulator::AvrDuration
source · pub struct AvrDuration { /* private fields */ }
Expand description
Like std::time::Duration
, but in AVR cycles; somewhat approximate¹.
Implementations§
source§impl AvrDuration
impl AvrDuration
sourcepub const fn new(clock_frequency: u32, cycles: u64) -> Self
pub const fn new(clock_frequency: u32, cycles: u64) -> Self
Creates a new duration for given clock frequency and number of cycles:
AvrDuration::new(
16_000_000, /* 16 MHz */
8_000_000, /* 8 million clock cycles (=500ms here) */
);
If you’re using AvrTester, you might find AvrDurationExt
more
convenient than this constructor.
sourcepub const fn add_cycles(self, n: u64) -> Self
pub const fn add_cycles(self, n: u64) -> Self
Returns a new duration with increased number of cycles.
§Examples
let tt = AvrDuration::new(16_000_000, 8_000_000);
assert_eq!(500, tt.as_millis());
assert_eq!(750, tt.add_cycles(4_000_000).as_millis());
sourcepub const fn add_micros(self, n: u64) -> Self
pub const fn add_micros(self, n: u64) -> Self
Returns a new duration with increased number of microseconds.
§Examples
let tt = AvrDuration::new(16_000_000, 8_000_000);
assert_eq!(500, tt.as_millis());
assert_eq!(501, tt.add_micros(1_000).as_millis());
sourcepub const fn add_millis(self, millis: u64) -> Self
pub const fn add_millis(self, millis: u64) -> Self
Returns a new duration with increased number of milliseconds.
§Examples
let tt = AvrDuration::new(16_000_000, 8_000_000);
assert_eq!(500, tt.as_millis());
assert_eq!(515, tt.add_millis(15).as_millis());
sourcepub const fn add_secs(self, secs: u64) -> Self
pub const fn add_secs(self, secs: u64) -> Self
Returns a new duration with increased number of seconds.
§Examples
let tt = AvrDuration::new(16_000_000, 8_000_000);
assert_eq!(500, tt.as_millis());
assert_eq!(2500, tt.add_secs(2).as_millis());
sourcepub const fn with_cycles(self, n: u64) -> Self
pub const fn with_cycles(self, n: u64) -> Self
Returns a new duration with specified number of cycles.
§Examples
let tt = AvrDuration::new(16_000_000, 8_000_000);
assert_eq!(250, tt.with_cycles(4_000_000).as_millis());
sourcepub const fn with_micros(self, n: u64) -> Self
pub const fn with_micros(self, n: u64) -> Self
Returns a new duration with specified number of microseconds.
§Examples
let tt = AvrDuration::new(16_000_000, 8_000_000);
assert_eq!(1, tt.with_micros(1_000).as_millis());
sourcepub const fn with_millis(self, n: u64) -> Self
pub const fn with_millis(self, n: u64) -> Self
Returns a new duration with specified number of milliseconds.
§Examples
let tt = AvrDuration::new(16_000_000, 8_000_000);
assert_eq!(15, tt.with_millis(15).as_millis());
sourcepub const fn with_secs(self, n: u64) -> Self
pub const fn with_secs(self, n: u64) -> Self
Returns a new duration with specified number of seconds.
§Examples
let tt = AvrDuration::new(16_000_000, 8_000_000);
assert_eq!(2000, tt.with_secs(2).as_millis());
sourcepub const fn clock_frequency(self) -> u32
pub const fn clock_frequency(self) -> u32
Returns the clock frequency associated with this duration (e.g. 16 MHz).
§Examples
let tt = AvrDuration::new(16_000_000, 8_000_000);
assert_eq!(16_000_000, tt.clock_frequency());
sourcepub const fn as_cycles(self) -> u64
pub const fn as_cycles(self) -> u64
Returns the number of cycles contained by this duration.
§Examples
let tt = AvrDuration::new(16_000_000, 8_000_000);
assert_eq!(8_000_000, tt.as_cycles());
let tt = AvrDuration::new(16_000_000, 0).add_secs(3);
assert_eq!(48_000_000, tt.as_cycles());
sourcepub fn as_micros(self) -> u64
pub fn as_micros(self) -> u64
Returns the number of microseconds contained by this duration.
§Examples
let tt = AvrDuration::new(16_000_000, 40);
assert_eq!(3, tt.as_micros());
sourcepub fn as_micros_f64(self) -> f64
pub fn as_micros_f64(self) -> f64
Returns the number of microseconds contained by this duration as f64
.
§Examples
let tt = AvrDuration::new(16_000_000, 40);
assert_eq!(2.5, tt.as_micros_f64());
sourcepub fn as_millis(self) -> u64
pub fn as_millis(self) -> u64
Returns the number of milliseconds contained by this duration.
§Examples
let tt = AvrDuration::new(16_000_000, 40_000);
assert_eq!(3, tt.as_millis());
sourcepub fn as_millis_f64(self) -> f64
pub fn as_millis_f64(self) -> f64
Returns the number of milliseconds contained by this duration as f64
.
§Examples
let tt = AvrDuration::new(16_000_000, 40_000);
assert_eq!(2.5, tt.as_millis_f64());
sourcepub fn as_secs(self) -> u64
pub fn as_secs(self) -> u64
Returns the number of seconds contained by this duration.
§Examples
let tt = AvrDuration::new(16_000_000, 40_000_000);
assert_eq!(3, tt.as_secs());
sourcepub fn as_secs_f64(self) -> f64
pub fn as_secs_f64(self) -> f64
Returns the number of seconds contained by this duration as f64
.
§Examples
let tt = AvrDuration::new(16_000_000, 40_000_000);
assert_eq!(2.5, tt.as_secs_f64());
Trait Implementations§
source§impl Add for AvrDuration
impl Add for AvrDuration
§Examples
let a = AvrDuration::new(16_000_000, 1_000);
let b = AvrDuration::new(16_000_000, 2_000);
assert_eq!(
AvrDuration::new(16_000_000, 3_000),
a + b,
);
source§impl AddAssign for AvrDuration
impl AddAssign for AvrDuration
§Examples
let mut a = AvrDuration::new(16_000_000, 1_000);
a += AvrDuration::new(16_000_000, 2_000);
assert_eq!(AvrDuration::new(16_000_000, 3_000), a);
source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+=
operation. Read moresource§impl Clone for AvrDuration
impl Clone for AvrDuration
source§fn clone(&self) -> AvrDuration
fn clone(&self) -> AvrDuration
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for AvrDuration
impl Debug for AvrDuration
source§impl Display for AvrDuration
impl Display for AvrDuration
§Examples
let tt = AvrDuration::new(16_000_000, 0).add_millis(123);
assert_eq!("123000 µs", tt.to_string());
source§impl Hash for AvrDuration
impl Hash for AvrDuration
source§impl Ord for AvrDuration
impl Ord for AvrDuration
source§fn cmp(&self, other: &AvrDuration) -> Ordering
fn cmp(&self, other: &AvrDuration) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl PartialEq for AvrDuration
impl PartialEq for AvrDuration
source§fn eq(&self, other: &AvrDuration) -> bool
fn eq(&self, other: &AvrDuration) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialOrd for AvrDuration
impl PartialOrd for AvrDuration
source§fn partial_cmp(&self, other: &AvrDuration) -> Option<Ordering>
fn partial_cmp(&self, other: &AvrDuration) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl Sub for AvrDuration
impl Sub for AvrDuration
§Examples
let a = AvrDuration::new(16_000_000, 3_000);
let b = AvrDuration::new(16_000_000, 2_000);
assert_eq!(
AvrDuration::new(16_000_000, 1_000),
a - b,
);
let a = AvrDuration::new(16_000_000, 3_000);
let b = AvrDuration::new(16_000_000, 4_000);
assert_eq!(
AvrDuration::new(16_000_000, 0),
a - b,
);
source§impl SubAssign for AvrDuration
impl SubAssign for AvrDuration
§Examples
let mut a = AvrDuration::new(16_000_000, 3_000);
a -= AvrDuration::new(16_000_000, 2_000);
assert_eq!(AvrDuration::new(16_000_000, 1_000), a);
let mut a = AvrDuration::new(16_000_000, 3_000);
a -= AvrDuration::new(16_000_000, 4_000);
assert_eq!(AvrDuration::new(16_000_000, 0), a);
source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-=
operation. Read more