util/rational: implement Debug and Display

This commit is contained in:
meh 2015-05-30 17:01:38 +02:00
parent dae7e523f6
commit 46a5f863c8

View File

@ -1,10 +1,11 @@
use std::cmp::Ordering; use std::cmp::Ordering;
use std::ops::{Add, Sub, Mul, Div}; use std::ops::{Add, Sub, Mul, Div};
use std::fmt;
use libc::{c_int, int64_t}; use libc::{c_int, int64_t};
use ffi::*; use ffi::*;
#[derive(Eq, PartialEq, Copy, Clone, Debug)] #[derive(Eq, PartialEq, Copy, Clone)]
pub struct Rational(pub AVRational); pub struct Rational(pub AVRational);
impl Rational { impl Rational {
@ -40,6 +41,18 @@ impl Rational {
} }
} }
impl fmt::Display for Rational {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
f.write_str(&format!("{}/{}", self.numerator(), self.denominator()))
}
}
impl fmt::Debug for Rational {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
f.write_str(&format!("Rational({}/{})", self.numerator(), self.denominator()))
}
}
impl From<f64> for Rational { impl From<f64> for Rational {
fn from(value: f64) -> Rational { fn from(value: f64) -> Rational {
unsafe { unsafe {