From 434d584dc14ee6f3f4bbb87639645d65789bf05d Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Thu, 31 Mar 2022 17:22:49 -0700 Subject: [PATCH] Provide a proper `new` method for `RefCount`. --- wgpu-core/src/lib.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/wgpu-core/src/lib.rs b/wgpu-core/src/lib.rs index f788086e0b..84922e6b30 100644 --- a/wgpu-core/src/lib.rs +++ b/wgpu-core/src/lib.rs @@ -93,6 +93,12 @@ unsafe impl Sync for RefCount {} impl RefCount { const MAX: usize = 1 << 24; + /// Construct a new `RefCount`, with an initial count of 1. + fn new() -> RefCount { + let bx = Box::new(AtomicUsize::new(1)); + Self(unsafe { ptr::NonNull::new_unchecked(Box::into_raw(bx)) }) + } + fn load(&self) -> usize { unsafe { self.0.as_ref() }.load(Ordering::Acquire) } @@ -184,9 +190,8 @@ pub struct LifeGuard { impl LifeGuard { #[allow(unused_variables)] fn new(label: &str) -> Self { - let bx = Box::new(AtomicUsize::new(1)); Self { - ref_count: ptr::NonNull::new(Box::into_raw(bx)).map(RefCount), + ref_count: Some(RefCount::new()), submission_index: AtomicUsize::new(0), #[cfg(debug_assertions)] label: label.to_string(),