Skip to content

Commit

Permalink
Remove unsafety in ResourceId creation (#534)
Browse files Browse the repository at this point in the history
* fetch_add actually returns the old value

* Update recording.rs

* Update recording.rs

* Update src/recording.rs

Co-authored-by: Daniel McNab <36049421+DJMcNab@users.noreply.github.com>

---------

Co-authored-by: Daniel McNab <36049421+DJMcNab@users.noreply.github.com>
  • Loading branch information
DasLixou and DJMcNab authored Mar 29, 2024
1 parent 0996361 commit 45bc031
Showing 1 changed file with 3 additions and 5 deletions.
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 so that the conversion below succeeds
static ID_COUNTER: AtomicU64 = AtomicU64::new(1);
ResourceId(NonZeroU64::new(ID_COUNTER.fetch_add(1, Ordering::Relaxed)).unwrap())
}
}

Expand Down

0 comments on commit 45bc031

Please sign in to comment.