Skip to content

Commit dff14f0

Browse files
authored
Rollup merge of rust-lang#139535 - ChrisDenton:default-ptr, r=tgross35
Implement `Default` for raw pointers ACP: rust-lang/libs-team#571 This is instantly stable so we will need an FCP here. Closes rust-lang/rfcs#2464
2 parents 103be60 + 830bd8b commit dff14f0

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

Diff for: library/core/src/ptr/const_ptr.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1739,3 +1739,11 @@ impl<T: ?Sized> PartialOrd for *const T {
17391739
*self >= *other
17401740
}
17411741
}
1742+
1743+
#[stable(feature = "raw_ptr_default", since = "CURRENT_RUSTC_VERSION")]
1744+
impl<T: ?Sized + Thin> Default for *const T {
1745+
/// Returns the default value of [`null()`][crate::ptr::null].
1746+
fn default() -> Self {
1747+
crate::ptr::null()
1748+
}
1749+
}

Diff for: library/core/src/ptr/mut_ptr.rs

+8
Original file line numberDiff line numberDiff line change
@@ -2156,3 +2156,11 @@ impl<T: ?Sized> PartialOrd for *mut T {
21562156
*self >= *other
21572157
}
21582158
}
2159+
2160+
#[stable(feature = "raw_ptr_default", since = "CURRENT_RUSTC_VERSION")]
2161+
impl<T: ?Sized + Thin> Default for *mut T {
2162+
/// Returns the default value of [`null_mut()`][crate::ptr::null_mut].
2163+
fn default() -> Self {
2164+
crate::ptr::null_mut()
2165+
}
2166+
}

Diff for: library/coretests/tests/ptr.rs

+17
Original file line numberDiff line numberDiff line change
@@ -1020,3 +1020,20 @@ fn test_ptr_swap_nonoverlapping_is_untyped() {
10201020
ptr_swap_nonoverlapping_is_untyped_inner();
10211021
const { ptr_swap_nonoverlapping_is_untyped_inner() };
10221022
}
1023+
1024+
#[test]
1025+
fn test_ptr_default() {
1026+
#[derive(Default)]
1027+
struct PtrDefaultTest {
1028+
ptr: *const u64,
1029+
}
1030+
let default = PtrDefaultTest::default();
1031+
assert!(default.ptr.is_null());
1032+
1033+
#[derive(Default)]
1034+
struct PtrMutDefaultTest {
1035+
ptr: *mut u64,
1036+
}
1037+
let default = PtrMutDefaultTest::default();
1038+
assert!(default.ptr.is_null());
1039+
}

0 commit comments

Comments
 (0)