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

Remove unsafety in ResourceId creation #534

Merged
merged 4 commits into from
Mar 29, 2024
Merged
Changes from 3 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
8 changes: 3 additions & 5 deletions src/recording.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ pub struct ResourceId(pub NonZeroU64);

impl ResourceId {
pub fn next() -> ResourceId {
static ID_COUNTER: AtomicU64 = AtomicU64::new(0);

let val = ID_COUNTER.fetch_add(1, Ordering::Relaxed);
// SAFETY: Smallest value is 1 because we just incremented.
ResourceId(unsafe { NonZeroU64::new_unchecked(val + 1) })
// We initialize with 1 because unsafe code expects this to be non-zero.
DasLixou marked this conversation as resolved.
Show resolved Hide resolved
static ID_COUNTER: AtomicU64 = AtomicU64::new(1);
DasLixou marked this conversation as resolved.
Show resolved Hide resolved
ResourceId(NonZeroU64::new(ID_COUNTER.fetch_add(1, Ordering::Relaxed)).unwrap())
}
}

Expand Down