Skip to content

Commit

Permalink
Merge pull request #107 from str4d/lexicon-integer-conversion
Browse files Browse the repository at this point in the history
Add direct conversions between the Lexicon integer types and primitives
  • Loading branch information
sugyan authored Feb 19, 2024
2 parents 0f514ee + aa18dcc commit 676e3b8
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions atrium-api/src/types/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ macro_rules! uint {
}
}

impl<const MAX: $primitive> From<$lim<MAX>> for $primitive {
fn from(value: $lim<MAX>) -> Self {
value.0
}
}

/// An unsigned integer with a minimum value of 1 and a maximum value of `MAX`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize)]
#[repr(transparent)]
Expand Down Expand Up @@ -76,6 +82,18 @@ macro_rules! uint {
}
}

impl<const MAX: $primitive> From<$lim_nz<MAX>> for $nz {
fn from(value: $lim_nz<MAX>) -> Self {
value.0
}
}

impl<const MAX: $primitive> From<$lim_nz<MAX>> for $primitive {
fn from(value: $lim_nz<MAX>) -> Self {
value.0.into()
}
}

/// An unsigned integer with a minimum value of `MIN` and a maximum value of `MAX`.
///
/// `MIN` must be non-zero.
Expand Down Expand Up @@ -119,6 +137,18 @@ macro_rules! uint {
Self::new(value).map_err(D::Error::custom)
}
}

impl<const MIN: $primitive, const MAX: $primitive> From<$bounded<MIN, MAX>> for $nz {
fn from(value: $bounded<MIN, MAX>) -> Self {
value.0
}
}

impl<const MIN: $primitive, const MAX: $primitive> From<$bounded<MIN, MAX>> for $primitive {
fn from(value: $bounded<MIN, MAX>) -> Self {
value.0.into()
}
}
};
}

Expand Down

0 comments on commit 676e3b8

Please sign in to comment.