Skip to content

Commit

Permalink
Auto merge of #54240 - csmoe:nonzero_from, r=alexcrichton
Browse files Browse the repository at this point in the history
Impl From<NonZero<T>> for T

Closes #54171

r? @SimonSapin
  • Loading branch information
bors committed Sep 29, 2018
2 parents eb50e75 + 95c1d81 commit bb0896a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 7 additions & 0 deletions src/libcore/tests/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit bb0896a

Please sign in to comment.