Auto merge of #54240 - csmoe:nonzero_from, r=alexcrichton

Impl From<NonZero<T>> for T

Closes https://github.com/rust-lang/rust/issues/54171

r? @SimonSapin
This commit is contained in:
bors 2018-09-29 19:38:12 +00:00
commit bb0896af11
2 changed files with 14 additions and 0 deletions

View file

@ -93,6 +93,13 @@ assert_eq!(size_of::<Option<std::num::", stringify!($Ty), ">>(), size_of::<", st
}
#[stable(feature = "from_nonzero", since = "1.31.0")]
impl From<$Ty> for $Int {
fn from(nonzero: $Ty) -> Self {
nonzero.0 .0
}
}
impl_nonzero_fmt! {
(Debug, Display, Binary, Octal, LowerHex, UpperHex) for $Ty
}

View file

@ -121,3 +121,10 @@ fn test_match_nonzero_const_pattern() {
_ => panic!("Expected the const item as a pattern to match.")
}
}
#[test]
fn test_from_nonzero() {
let nz = NonZeroU32::new(1).unwrap();
let num: u32 = nz.into();
assert_eq!(num, 1u32);
}