Skip to content

Update mutable static items example for modern Rust. #1544

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

Merged
merged 1 commit into from
Jul 23, 2024

Conversation

ehuss
Copy link
Contributor

@ehuss ehuss commented Jul 23, 2024

This updates the mutable static item example to fix some issues with how it works and how it is presented.

First, the unsafe operations are wrapped in an unsafe block so that it doesn't trigger unsafe_op_in_unsafe_fn.

Second, it switches the mutation example to use addr_of_mut to avoid the undefined behavior, and to compile correctly in 2024 edition.

Third, it rewrites the second example to be safe with a different description. My understanding is that the original example was written at a time when it was considered that any potential race condition was considered unsafe. However, that is no longer a widely held view. The example has been rewritten to illustrate the two different ways you can wrap mutable static access.

This is an alternative to #1349.

This updates the mutable static item example to fix some issues with how
it works and how it is presented.

First, the unsafe operations are wrapped in an `unsafe` block so that it
doesn't trigger `unsafe_op_in_unsafe_fn`.

Second, it switches the mutation example to use addr_of_mut to avoid the
undefined behavior, and to compile correctly in 2024 edition.

Third, it rewrites the second example to be safe with a different
description. My understanding is that the original example was written
at a time when it was considered that any potential race condition was
considered unsafe. However, that is no longer a widely held view. The
example has been rewritten to illustrate the two different ways you can
wrap mutable static access.
@traviscross traviscross added this pull request to the merge queue Jul 23, 2024
Merged via the queue into rust-lang:master with commit cdef0cb Jul 23, 2024
1 check passed
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