simplify OptU16 API
parent
422a7989f5
commit
6ee6140ba8
|
@ -49,7 +49,7 @@ macro_rules! _durandal_read_impl {
|
||||||
};
|
};
|
||||||
($e:ident $b:expr; $nam:ident OptU16 $n:expr) => {
|
($e:ident $b:expr; $nam:ident OptU16 $n:expr) => {
|
||||||
_durandal_read_impl!($e $b; $nam u16 $n);
|
_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) => {
|
($e:ident $b:expr; $nam:ident usize u16 $n:expr) => {
|
||||||
_durandal_read_impl!($e $b; $nam u16 $n);
|
_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
|
/// - `Unit`: same as `u16`, but the result is passed to
|
||||||
/// `fixed::Unit::from_bits`, resulting in a `fixed::Unit` object.
|
/// `fixed::Unit::from_bits`, resulting in a `fixed::Unit` object.
|
||||||
/// - `OptU16`: same as `u16`, but the result is passed to
|
/// - `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
|
/// - The name of a function, which is passed `&source[place]` as its only
|
||||||
/// argument. The function's result has the `?` operator applied to it.
|
/// argument. The function's result has the `?` operator applied to it.
|
||||||
/// - `opts` may be one of:
|
/// - `opts` may be one of:
|
||||||
|
@ -376,6 +376,46 @@ pub fn rd_ofstable<T, F>(b: &[u8],
|
||||||
Ok(v)
|
Ok(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<u16> for OptU16
|
||||||
|
{
|
||||||
|
#[inline]
|
||||||
|
fn from(n: u16) -> Self
|
||||||
|
{
|
||||||
|
if n == u16::max_value() {
|
||||||
|
Self(None)
|
||||||
|
} else {
|
||||||
|
Self(NonZeroU16::new(n + 1))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Into<u16> 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!(<OptU16 as Into<u16>>::into(OptU16::from(500u16)), 500u16);
|
||||||
|
/// assert_eq!(<OptU16 as Into<u16>>::into(OptU16::from(u16_max)), u16_max);
|
||||||
|
/// assert_eq!(<OptU16 as Into<u16>>::into(OptU16::from(0u16)), 0u16);
|
||||||
|
/// ```
|
||||||
|
#[inline]
|
||||||
|
fn into(self) -> u16
|
||||||
|
{
|
||||||
|
match self.0 {
|
||||||
|
None => u16::max_value(),
|
||||||
|
Some(n) => n.get() - 1,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl OptU16
|
impl OptU16
|
||||||
{
|
{
|
||||||
/// Creates an `OptU16` representing `None`.
|
/// Creates an `OptU16` representing `None`.
|
||||||
|
@ -385,41 +425,11 @@ impl OptU16
|
||||||
/// ```
|
/// ```
|
||||||
/// use maraiah::durandal::bin::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)}
|
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.
|
/// Returns the `Option` representation.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
|
@ -427,10 +437,11 @@ impl OptU16
|
||||||
/// ```
|
/// ```
|
||||||
/// use maraiah::durandal::bin::OptU16;
|
/// use maraiah::durandal::bin::OptU16;
|
||||||
///
|
///
|
||||||
/// assert_eq!(OptU16::from_repr(500u16).get(), Some(500u16));
|
/// assert_eq!(OptU16::from(500u16).get(), Some(500u16));
|
||||||
/// assert_eq!(OptU16::from_repr(u16::max_value()).get(), None);
|
/// assert_eq!(OptU16::from(u16::max_value()).get(), None);
|
||||||
/// assert_eq!(OptU16::from_repr(0u16).get(), Some(0u16));
|
/// assert_eq!(OptU16::from(0u16).get(), Some(0u16));
|
||||||
/// ```
|
/// ```
|
||||||
|
#[inline]
|
||||||
pub fn get(self) -> Option<u16>
|
pub fn get(self) -> Option<u16>
|
||||||
{
|
{
|
||||||
match self.0 {
|
match self.0 {
|
||||||
|
|
Loading…
Reference in New Issue