Fix the conversion functions, make sure we can do usize, too.

This commit is contained in:
2018-04-13 10:57:22 -04:00
parent d98baa1381
commit 017392ff6c
2 changed files with 32 additions and 12 deletions

View File

@@ -35,32 +35,50 @@ macro_rules! define_signed_from
macro_rules! define_into macro_rules! define_into
{ {
($type: ident, $base: ident) => { ($type: ident, $base: ident) => {
impl Into<$base> for $type { impl<'a> From<&'a $type> for $base {
fn into(self) -> $base { fn from(x: &$type) -> $base {
if self.contents.is_empty() { if x.contents.is_empty() {
0 0
} else { } else {
self.contents[0] as $base x.contents[0] as $base
} }
} }
} }
impl From<$type> for $base {
fn from(x: $type) -> $base {
$base::from(&x)
}
}
} }
} }
macro_rules! define_signed_into macro_rules! define_signed_into
{ {
($type: ident, $base: ident, $uns: ident) => { ($type: ident, $base: ident, $uns: ident) => {
impl Into<$uns> for $type { impl<'a> From<&'a $type> for $uns {
fn into(self) -> $uns { fn from(x: &$type) -> $uns {
let res: $uns = self.value.into(); let res: $uns = $uns::from(&x.value);
if self.negative { 0-res } else { res } if x.negative { 0-res } else { res }
} }
} }
impl Into<$base> for $type { impl<'a> From<&'a $type> for $base {
fn into(self) -> $base { fn from(x: &$type) -> $base {
let res: $uns = self.value.into(); let res: $uns = $uns::from(&x.value);
if self.negative { (0-res) as $base } else { res as $base } if x.negative { (0-res) as $base } else { res as $base }
}
}
impl From<$type> for $uns {
fn from(x: $type) -> $uns {
$uns::from(&x)
}
}
impl From<$type> for $base {
fn from(x: $type) -> $base {
$base::from(&x)
} }
} }
} }

View File

@@ -418,10 +418,12 @@ define_from!(UCN, u8);
define_from!(UCN, u16); define_from!(UCN, u16);
define_from!(UCN, u32); define_from!(UCN, u32);
define_from!(UCN, u64); define_from!(UCN, u64);
define_from!(UCN, usize);
define_into!(UCN, u8); define_into!(UCN, u8);
define_into!(UCN, u16); define_into!(UCN, u16);
define_into!(UCN, u32); define_into!(UCN, u32);
define_into!(UCN, u64); define_into!(UCN, u64);
define_into!(UCN, usize);
impl From<BigUint> for UCN { impl From<BigUint> for UCN {
fn from(mut x: BigUint) -> UCN { fn from(mut x: BigUint) -> UCN {