1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-11-25 16:41:00 +00:00

improve (U)Float

This commit is contained in:
Luro02 2020-02-24 12:19:37 +01:00
parent 88a5fa4460
commit dae826b4e5
No known key found for this signature in database
GPG key ID: B66FD4F74501A9CF
2 changed files with 25 additions and 14 deletions

View file

@ -2,17 +2,17 @@ use core::cmp::Ordering;
use core::convert::TryFrom; use core::convert::TryFrom;
use core::str::FromStr; use core::str::FromStr;
use derive_more::{Deref, Display}; use derive_more::{AsRef, Deref, Display};
use crate::Error; use crate::Error;
/// This is a wrapper type around an [`f32`] that can not be constructed /// A wrapper type around an [`f32`] that can not be constructed
/// with [`NaN`], [`INFINITY`] or [`NEG_INFINITY`]. /// with [`NaN`], [`INFINITY`] or [`NEG_INFINITY`].
/// ///
/// [`NaN`]: core::f32::NAN /// [`NaN`]: core::f32::NAN
/// [`INFINITY`]: core::f32::INFINITY /// [`INFINITY`]: core::f32::INFINITY
/// [`NEG_INFINITY`]: core::f32::NEG_INFINITY /// [`NEG_INFINITY`]: core::f32::NEG_INFINITY
#[derive(Deref, Default, Debug, Copy, Clone, Display, PartialOrd)] #[derive(AsRef, Deref, Default, Debug, Copy, Clone, Display, PartialOrd)]
pub struct Float(f32); pub struct Float(f32);
impl Float { impl Float {
@ -25,16 +25,15 @@ impl Float {
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// use hls_m3u8::types::Float; /// # use hls_m3u8::types::Float;
///
/// let float = Float::new(1.0); /// let float = Float::new(1.0);
/// ``` /// ```
/// ///
/// This would panic: /// This would panic:
/// ///
/// ```should_panic /// ```should_panic
/// # use hls_m3u8::types::Float;
/// use core::f32::NAN; /// use core::f32::NAN;
/// use hls_m3u8::types::Float;
/// ///
/// let float = Float::new(NAN); /// let float = Float::new(NAN);
/// ``` /// ```
@ -53,6 +52,13 @@ impl Float {
} }
/// Returns the underlying [`f32`]. /// Returns the underlying [`f32`].
///
/// # Example
///
/// ```
/// # use hls_m3u8::types::Float;
/// assert_eq!(Float::new(1.1_f32).as_f32(), 1.1_f32);
/// ```
pub const fn as_f32(self) -> f32 { self.0 } pub const fn as_f32(self) -> f32 { self.0 }
} }

View file

@ -2,18 +2,18 @@ use core::cmp::Ordering;
use core::convert::TryFrom; use core::convert::TryFrom;
use core::str::FromStr; use core::str::FromStr;
use derive_more::{Deref, Display}; use derive_more::{AsRef, Deref, Display};
use crate::Error; use crate::Error;
/// This is a wrapper type around an [`f32`] that can not be constructed /// A wrapper type around an [`f32`], that can not be constructed
/// with a negative float (ex. `-1.1`), [`NaN`], [`INFINITY`] or /// with a negative float (e.g. `-1.1`), [`NaN`], [`INFINITY`] or
/// [`NEG_INFINITY`]. /// [`NEG_INFINITY`].
/// ///
/// [`NaN`]: core::f32::NAN /// [`NaN`]: core::f32::NAN
/// [`INFINITY`]: core::f32::INFINITY /// [`INFINITY`]: core::f32::INFINITY
/// [`NEG_INFINITY`]: core::f32::NEG_INFINITY /// [`NEG_INFINITY`]: core::f32::NEG_INFINITY
#[derive(Deref, Default, Debug, Copy, Clone, PartialOrd, Display)] #[derive(AsRef, Deref, Default, Debug, Copy, Clone, PartialOrd, Display)]
pub struct UFloat(f32); pub struct UFloat(f32);
impl UFloat { impl UFloat {
@ -26,16 +26,14 @@ impl UFloat {
/// # Examples /// # Examples
/// ///
/// ``` /// ```
/// use hls_m3u8::types::UFloat; /// # use hls_m3u8::types::UFloat;
///
/// let float = UFloat::new(1.0); /// let float = UFloat::new(1.0);
/// ``` /// ```
/// ///
/// This would panic: /// This would panic:
/// ///
/// ```should_panic /// ```should_panic
/// use hls_m3u8::types::UFloat; /// # use hls_m3u8::types::UFloat;
///
/// let float = UFloat::new(-1.0); /// let float = UFloat::new(-1.0);
/// ``` /// ```
/// ///
@ -57,6 +55,13 @@ impl UFloat {
} }
/// Returns the underlying [`f32`]. /// Returns the underlying [`f32`].
///
/// # Example
///
/// ```
/// # use hls_m3u8::types::UFloat;
/// assert_eq!(UFloat::new(1.1_f32).as_f32(), 1.1_f32);
/// ```
pub const fn as_f32(self) -> f32 { self.0 } pub const fn as_f32(self) -> f32 { self.0 }
} }