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

use from_{ref,mut} to eliminate casts to pointers #272

Merged
merged 2 commits into from
Feb 22, 2024

Conversation

cagatay-y
Copy link
Contributor

As it stands, the PR requires ptr_from_ref and strict_provenance to land in stable. If the ptr_from_ref lands first, the branch can be updated to make use of the polyfill for the pointer::addr method in the sptr crate, as instructed in its documentation.

@mkroening
Copy link
Member

@cagatay-y, ptr_from_ref will be stable with tomorrow's 1.76.0 release.

@mkroening mkroening self-requested a review February 20, 2024 10:20
@mkroening mkroening self-assigned this Feb 20, 2024
Copy link
Member

@mkroening mkroening left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ptr_from_ref is now stable. :)

@cagatay-y cagatay-y marked this pull request as ready for review February 21, 2024 11:03
Copy link
Member

@mkroening mkroening left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's replace all instances of ptr::from_ref(unsafe {&foo}) with ptr::addr_of!(foo).

Copy link
Member

@mkroening mkroening left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for replacing ptr::from_ref(unsafe {&mut foo}) with ptr::addr_of_mut!(foo).

@cagatay-y
Copy link
Contributor Author

Do you mean the ones inside a from_raw_parts_mut? We have to have an unsafe block for from_raw_parts_mut in that case. Of course, I can convert them too, I am asking to make sure I am not missing something else.

@mkroening
Copy link
Member

Do you mean the ones inside a from_raw_parts_mut? We have to have an unsafe block for from_raw_parts_mut in that case. Of course, I can convert them too, I am asking to make sure I am not missing something else.

Yeah. While the amount of unsafe blocks stays the same in those cases, the amount of unsafe operations would be less. And it would be more consistent with the rest of the code.

Copy link
Member

@mkroening mkroening left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for not catching this earlier, but can we also enable the corresponding lint?
https://rust-lang.github.io/rust-clippy/master/index.html#ref_as_ptr

@cagatay-y
Copy link
Contributor Author

I can't get the lint to work on my machine. It complains that no such lint exists. I am version on 0.1.77. Does it work for you?

@mkroening
Copy link
Member

I can't get the lint to work on my machine. It complains that no such lint exists. I am version on 0.1.77. Does it work for you?

Ah, true. This will be in Rust 1.77.0, but the current stable is 1.76.0.

Copy link
Member

@mkroening mkroening left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@mkroening mkroening added this pull request to the merge queue Feb 22, 2024
Merged via the queue into hermit-os:main with commit 3472978 Feb 22, 2024
13 checks passed
@cagatay-y cagatay-y deleted the from-ref branch February 22, 2024 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants