From 6ee6140ba8f08cd174a43cc45ef8c0b4af8747c9 Mon Sep 17 00:00:00 2001 From: Marrub Date: Mon, 18 Mar 2019 12:07:44 -0400 Subject: [PATCH] simplify OptU16 API --- source/durandal/bin.rs | 85 ++++++++++++++++++++++++------------------ 1 file changed, 48 insertions(+), 37 deletions(-) diff --git a/source/durandal/bin.rs b/source/durandal/bin.rs index f4c8e60..0be9177 100644 --- a/source/durandal/bin.rs +++ b/source/durandal/bin.rs @@ -49,7 +49,7 @@ macro_rules! _durandal_read_impl { }; ($e:ident $b:expr; $nam:ident OptU16 $n:expr) => { _durandal_read_impl!($e $b; $nam u16 $n); - let $nam = OptU16::from_repr($nam); + let $nam = OptU16::from($nam); }; ($e:ident $b:expr; $nam:ident usize u16 $n:expr) => { _durandal_read_impl!($e $b; $nam u16 $n); @@ -114,7 +114,7 @@ macro_rules! _durandal_read_impl { /// - `Unit`: same as `u16`, but the result is passed to /// `fixed::Unit::from_bits`, resulting in a `fixed::Unit` object. /// - `OptU16`: same as `u16`, but the result is passed to -/// `OptU16::from_repr`, resulting in an `OptU16` object. +/// `OptU16::from`, resulting in an `OptU16` object. /// - The name of a function, which is passed `&source[place]` as its only /// argument. The function's result has the `?` operator applied to it. /// - `opts` may be one of: @@ -376,6 +376,46 @@ pub fn rd_ofstable(b: &[u8], Ok(v) } +impl From for OptU16 +{ + #[inline] + fn from(n: u16) -> Self + { + if n == u16::max_value() { + Self(None) + } else { + Self(NonZeroU16::new(n + 1)) + } + } +} + +impl Into for OptU16 +{ + /// Returns the `u16` representation. + /// + /// # Examples + /// + /// ``` + /// use maraiah::durandal::bin::OptU16; + /// + /// let u16_max = u16::max_value(); + /// + /// // These type annotations are necessary. + /// + /// assert_eq!(>::into(OptU16::from(500u16)), 500u16); + /// assert_eq!(>::into(OptU16::from(u16_max)), u16_max); + /// assert_eq!(>::into(OptU16::from(0u16)), 0u16); + /// ``` + #[inline] + fn into(self) -> u16 + { + match self.0 { + None => u16::max_value(), + Some(n) => n.get() - 1, + } + } +} + impl OptU16 { /// Creates an `OptU16` representing `None`. @@ -385,41 +425,11 @@ impl OptU16 /// ``` /// use maraiah::durandal::bin::OptU16; /// - /// assert_eq!(OptU16::none(), OptU16::from_repr(u16::max_value())); + /// assert_eq!(OptU16::none(), OptU16::from(u16::max_value())); /// ``` + #[inline] pub const fn none() -> Self {Self(None)} - /// Creates an `OptU16` from a `u16`. - pub fn from_repr(n: u16) -> Self - { - if n == u16::max_value() { - Self(None) - } else { - Self(NonZeroU16::new(n + 1)) - } - } - - /// Returns the `u16` representation. - /// - /// # Examples - /// - /// ``` - /// use maraiah::durandal::bin::OptU16; - /// - /// let u16max = u16::max_value(); - /// - /// assert_eq!(OptU16::from_repr(500u16).get_repr(), 500u16); - /// assert_eq!(OptU16::from_repr(u16max).get_repr(), u16max); - /// assert_eq!(OptU16::from_repr(0u16).get_repr(), 0u16); - /// ``` - pub fn get_repr(self) -> u16 - { - match self.0 { - None => u16::max_value(), - Some(n) => n.get() - 1, - } - } - /// Returns the `Option` representation. /// /// # Examples @@ -427,10 +437,11 @@ impl OptU16 /// ``` /// use maraiah::durandal::bin::OptU16; /// - /// assert_eq!(OptU16::from_repr(500u16).get(), Some(500u16)); - /// assert_eq!(OptU16::from_repr(u16::max_value()).get(), None); - /// assert_eq!(OptU16::from_repr(0u16).get(), Some(0u16)); + /// assert_eq!(OptU16::from(500u16).get(), Some(500u16)); + /// assert_eq!(OptU16::from(u16::max_value()).get(), None); + /// assert_eq!(OptU16::from(0u16).get(), Some(0u16)); /// ``` + #[inline] pub fn get(self) -> Option { match self.0 {