1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-26 08:20:32 +00:00
hls_m3u8/src/types/signed_decimal_floating_point.rs

101 lines
2.9 KiB
Rust
Raw Normal View History

2019-09-22 18:33:40 +00:00
use core::ops::Deref;
use derive_more::{Display, FromStr};
2019-09-06 11:20:40 +00:00
/// Signed decimal floating-point number.
///
/// See: [4.2. Attribute Lists]
///
/// [4.2. Attribute Lists]: https://tools.ietf.org/html/rfc8216#section-4.2
2019-09-22 18:33:40 +00:00
#[derive(Default, Debug, Clone, Copy, PartialEq, PartialOrd, Display, FromStr)]
pub(crate) struct SignedDecimalFloatingPoint(f64);
2019-09-06 11:20:40 +00:00
impl SignedDecimalFloatingPoint {
2019-10-06 14:39:18 +00:00
/// Makes a new [`SignedDecimalFloatingPoint`] instance.
2019-09-06 11:20:40 +00:00
///
2019-09-22 18:33:40 +00:00
/// # Panics
2020-02-02 12:38:11 +00:00
///
2019-09-22 18:33:40 +00:00
/// The given value must be finite, otherwise this function will panic!
pub fn new(value: f64) -> Self {
2019-10-06 14:39:18 +00:00
if value.is_infinite() || value.is_nan() {
panic!("Floating point value must be finite and not NaN!");
2019-09-13 14:06:52 +00:00
}
2019-09-22 18:33:40 +00:00
Self(value)
2019-09-06 11:20:40 +00:00
}
2019-10-03 15:01:15 +00:00
pub(crate) const fn from_f64_unchecked(value: f64) -> Self { Self(value) }
2019-09-22 18:33:40 +00:00
2019-10-06 14:39:18 +00:00
/// Converts [`DecimalFloatingPoint`] to [`f64`].
2019-10-03 15:01:15 +00:00
pub const fn as_f64(self) -> f64 { self.0 }
2019-09-06 11:20:40 +00:00
}
2019-09-22 18:33:40 +00:00
impl Deref for SignedDecimalFloatingPoint {
type Target = f64;
2019-10-03 15:01:15 +00:00
fn deref(&self) -> &Self::Target { &self.0 }
2019-09-06 11:20:40 +00:00
}
2019-09-22 18:33:40 +00:00
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
2019-09-22 18:33:40 +00:00
macro_rules! test_from {
( $( $input:expr => $output:expr ),* ) => {
use ::core::convert::From;
#[test]
fn test_from() {
$(
assert_eq!(
$input,
$output,
);
)*
}
}
2019-09-06 11:20:40 +00:00
}
2019-09-22 18:33:40 +00:00
test_from![
2019-10-05 14:08:03 +00:00
SignedDecimalFloatingPoint::from(1_u8) => SignedDecimalFloatingPoint::new(1.0),
SignedDecimalFloatingPoint::from(1_i8) => SignedDecimalFloatingPoint::new(1.0),
SignedDecimalFloatingPoint::from(1_u16) => SignedDecimalFloatingPoint::new(1.0),
SignedDecimalFloatingPoint::from(1_i16) => SignedDecimalFloatingPoint::new(1.0),
SignedDecimalFloatingPoint::from(1_u32) => SignedDecimalFloatingPoint::new(1.0),
SignedDecimalFloatingPoint::from(1_i32) => SignedDecimalFloatingPoint::new(1.0),
SignedDecimalFloatingPoint::from(1.0_f32) => SignedDecimalFloatingPoint::new(1.0),
SignedDecimalFloatingPoint::from(1.0_f64) => SignedDecimalFloatingPoint::new(1.0)
2019-09-22 18:33:40 +00:00
];
#[test]
fn test_display() {
assert_eq!(
SignedDecimalFloatingPoint::new(1.0).to_string(),
2019-10-05 14:08:03 +00:00
1.0_f64.to_string()
2019-09-22 18:33:40 +00:00
);
}
#[test]
#[should_panic]
2019-10-03 15:01:15 +00:00
fn test_new_panic() { SignedDecimalFloatingPoint::new(::std::f64::INFINITY); }
2019-09-22 18:33:40 +00:00
#[test]
fn test_parser() {
assert_eq!(
SignedDecimalFloatingPoint::new(1.0),
"1.0".parse::<SignedDecimalFloatingPoint>().unwrap()
);
assert!("garbage".parse::<SignedDecimalFloatingPoint>().is_err());
}
#[test]
fn test_as_f64() {
assert_eq!(SignedDecimalFloatingPoint::new(1.0).as_f64(), 1.0);
}
2019-09-13 14:06:52 +00:00
2019-09-22 18:33:40 +00:00
#[test]
fn test_deref() {
assert_eq!(SignedDecimalFloatingPoint::from(0.1).floor(), 0.0);
2019-09-06 11:20:40 +00:00
}
}