Skip to content

Commit d196240

Browse files
committed
Remove option_payload_ptr; redundant to offset_of
1 parent 01a4518 commit d196240

File tree

5 files changed

+14
-10
lines changed

5 files changed

+14
-10
lines changed

core/src/intrinsics.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2487,12 +2487,6 @@ extern "rust-intrinsic" {
24872487
where
24882488
G: FnOnce<ARG, Output = RET>,
24892489
F: FnOnce<ARG, Output = RET>;
2490-
2491-
/// This method creates a pointer to any `Some` value. If the argument is
2492-
/// `None`, an invalid within-bounds pointer (that is still acceptable for
2493-
/// constructing an empty slice) is returned.
2494-
#[rustc_nounwind]
2495-
pub fn option_payload_ptr<T>(arg: *const Option<T>) -> *const T;
24962490
}
24972491

24982492
// Some functions are defined here because they accidentally got made

core/src/iter/adapters/filter_map.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ where
9797
// SAFETY: Loop conditions ensure the index is in bounds.
9898

9999
unsafe {
100-
let opt_payload_at = core::intrinsics::option_payload_ptr(&val);
100+
let opt_payload_at =
101+
(&val as *const Option<B>).byte_add(core::mem::offset_of!(Option<B>, Some.0));
101102
let dst = guard.array.as_mut_ptr().add(idx);
102103
crate::ptr::copy_nonoverlapping(opt_payload_at.cast(), dst, 1);
103104
crate::mem::forget(val);

core/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@
178178
#![feature(is_ascii_octdigit)]
179179
#![feature(isqrt)]
180180
#![feature(maybe_uninit_uninit_array)]
181+
#![feature(offset_of)]
182+
#![feature(offset_of_enum)]
181183
#![feature(ptr_alignment_type)]
182184
#![feature(ptr_metadata)]
183185
#![feature(set_ptr_value)]

core/src/option.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ impl<T> Option<T> {
779779
// `None` case it's just padding).
780780
unsafe {
781781
slice::from_raw_parts(
782-
crate::intrinsics::option_payload_ptr(crate::ptr::from_ref(self)),
782+
(self as *const Self).byte_add(core::mem::offset_of!(Self, Some.0)).cast(),
783783
usize::from(self.is_some()),
784784
)
785785
}
@@ -835,8 +835,7 @@ impl<T> Option<T> {
835835
// the `None` case it's just padding).
836836
unsafe {
837837
slice::from_raw_parts_mut(
838-
crate::intrinsics::option_payload_ptr(crate::ptr::from_mut(self).cast_const())
839-
.cast_mut(),
838+
(self as *mut Self).byte_add(core::mem::offset_of!(Self, Some.0)).cast(),
840839
usize::from(self.is_some()),
841840
)
842841
}

core/tests/option.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,3 +568,11 @@ fn zip_unzip_roundtrip() {
568568
let a = z.unzip();
569569
assert_eq!(a, (x, y));
570570
}
571+
572+
#[test]
573+
fn as_slice() {
574+
assert_eq!(Some(42).as_slice(), &[42]);
575+
assert_eq!(Some(43).as_mut_slice(), &[43]);
576+
assert_eq!(None::<i32>.as_slice(), &[]);
577+
assert_eq!(None::<i32>.as_mut_slice(), &[]);
578+
}

0 commit comments

Comments
 (0)