Skip to content

Commit

Permalink
Merge pull request #19 from palantir/safelong-from
Browse files Browse the repository at this point in the history
Add infallible conversions to and from SafeLong
  • Loading branch information
sfackler authored Feb 11, 2019
2 parents 1adefa7 + 23f409c commit 7878de8
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions conjure-object/src/safe_long.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,36 @@ impl<'de> de::Deserialize<'de> for SafeLong {
}
}

macro_rules! impl_from {
($($t:ty),*) => {
$(
impl From<$t> for SafeLong {
#[inline]
fn from(n: $t) -> SafeLong {
SafeLong(i64::from(n))
}
}
)*
}
}

impl_from!(u8, i8, u16, i16, u32, i32);

macro_rules! impl_into {
($($t:ty),*) => {
$(
impl From<SafeLong> for $t {
#[inline]
fn from(n: SafeLong) -> $t {
n.0.into()
}
}
)*
}
}

impl_into!(i64, i128);

/// The error returned from constructing an out-of bounds `SafeLong`.
#[derive(Debug, Clone)]
pub struct BoundsError(());
Expand Down

0 comments on commit 7878de8

Please sign in to comment.