v4l/fraction.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
use crate::v4l_sys::*;
use std::fmt;
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
/// Fraction representation used for timing settings.
///
/// This is only a representation of the data returned by the backend, not a mathematical type. Two Fractions are equal **when fields are equal**, not when they are the equal rational number.
pub struct Fraction {
pub numerator: u32,
pub denominator: u32,
}
impl Fraction {
/// Returns a fraction representation
///
/// # Arguments
///
/// * `num` - Numerator
/// * `denom` - Denominator
///
/// # Example
///
/// ```
/// use v4l::fraction::Fraction;
/// let frac = Fraction::new(30, 1);
/// ```
pub fn new(num: u32, denom: u32) -> Self {
Fraction {
numerator: num,
denominator: denom,
}
}
}
impl fmt::Display for Fraction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}/{}", self.numerator, self.denominator)
}
}
impl From<v4l2_fract> for Fraction {
fn from(frac: v4l2_fract) -> Self {
Self {
numerator: frac.numerator,
denominator: frac.denominator,
}
}
}
impl From<Fraction> for v4l2_fract {
fn from(fraction: Fraction) -> Self {
Self {
numerator: fraction.numerator,
denominator: fraction.denominator,
}
}
}