pub struct ConstantRateDuration { /* private fields */ }
Expand description
Represents the duration of a dataset which has been sampled at a constant rate.
Examples
Consider an audio file which consists of 8_394_223
samples per
channel recorded with a 48kHz sampling frequency. We can see what
the duration of this is by using the default implementation of
std::fmt::Display
for a ConstantRateDuration
which outputs the
duration in the form hh:mm:ss;samples
.
use sampled_data_duration::ConstantRateDuration;
let crd = ConstantRateDuration::new(8_394_223, 48000);
assert_eq!(crd.to_string(), "00:02:54;42223");
Implementations
sourceimpl ConstantRateDuration
impl ConstantRateDuration
sourcepub fn new(count: u64, rate: u64) -> ConstantRateDuration
pub fn new(count: u64, rate: u64) -> ConstantRateDuration
Construct a new ConstantRateDuration
, where count
corresponds to the number of samples and rate
is the
sampling rate in Hertz.
sourcepub fn as_secs(&self) -> u64
pub fn as_secs(&self) -> u64
Returns the number of whole seconds contained by this ConstantRateDuration
.
The returned value does not include the fractional part of the
duration which can be obtained with subsec_secs
.
Example
use sampled_data_duration::ConstantRateDuration;
let crd = ConstantRateDuration::new(48000 * 62 + 123, 48000);
assert_eq!(crd.to_string(), "00:01:02;123");
assert_eq!(crd.as_secs(), 62);
sourcepub fn as_mins(&self) -> u64
pub fn as_mins(&self) -> u64
Returns the number of whole minutes contained by this ConstantRateDuration
.
The returned value does not include the fractional part of the duration, and can be a value greater than 59.
Example
use sampled_data_duration::ConstantRateDuration;
let crd = ConstantRateDuration::new(48000 * 60 * 91, 48000);
assert_eq!(crd.to_string(), "01:31:00;0");
assert_eq!(crd.as_mins(), 91);
sourcepub fn as_hours(&self) -> u64
pub fn as_hours(&self) -> u64
Returns the number of whole hours contained by this ConstantRateDuration
.
The returned value does not include the fractional part of the duration, and can be a value greater than 23.
Example
use sampled_data_duration::ConstantRateDuration;
let crd = ConstantRateDuration::new(48000 * 60 * 60 * 48 + 12345, 48000);
assert_eq!(crd.to_string(), "48:00:00;12345");
assert_eq!(crd.as_hours(), 48);
sourcepub fn as_days(&self) -> u64
pub fn as_days(&self) -> u64
Returns the number of whole days contained by this ConstantRateDuration
.
The returned value does not include the fractional part of the duration, and can be a value greater than 6.
Example
use sampled_data_duration::ConstantRateDuration;
let crd = ConstantRateDuration::new(48000 * 60 * 60 * 48 + 12345, 48000);
assert_eq!(crd.to_string(), "48:00:00;12345");
assert_eq!(crd.as_days(), 2);
sourcepub fn as_weeks(&self) -> u64
pub fn as_weeks(&self) -> u64
Returns the number of whole weeks contained by this ConstantRateDuration
.
The returned value does not include the fractional part of the duration.
Example
use sampled_data_duration::ConstantRateDuration;
let crd = ConstantRateDuration::new(48000 * 60 * 60 * 24 * 21 + 12345, 48000);
assert_eq!(crd.to_string(), "504:00:00;12345");
assert_eq!(crd.as_weeks(), 3);
sourcepub fn subsec_samples(&self) -> u64
pub fn subsec_samples(&self) -> u64
Returns the number of samples in the sub-second part of this
ConstantRateDuration
.
The returned value will always be less than the sampling rate.
Example
use sampled_data_duration::ConstantRateDuration;
let crd = ConstantRateDuration::new(48000 + 12345, 48000);
assert_eq!(crd.to_string(), "00:00:01;12345");
assert_eq!(crd.subsec_samples(), 12345);
sourcepub fn subsec_nanos(&self) -> u32
pub fn subsec_nanos(&self) -> u32
Returns the whole number of nanoseconds in the fractional
part of this ConstantRateDuration
.
The returned value will always be less than one second i.e. >
1_000_000_000
nanoseconds.
Example
use sampled_data_duration::ConstantRateDuration;
let crd = ConstantRateDuration::new(48000 + 24000, 48000);
assert_eq!(crd.to_string(), "00:00:01;24000");
assert_eq!(crd.subsec_nanos(), 500_000_000);
sourcepub fn subsec_secs(&self) -> f64
pub fn subsec_secs(&self) -> f64
Return the sub-second part of this duration in seconds.
This will return a value in the range 0.0 <= subsec_secs < 1.0
.
Example
use sampled_data_duration::ConstantRateDuration;
let crd = ConstantRateDuration::new(48000 + 24000, 48000);
assert_eq!(crd.to_string(), "00:00:01;24000");
assert_eq!(crd.subsec_secs(), 0.5);
sourcepub fn submin_secs(&self) -> u64
pub fn submin_secs(&self) -> u64
Returns the whole number of seconds left over when this duration is measured in minutes.
The returned value will always be 0 <= submin_secs
<= 59.
Example
use sampled_data_duration::ConstantRateDuration;
let crd = ConstantRateDuration::new(48000 * 65 + 32000, 48000);
assert_eq!(crd.to_string(), "00:01:05;32000");
assert_eq!(crd.submin_secs(), 5);
sourcepub fn subhour_mins(&self) -> u64
pub fn subhour_mins(&self) -> u64
Returns the whole number of minutes left over when this duration is measured in hours.
The returned value will always be 0 <= subhour_mins
<= 59.
Example
use sampled_data_duration::ConstantRateDuration;
let crd = ConstantRateDuration::new(48000 * 60 * 68, 48000);
assert_eq!(crd.to_string(), "01:08:00;0");
assert_eq!(crd.subhour_mins(), 8);
sourcepub fn subday_hours(&self) -> u64
pub fn subday_hours(&self) -> u64
Returns the whole number of hours left over when this duration is measured in days.
The returned value will always be 0 <= subday_hours
<= 23.
Example
use sampled_data_duration::ConstantRateDuration;
let crd = ConstantRateDuration::new(48000 * 60 * 60 * 25, 48000);
assert_eq!(crd.to_string(), "25:00:00;0");
assert_eq!(crd.subday_hours(), 1);
sourcepub fn subweek_days(&self) -> u64
pub fn subweek_days(&self) -> u64
Returns the whole number of days left over when this duration is measured in weeks.
The returned value will always be 0 <= subweek_days
<= 6.
Example
use sampled_data_duration::ConstantRateDuration;
let crd = ConstantRateDuration::new(48000 * 60 * 60 * 24 * 9, 48000);
assert_eq!(crd.to_string(), "216:00:00;0");
assert_eq!(crd.subweek_days(), 2);
sourcepub fn to_duration(&self) -> Duration
pub fn to_duration(&self) -> Duration
Returns this ConstantRateDuration
as a std::time::Duration
.
Example
use sampled_data_duration::ConstantRateDuration;
let crd = ConstantRateDuration::new(48000 * 42, 48000);
assert_eq!(crd.to_string(), "00:00:42;0");
assert_eq!(crd.to_duration().as_secs_f64(), 42.0);
sourcepub fn try_add(
self,
other: ConstantRateDuration
) -> Result<ConstantRateDuration, MixedRateDuration>
pub fn try_add(
self,
other: ConstantRateDuration
) -> Result<ConstantRateDuration, MixedRateDuration>
Computes self + other
returning Ok(ConstantRateDuration)
if the sampling rates of self
and other
are the same, or
Err(MixedRateDuration)
if they are not.
If the sample count of the new duration exceeds the maximum
capacity of a u64
then this method will panic with the error
message "overflow when adding ConstantRateDurations"
.
Examples
use sampled_data_duration::*;
let a = ConstantRateDuration::new(48000, 48000);
let b = ConstantRateDuration::new(48000 * 2, 48000);
if let Ok(c) = a.try_add(b) {
assert_eq!(c.as_secs(), 3);
} else {
assert!(false);
}
Errors
Will return a Err(MixedRateDuration)
if the provided
ConstantRateDurations
have incommensurate sampling rates.
sourcepub fn try_add_assign(
&mut self,
crd: ConstantRateDuration
) -> Result<(), MixedRateDuration>
pub fn try_add_assign(
&mut self,
crd: ConstantRateDuration
) -> Result<(), MixedRateDuration>
Add-assigns crd
to this ConstantRateDuration
returning
Ok(())
if the sampling rates of self
and other
are the
same, or Err(MixedRateDuration)
if they are not.
If the sample count of updated duration exceeds the maximum
capacity of a u64
then this method will panic with the error
message "overflow when add-assigning ConstantRateDurations"
.
Examples
use sampled_data_duration::*;
let mut a = ConstantRateDuration::new(48000, 48000);
let b = ConstantRateDuration::new(48000 * 2, 48000);
if let Ok(()) = a.try_add_assign(b) {
assert_eq!(a.as_secs(), 3);
} else {
assert!(false);
}
Errors
Will return a Err(MixedRateDuration)
if the provided
ConstantRateDurations
have incommensurate sampling rates.
sourcepub fn try_saturating_sub(
self,
other: ConstantRateDuration
) -> Result<ConstantRateDuration, Error>
pub fn try_saturating_sub(
self,
other: ConstantRateDuration
) -> Result<ConstantRateDuration, Error>
Computes self - other
returning Ok(ConstantRateDuration)
if the sampling rates of self
and other
are the same, or
Err(())
if they are not.
If the sample count of the new duration would be less than 0
then then returned ConstantRateDuraiton
will have 0 duration
— this is a what is meant by saturating..
Examples
use sampled_data_duration::*;
let a = ConstantRateDuration::new(48001, 48000);
let b = ConstantRateDuration::new(48000, 48000);
assert_eq!(a.try_saturating_sub(b), Ok(ConstantRateDuration::new(1, 48000)));
let c = ConstantRateDuration::new(48001, 48000);
let d = ConstantRateDuration::new(48000, 48000);
assert_eq!(d.try_saturating_sub(c), Ok(ConstantRateDuration::new(0, 48000)));
let e = ConstantRateDuration::new(96000, 96000);
let f = ConstantRateDuration::new(48000, 48000);
assert_eq!(e.try_saturating_sub(f), Err(Error::IncommensurateRates));
Errors
Will return an Error::IncommensurateRates
if the provided
ConstantRateDurations
have incommensurate sampling rates.
sourcepub fn try_saturating_sub_assign(
&mut self,
crd: ConstantRateDuration
) -> Result<(), Error>
pub fn try_saturating_sub_assign(
&mut self,
crd: ConstantRateDuration
) -> Result<(), Error>
Sub-assigns crd
from this ConstantRateDuration
returning
Ok(())
if the sampling rates of self
and other
are the
same, or Err(())
if they are not.
If the sample count of updated duration would be negative then it “saturates” and becomes zero.
Examples
use sampled_data_duration::*;
let mut a = ConstantRateDuration::new(10, 48000);
let b = ConstantRateDuration::new(2, 48000);
if let Ok(()) = a.try_saturating_sub_assign(b) {
assert_eq!(a.subsec_samples(), 8);
} else {
assert!(false);
}
let mut c = ConstantRateDuration::new(1, 48000);
let d = ConstantRateDuration::new(5, 48000);
if let Ok(()) = c.try_saturating_sub_assign(d) {
assert_eq!(c.subsec_samples(), 0);
} else {
assert!(false);
}
let mut e = ConstantRateDuration::new(96000, 96000);
let f = ConstantRateDuration::new(48000, 48000);
assert!(e.try_saturating_sub(f).is_err());
Errors
Will return an Error::IncommensurateRates
if the provided
ConstantRateDurations
have incommensurate sampling rates.
Trait Implementations
sourceimpl Add<ConstantRateDuration> for MixedRateDuration
impl Add<ConstantRateDuration> for MixedRateDuration
sourcefn add(self, crd: ConstantRateDuration) -> MixedRateDuration
fn add(self, crd: ConstantRateDuration) -> MixedRateDuration
Add a ConstantRateDuration
to this MixedRateDuration
returning a new MixedRateDuration
.
type Output = MixedRateDuration
type Output = MixedRateDuration
The resulting type after applying the +
operator.
sourceimpl AddAssign<ConstantRateDuration> for MixedRateDuration
impl AddAssign<ConstantRateDuration> for MixedRateDuration
sourcefn add_assign(&mut self, crd: ConstantRateDuration)
fn add_assign(&mut self, crd: ConstantRateDuration)
Add a ConstantRateDuration
to this MixedRateDuration
.
sourceimpl Clone for ConstantRateDuration
impl Clone for ConstantRateDuration
sourcefn clone(&self) -> ConstantRateDuration
fn clone(&self) -> ConstantRateDuration
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for ConstantRateDuration
impl Debug for ConstantRateDuration
sourceimpl Display for ConstantRateDuration
impl Display for ConstantRateDuration
sourceimpl Div<u64> for ConstantRateDuration
impl Div<u64> for ConstantRateDuration
sourcefn div(self, rhs: u64) -> Self
fn div(self, rhs: u64) -> Self
Divide a ConstantRateDuration
by a u64
returning a new
ConstantRateDuration
.
type Output = ConstantRateDuration
type Output = ConstantRateDuration
The resulting type after applying the /
operator.
sourceimpl DivAssign<u64> for ConstantRateDuration
impl DivAssign<u64> for ConstantRateDuration
sourcefn div_assign(&mut self, rhs: u64)
fn div_assign(&mut self, rhs: u64)
Divide-assign a ConstantRateDuration
by a u64
.
sourceimpl From<ConstantRateDuration> for MixedRateDuration
impl From<ConstantRateDuration> for MixedRateDuration
sourcefn from(crd: ConstantRateDuration) -> Self
fn from(crd: ConstantRateDuration) -> Self
Construct a MixedRateDuration
from a ConstantRateDuration
.
sourceimpl Mul<u64> for ConstantRateDuration
impl Mul<u64> for ConstantRateDuration
sourcefn mul(self, rhs: u64) -> Self
fn mul(self, rhs: u64) -> Self
Multiply a ConstantRateDuration
by a u64
returning a new
ConstantRateDuration
.
type Output = ConstantRateDuration
type Output = ConstantRateDuration
The resulting type after applying the *
operator.
sourceimpl MulAssign<u64> for ConstantRateDuration
impl MulAssign<u64> for ConstantRateDuration
sourcefn mul_assign(&mut self, rhs: u64)
fn mul_assign(&mut self, rhs: u64)
Multiply-assign a ConstantRateDuration
by a u64
.
sourceimpl PartialEq<ConstantRateDuration> for ConstantRateDuration
impl PartialEq<ConstantRateDuration> for ConstantRateDuration
sourcefn eq(&self, other: &ConstantRateDuration) -> bool
fn eq(&self, other: &ConstantRateDuration) -> bool
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
sourcefn ne(&self, other: &ConstantRateDuration) -> bool
fn ne(&self, other: &ConstantRateDuration) -> bool
This method tests for !=
.
sourceimpl Sub<ConstantRateDuration> for MixedRateDuration
impl Sub<ConstantRateDuration> for MixedRateDuration
sourcefn sub(self, crd: ConstantRateDuration) -> MixedRateDuration
fn sub(self, crd: ConstantRateDuration) -> MixedRateDuration
Perform a saturating subtraction of a ConstantRateDuration
from this MixedRateDuration
returning a new
MixedRateDuration
.
type Output = MixedRateDuration
type Output = MixedRateDuration
The resulting type after applying the -
operator.
sourceimpl SubAssign<ConstantRateDuration> for MixedRateDuration
impl SubAssign<ConstantRateDuration> for MixedRateDuration
sourcefn sub_assign(&mut self, crd: ConstantRateDuration)
fn sub_assign(&mut self, crd: ConstantRateDuration)
Perform a saturating subtraction of a ConstantRateDuration
from this MixedRateDuration
.
impl Copy for ConstantRateDuration
impl StructuralPartialEq for ConstantRateDuration
Auto Trait Implementations
impl RefUnwindSafe for ConstantRateDuration
impl Send for ConstantRateDuration
impl Sync for ConstantRateDuration
impl Unpin for ConstantRateDuration
impl UnwindSafe for ConstantRateDuration
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more