Skip to content

Commit

Permalink
Use an enum
Browse files Browse the repository at this point in the history
  • Loading branch information
nical committed Feb 15, 2024
1 parent 421137d commit 1d87b9a
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions wgpu-core/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ use crate::{
};
use std::{fmt::Debug, marker::PhantomData};

#[derive(Copy, Clone, Debug, PartialEq)]
enum IdSource {
External,
Allocated,
None,
}

/// A simple structure to allocate [`Id`] identifiers.
///
/// Calling [`alloc`] returns a fresh, never-before-seen id. Calling [`free`]
Expand Down Expand Up @@ -34,15 +41,15 @@ use std::{fmt::Debug, marker::PhantomData};
/// [`Backend`]: wgt::Backend;
/// [`alloc`]: IdentityManager::alloc
/// [`free`]: IdentityManager::free
#[derive(Debug, Default)]
#[derive(Debug)]
pub(super) struct IdentityValues {
free: Vec<(Index, Epoch)>,
next_index: Index,
count: usize,
// Sanity check: The allocation logic works under the assumption that we don't
// do a mix of allocating ids from here and providing ids manually for the same
// storage container.
allocate_ids: Option<bool>,
id_source: IdSource,
}

impl IdentityValues {
Expand All @@ -52,10 +59,10 @@ impl IdentityValues {
/// different `backend` values are always distinct.
pub fn alloc<T: Marker>(&mut self, backend: Backend) -> Id<T> {
assert!(
self.allocate_ids != Some(false),
self.id_source != IdSource::External,
"Mix of internally allocated and externally provided IDs"
);
self.allocate_ids = Some(true);
self.id_source = IdSource::Allocated;

self.count += 1;
match self.free.pop() {
Expand All @@ -71,10 +78,10 @@ impl IdentityValues {

pub fn mark_as_used<T: Marker>(&mut self, id: Id<T>) -> Id<T> {
assert!(
self.allocate_ids != Some(true),
self.id_source != IdSource::Allocated,
"Mix of internally allocated and externally provided IDs"
);
self.allocate_ids = Some(false);
self.id_source = IdSource::External;

self.count += 1;
id
Expand Down Expand Up @@ -117,7 +124,7 @@ impl<T: Marker> IdentityManager<T> {
free: Vec::new(),
next_index: 0,
count: 0,
allocate_ids: None,
id_source: IdSource::None,
}),
_phantom: PhantomData,
}
Expand Down

0 comments on commit 1d87b9a

Please sign in to comment.