From d5c1d15d31dc6164ac67e71526fe15f23a8014e7 Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Sat, 15 Sep 2018 09:34:36 +0800 Subject: [PATCH 1/4] implement From> for Int --- src/libcore/num/mod.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 12da0455cc56b..82a30864fd250 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -93,6 +93,13 @@ assert_eq!(size_of::>(), size_of::<", st } + #[stable(feature = "nonzero", since = "1.28.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 } From f451580a91953391b60c408adc9be384690d3e91 Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Sat, 15 Sep 2018 09:35:01 +0800 Subject: [PATCH 2/4] add run-pass test for From --- src/test/run-pass/nonzero.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/test/run-pass/nonzero.rs diff --git a/src/test/run-pass/nonzero.rs b/src/test/run-pass/nonzero.rs new file mode 100644 index 0000000000000..0f2b1dcc8d3a5 --- /dev/null +++ b/src/test/run-pass/nonzero.rs @@ -0,0 +1,16 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn main() { + use std::num::NonZeroU32; + let nz = NonZeroU32::new(5).unwrap(); + let num: u32 = nz.into(); +} + From ac61a9ce21546414547025c8bb49a85f1cf821e8 Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Mon, 17 Sep 2018 16:34:24 +0800 Subject: [PATCH 3/4] introduce from_nonzero feature --- src/libcore/num/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 82a30864fd250..c4b59738478dc 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -93,7 +93,7 @@ assert_eq!(size_of::>(), size_of::<", st } - #[stable(feature = "nonzero", since = "1.28.0")] + #[stable(feature = "from_nonzero", since = "1.31.0")] impl From<$Ty> for $Int { fn from(nonzero: $Ty) -> Self { nonzero.0 .0 From 95c1d817ae6ec50d3c636c54e33b4d51cab57148 Mon Sep 17 00:00:00 2001 From: csmoe <35686186+csmoe@users.noreply.github.com> Date: Mon, 17 Sep 2018 16:35:08 +0800 Subject: [PATCH 4/4] move from_nonzero test from run-pass to libcore --- src/libcore/tests/nonzero.rs | 7 +++++++ src/test/run-pass/nonzero.rs | 16 ---------------- 2 files changed, 7 insertions(+), 16 deletions(-) delete mode 100644 src/test/run-pass/nonzero.rs diff --git a/src/libcore/tests/nonzero.rs b/src/libcore/tests/nonzero.rs index 8d39298bac3d1..bbb1ef76bccec 100644 --- a/src/libcore/tests/nonzero.rs +++ b/src/libcore/tests/nonzero.rs @@ -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); +} diff --git a/src/test/run-pass/nonzero.rs b/src/test/run-pass/nonzero.rs deleted file mode 100644 index 0f2b1dcc8d3a5..0000000000000 --- a/src/test/run-pass/nonzero.rs +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright 2018 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -fn main() { - use std::num::NonZeroU32; - let nz = NonZeroU32::new(5).unwrap(); - let num: u32 = nz.into(); -} -