diff options
| author | woppopo <woppopo@protonmail.com> | 2022-01-28 18:41:35 +0900 |
|---|---|---|
| committer | woppopo <woppopo@protonmail.com> | 2022-01-28 18:41:35 +0900 |
| commit | cdd0873db690cbcea1e5a011bbfc7fc94c5d5a6e (patch) | |
| tree | 3535d2bc20ea9f92c0eba1aef7228774e783584c | |
| parent | 5e97fc9aa265cb523a16cbe035c638eb35257cf5 (diff) | |
| download | rust-cdd0873db690cbcea1e5a011bbfc7fc94c5d5a6e.tar.gz rust-cdd0873db690cbcea1e5a011bbfc7fc94c5d5a6e.zip | |
Add a test case for using NonNull::new in const context
| -rw-r--r-- | library/core/tests/lib.rs | 2 | ||||
| -rw-r--r-- | library/core/tests/ptr.rs | 15 |
2 files changed, 17 insertions, 0 deletions
diff --git a/library/core/tests/lib.rs b/library/core/tests/lib.rs index 841c114063d..e5ae65f1ab5 100644 --- a/library/core/tests/lib.rs +++ b/library/core/tests/lib.rs @@ -16,7 +16,9 @@ #![feature(const_maybe_uninit_as_mut_ptr)] #![feature(const_maybe_uninit_assume_init)] #![feature(const_maybe_uninit_assume_init_read)] +#![feature(const_nonnull_new)] #![feature(const_num_from_num)] +#![feature(const_ptr_as_ref)] #![feature(const_ptr_read)] #![feature(const_ptr_write)] #![feature(const_ptr_offset)] diff --git a/library/core/tests/ptr.rs b/library/core/tests/ptr.rs index b9c0d75b702..bbfd2d64dda 100644 --- a/library/core/tests/ptr.rs +++ b/library/core/tests/ptr.rs @@ -275,6 +275,21 @@ fn test_unsized_nonnull() { } #[test] +fn test_const_nonnull_new() { + const { + assert!(NonNull::new(core::ptr::null_mut::<()>()).is_none()); + + let value = &mut 0u32; + let mut ptr = NonNull::new(value).unwrap(); + unsafe { *ptr.as_mut() = 42 }; + + let reference = unsafe { &*ptr.as_ref() }; + assert!(*reference == *value); + assert!(*reference == 42); + }; +} + +#[test] #[allow(warnings)] // Have a symbol for the test below. It doesn’t need to be an actual variadic function, match the // ABI, or even point to an actual executable code, because the function itself is never invoked. |
