Skip to content
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

Simplify pointer writes in generated code #3089

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions crates/libs/bindgen/src/rust/com_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ pub fn gen_upcall(writer: &Writer, sig: &metadata::Signature, inner: TokenStream
quote! {
match #inner(this, #(#invoke_args,)*) {
Ok(ok__) => {
// use `core::ptr::write` since the result could be uninitialized
core::ptr::write(#result, core::mem::transmute(ok__));
// use `ptr::write` since the result could be uninitialized
#result.write(core::mem::transmute(ok__));
windows_core::HRESULT(0)
}
Err(err) => err.into()
Expand Down
18 changes: 9 additions & 9 deletions crates/libs/bindgen/src/rust/winrt_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,18 +263,18 @@ pub fn gen_upcall(
quote! {
let ok__ = #inner(#this #(#invoke_args,)*);
let (ok_data__, ok_data_len__) = ok__.into_abi();
core::ptr::write(result__, ok_data__);
core::ptr::write(result_size__, ok_data_len__);
result__.write(ok_data__);
result_size__.write(ok_data_len__);
windows_core::HRESULT(0)
}
} else {
quote! {
match #inner(#this #(#invoke_args,)*) {
Ok(ok__) => {
let (ok_data__, ok_data_len__) = ok__.into_abi();
// use `core::ptr::write` since `result` could be uninitialized
core::ptr::write(result__, ok_data__);
core::ptr::write(result_size__, ok_data_len__);
// use `ptr::write` since `result` could be uninitialized
result__.write(ok_data__);
result_size__.write(ok_data_len__);
windows_core::HRESULT(0)
}
Err(err) => err.into()
Expand All @@ -292,17 +292,17 @@ pub fn gen_upcall(
if noexcept {
quote! {
let ok__ = #inner(#this #(#invoke_args,)*);
// use `core::ptr::write` since `result` could be uninitialized
core::ptr::write(result__, core::mem::transmute_copy(&ok__));
// use `ptr::write` since `result` could be uninitialized
result__.write(core::mem::transmute_copy(&ok__));
#forget
windows_core::HRESULT(0)
}
} else {
quote! {
match #inner(#this #(#invoke_args,)*) {
Ok(ok__) => {
// use `core::ptr::write` since `result` could be uninitialized
core::ptr::write(result__, core::mem::transmute_copy(&ok__));
// use `ptr::write` since `result` could be uninitialized
result__.write(core::mem::transmute_copy(&ok__));
#forget
windows_core::HRESULT(0)
}
Expand Down
6 changes: 3 additions & 3 deletions crates/libs/core/src/strings/hstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ impl HSTRING {
for (index, wide) in iter.enumerate() {
debug_assert!(index < len);

core::ptr::write((*ptr).data.add(index), wide);
(*ptr).data.add(index).write(wide);
(*ptr).len = index as u32 + 1;
}

// Write a 0 byte to the end of the buffer.
core::ptr::write((*ptr).data.offset((*ptr).len as isize), 0);
(*ptr).data.offset((*ptr).len as isize).write(0);
Ok(Self(core::ptr::NonNull::new(ptr)))
}

Expand Down Expand Up @@ -430,7 +430,7 @@ impl Header {

let header = imp::heap_alloc(alloc_size)? as *mut Header;

// SAFETY: uses `core::ptr::write` (since `header` is unintialized). `Header` is safe to be all zeros.
// SAFETY: uses `ptr::write` (since `header` is unintialized). `Header` is safe to be all zeros.
unsafe {
header.write(core::mem::MaybeUninit::<Header>::zeroed().assume_init());
(*header).len = len;
Expand Down
14 changes: 7 additions & 7 deletions crates/libs/windows/src/Windows/AI/MachineLearning/impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl ILearningModelFeatureDescriptor_Vtbl {
let this = (*this).get_impl();
match ILearningModelFeatureDescriptor_Impl::Name(this) {
Ok(ok__) => {
core::ptr::write(result__, core::mem::transmute_copy(&ok__));
result__.write(core::mem::transmute_copy(&ok__));
core::mem::forget(ok__);
windows_core::HRESULT(0)
}
Expand All @@ -26,7 +26,7 @@ impl ILearningModelFeatureDescriptor_Vtbl {
let this = (*this).get_impl();
match ILearningModelFeatureDescriptor_Impl::Description(this) {
Ok(ok__) => {
core::ptr::write(result__, core::mem::transmute_copy(&ok__));
result__.write(core::mem::transmute_copy(&ok__));
core::mem::forget(ok__);
windows_core::HRESULT(0)
}
Expand All @@ -38,7 +38,7 @@ impl ILearningModelFeatureDescriptor_Vtbl {
let this = (*this).get_impl();
match ILearningModelFeatureDescriptor_Impl::Kind(this) {
Ok(ok__) => {
core::ptr::write(result__, core::mem::transmute_copy(&ok__));
result__.write(core::mem::transmute_copy(&ok__));
windows_core::HRESULT(0)
}
Err(err) => err.into(),
Expand All @@ -49,7 +49,7 @@ impl ILearningModelFeatureDescriptor_Vtbl {
let this = (*this).get_impl();
match ILearningModelFeatureDescriptor_Impl::IsRequired(this) {
Ok(ok__) => {
core::ptr::write(result__, core::mem::transmute_copy(&ok__));
result__.write(core::mem::transmute_copy(&ok__));
windows_core::HRESULT(0)
}
Err(err) => err.into(),
Expand Down Expand Up @@ -80,7 +80,7 @@ impl ILearningModelFeatureValue_Vtbl {
let this = (*this).get_impl();
match ILearningModelFeatureValue_Impl::Kind(this) {
Ok(ok__) => {
core::ptr::write(result__, core::mem::transmute_copy(&ok__));
result__.write(core::mem::transmute_copy(&ok__));
windows_core::HRESULT(0)
}
Err(err) => err.into(),
Expand Down Expand Up @@ -121,7 +121,7 @@ impl ITensor_Vtbl {
let this = (*this).get_impl();
match ITensor_Impl::TensorKind(this) {
Ok(ok__) => {
core::ptr::write(result__, core::mem::transmute_copy(&ok__));
result__.write(core::mem::transmute_copy(&ok__));
windows_core::HRESULT(0)
}
Err(err) => err.into(),
Expand All @@ -132,7 +132,7 @@ impl ITensor_Vtbl {
let this = (*this).get_impl();
match ITensor_Impl::Shape(this) {
Ok(ok__) => {
core::ptr::write(result__, core::mem::transmute_copy(&ok__));
result__.write(core::mem::transmute_copy(&ok__));
core::mem::forget(ok__);
windows_core::HRESULT(0)
}
Expand Down
Loading