Skip to content

Commit a70d907

Browse files
authored
Merge pull request #277 from briansmith/b/as-ptr
Reduce MSRV to 1.65 by polyfilling `AtomicUsize::as_ptr()`.
2 parents 0d6bc31 + 9173b99 commit a70d907

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
# Changelog
22

3+
## 1.21.1
4+
- Reduce MSRV to 1.65: [#277](https://github.com/matklad/once_cell/pull/277).
5+
36
## 1.21.0
47

58
- Outline initialization in `race`: [#273](https://github.com/matklad/once_cell/pull/273).
69
- Add `OnceNonZereUsize::get_unchecked`: [#274](https://github.com/matklad/once_cell/pull/274).
710
- Add `OnceBox::clone` and `OnceBox::with_value`: [#275](https://github.com/matklad/once_cell/pull/275).
11+
- Increase MSRV to 1.70
812

913
## 1.20.2
1014

Cargo.lock.msrv

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
[package]
22
name = "once_cell"
3-
version = "1.21.0"
3+
version = "1.21.1"
44
authors = ["Aleksey Kladov <aleksey.kladov@gmail.com>"]
55
license = "MIT OR Apache-2.0"
66
edition = "2021"
7-
rust-version = "1.70"
7+
rust-version = "1.65"
88

99
description = "Single assignment cells and lazy values."
1010
readme = "README.md"

src/race.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,23 @@ impl OnceNonZeroUsize {
5858
/// Caller must ensure that the cell is in initialized state, and that
5959
/// the contents are acquired by (synchronized to) this thread.
6060
pub unsafe fn get_unchecked(&self) -> NonZeroUsize {
61-
let p = self.inner.as_ptr();
61+
#[inline(always)]
62+
fn as_const_ptr(r: &AtomicUsize) -> *const usize {
63+
use core::mem::align_of;
64+
65+
let p: *const AtomicUsize = r;
66+
// SAFETY: "This type has the same size and bit validity as
67+
// the underlying integer type, usize. However, the alignment of
68+
// this type is always equal to its size, even on targets where
69+
// usize has a lesser alignment."
70+
const _ALIGNMENT_COMPATIBLE: () =
71+
assert!(align_of::<AtomicUsize>() % align_of::<usize>() == 0);
72+
p.cast::<usize>()
73+
}
74+
75+
// TODO(MSRV-1.70): Use `AtomicUsize::as_ptr().cast_const()`
76+
// See https://github.com/rust-lang/rust/issues/138246.
77+
let p = as_const_ptr(&self.inner);
6278

6379
// SAFETY: The caller is responsible for ensuring that the value
6480
// was initialized and that the contents have been acquired by

0 commit comments

Comments
 (0)