-
Notifications
You must be signed in to change notification settings - Fork 13.4k
implement ptr::write without dedicated intrinsic #80290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -883,12 +883,19 @@ pub const unsafe fn read_unaligned<T>(src: *const T) -> T { | |
#[inline] | ||
#[stable(feature = "rust1", since = "1.0.0")] | ||
pub unsafe fn write<T>(dst: *mut T, src: T) { | ||
if cfg!(debug_assertions) && !is_aligned_and_not_null(dst) { | ||
// Not panicking to keep codegen impact smaller. | ||
abort(); | ||
// We are calling the intrinsics directly to avoid function calls in the generated code | ||
// as `intrinsics::copy_nonoverlapping` is a wrapper function. | ||
extern "rust-intrinsic" { | ||
fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize); | ||
} | ||
|
||
// SAFETY: the caller must guarantee that `dst` is valid for writes. | ||
// `dst` cannot overlap `src` because the caller has mutable access | ||
// to `dst` while `src` is owned by this function. | ||
unsafe { | ||
RalfJung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
copy_nonoverlapping(&src as *const T, dst, 1); | ||
intrinsics::forget(src); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the difference between There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, ok. Thanks There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, currently only We could also decide to make There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Am I right in assuming that we will want to make I believe borrowing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
On second thought, is that #80418? :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While yes, we want to make it But also, as you said, with #80418 this function should actually be fine. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Not what I meant to suggest :)
Awesome then :) |
||
} | ||
// SAFETY: the caller must uphold the safety contract for `move_val_init`. | ||
unsafe { intrinsics::move_val_init(&mut *dst, src) } | ||
} | ||
|
||
/// Overwrites a memory location with the given value without reading or | ||
|
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.