diff options
| author | Chris Denton <chris@chrisdenton.dev> | 2025-04-19 14:01:37 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-04-19 14:01:37 +0000 |
| commit | dff14f06341309b7edf3e760ab9762175db322f3 (patch) | |
| tree | b8ef039fbee61a070f3e07db7bb1cd4dcff63088 | |
| parent | 103be60afe79a547c56011ba216bcd9cd3e9e1d7 (diff) | |
| parent | 830bd8b6f4feb8665c8865beb20ffb424e8d8ee6 (diff) | |
Rollup merge of #139535 - ChrisDenton:default-ptr, r=tgross35
Implement `Default` for raw pointers ACP: https://github.com/rust-lang/libs-team/issues/571 This is instantly stable so we will need an FCP here. Closes https://github.com/rust-lang/rfcs/issues/2464
| -rw-r--r-- | library/core/src/ptr/const_ptr.rs | 8 | ||||
| -rw-r--r-- | library/core/src/ptr/mut_ptr.rs | 8 | ||||
| -rw-r--r-- | library/coretests/tests/ptr.rs | 17 |
3 files changed, 33 insertions, 0 deletions
diff --git a/library/core/src/ptr/const_ptr.rs b/library/core/src/ptr/const_ptr.rs index 0854e31c199..2d869958b85 100644 --- a/library/core/src/ptr/const_ptr.rs +++ b/library/core/src/ptr/const_ptr.rs @@ -1739,3 +1739,11 @@ impl<T: ?Sized> PartialOrd for *const T { *self >= *other } } + +#[stable(feature = "raw_ptr_default", since = "CURRENT_RUSTC_VERSION")] +impl<T: ?Sized + Thin> Default for *const T { + /// Returns the default value of [`null()`][crate::ptr::null]. + fn default() -> Self { + crate::ptr::null() + } +} diff --git a/library/core/src/ptr/mut_ptr.rs b/library/core/src/ptr/mut_ptr.rs index e29774963db..df49eedb350 100644 --- a/library/core/src/ptr/mut_ptr.rs +++ b/library/core/src/ptr/mut_ptr.rs @@ -2156,3 +2156,11 @@ impl<T: ?Sized> PartialOrd for *mut T { *self >= *other } } + +#[stable(feature = "raw_ptr_default", since = "CURRENT_RUSTC_VERSION")] +impl<T: ?Sized + Thin> Default for *mut T { + /// Returns the default value of [`null_mut()`][crate::ptr::null_mut]. + fn default() -> Self { + crate::ptr::null_mut() + } +} diff --git a/library/coretests/tests/ptr.rs b/library/coretests/tests/ptr.rs index cc5f7946863..7d6e4eac1e2 100644 --- a/library/coretests/tests/ptr.rs +++ b/library/coretests/tests/ptr.rs @@ -1020,3 +1020,20 @@ fn test_ptr_swap_nonoverlapping_is_untyped() { ptr_swap_nonoverlapping_is_untyped_inner(); const { ptr_swap_nonoverlapping_is_untyped_inner() }; } + +#[test] +fn test_ptr_default() { + #[derive(Default)] + struct PtrDefaultTest { + ptr: *const u64, + } + let default = PtrDefaultTest::default(); + assert!(default.ptr.is_null()); + + #[derive(Default)] + struct PtrMutDefaultTest { + ptr: *mut u64, + } + let default = PtrMutDefaultTest::default(); + assert!(default.ptr.is_null()); +} |
