diff options
| author | Mark Simulacrum <mark.simulacrum@gmail.com> | 2017-07-26 06:15:01 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-07-26 06:15:01 -0600 |
| commit | b5b7266b786cb953581ba588d400bddcdae4a852 (patch) | |
| tree | c957402d4bf9f3f60cc718e86bf45ebba616bf4b /src/test | |
| parent | bad58f27916e7e233cc2916dcc9167708077e792 (diff) | |
| parent | 0d1864b8cf9585e6133aa3da2b06b29cbfb791bd (diff) | |
Rollup merge of #42959 - SimonSapin:nonzero-checked, r=sfackler
Make the "main" constructors of NonZero/Shared/Unique return Option Per discussion in https://github.com/rust-lang/rust/issues/27730#issuecomment-303939441. This is a breaking change to unstable APIs. The old behavior is still available under the name `new_unchecked`. Note that only that one can be `const fn`, since `if` is currently not allowed in constant contexts. In the case of `NonZero` this requires adding a new `is_zero` method to the `Zeroable` trait. I mildly dislike this, but it’s not much worse than having a `Zeroable` trait in the first place. `Zeroable` and `NonZero` are both unstable, this can be reworked later.
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/run-pass/issue-23433.rs | 2 | ||||
| -rw-r--r-- | src/test/ui/print_type_sizes/nullable.rs | 16 |
2 files changed, 12 insertions, 6 deletions
diff --git a/src/test/run-pass/issue-23433.rs b/src/test/run-pass/issue-23433.rs index 82f80586b9f..aa13d6fad47 100644 --- a/src/test/run-pass/issue-23433.rs +++ b/src/test/run-pass/issue-23433.rs @@ -16,7 +16,7 @@ use std::ptr::Unique; fn main() { let mut a = [0u8; 5]; - let b: Option<Unique<[u8]>> = unsafe { Some(Unique::new(&mut a)) }; + let b: Option<Unique<[u8]>> = Some(Unique::from(&mut a)); match b { Some(_) => println!("Got `Some`"), None => panic!("Unexpected `None`"), diff --git a/src/test/ui/print_type_sizes/nullable.rs b/src/test/ui/print_type_sizes/nullable.rs index f7fdcac81da..5052c59a39d 100644 --- a/src/test/ui/print_type_sizes/nullable.rs +++ b/src/test/ui/print_type_sizes/nullable.rs @@ -42,7 +42,7 @@ impl Default for EmbeddedDiscr { } #[derive(Default)] -pub struct IndirectNonZero<T: Zeroable> { +pub struct IndirectNonZero<T: Zeroable + One> { pre: u8, nested: NestedNonZero<T>, post: u16, @@ -54,14 +54,20 @@ pub struct NestedNonZero<T: Zeroable> { post: u16, } -impl<T: Zeroable+Default> Default for NestedNonZero<T> { +impl<T: Zeroable+One> Default for NestedNonZero<T> { fn default() -> Self { - unsafe { - NestedNonZero { pre: 0, val: NonZero::new(Default::default()), post: 0 } - } + NestedNonZero { pre: 0, val: NonZero::new(T::one()).unwrap(), post: 0 } } } +pub trait One { + fn one() -> Self; +} + +impl One for u32 { + fn one() -> Self { 1 } +} + pub fn main() { let _x: MyOption<NonZero<u32>> = Default::default(); let _y: EmbeddedDiscr = Default::default(); |
