/src/wgpu/naga/src/arena/handlevec.rs
Line | Count | Source |
1 | | //! The [`HandleVec`] type and associated definitions. |
2 | | |
3 | | use super::handle::Handle; |
4 | | use alloc::{vec, vec::Vec}; |
5 | | |
6 | | use core::marker::PhantomData; |
7 | | use core::ops; |
8 | | |
9 | | /// A [`Vec`] indexed by [`Handle`]s. |
10 | | /// |
11 | | /// A `HandleVec<T, U>` is a [`Vec<U>`] indexed by values of type `Handle<T>`, |
12 | | /// rather than `usize`. |
13 | | /// |
14 | | /// Rather than a `push` method, `HandleVec` has an [`insert`] method, analogous |
15 | | /// to [`HashMap::insert`], that requires you to provide the handle at which the |
16 | | /// new value should appear. However, since `HandleVec` only supports insertion |
17 | | /// at the end, the given handle's index must be equal to the `HandleVec`'s |
18 | | /// current length; otherwise, the insertion will panic. |
19 | | /// |
20 | | /// [`insert`]: HandleVec::insert |
21 | | /// [`HashMap::insert`]: hashbrown::HashMap::insert |
22 | | #[derive(Debug)] |
23 | | pub(crate) struct HandleVec<T, U> { |
24 | | inner: Vec<U>, |
25 | | as_keys: PhantomData<T>, |
26 | | } |
27 | | |
28 | | impl<T, U> Default for HandleVec<T, U> { |
29 | 123 | fn default() -> Self { |
30 | 123 | Self { |
31 | 123 | inner: vec![], |
32 | 123 | as_keys: PhantomData, |
33 | 123 | } |
34 | 123 | } <naga::arena::handlevec::HandleVec<naga::ir::Expression, naga::proc::typifier::TypeResolution> as core::default::Default>::default Line | Count | Source | 29 | 31 | fn default() -> Self { | 30 | 31 | Self { | 31 | 31 | inner: vec![], | 32 | 31 | as_keys: PhantomData, | 33 | 31 | } | 34 | 31 | } |
<naga::arena::handlevec::HandleVec<naga::ir::Type, naga::proc::layouter::TypeLayout> as core::default::Default>::default Line | Count | Source | 29 | 92 | fn default() -> Self { | 30 | 92 | Self { | 31 | 92 | inner: vec![], | 32 | 92 | as_keys: PhantomData, | 33 | 92 | } | 34 | 92 | } |
|
35 | | } |
36 | | |
37 | | #[allow(dead_code)] |
38 | | impl<T, U> HandleVec<T, U> { |
39 | 145 | pub(crate) const fn new() -> Self { |
40 | 145 | Self { |
41 | 145 | inner: vec![], |
42 | 145 | as_keys: PhantomData, |
43 | 145 | } |
44 | 145 | } <naga::arena::handlevec::HandleVec<naga::ir::Expression, naga::proc::constant_evaluator::ExpressionKind>>::new Line | Count | Source | 39 | 88 | pub(crate) const fn new() -> Self { | 40 | 88 | Self { | 41 | 88 | inner: vec![], | 42 | 88 | as_keys: PhantomData, | 43 | 88 | } | 44 | 88 | } |
<naga::arena::handlevec::HandleVec<naga::ir::Expression, naga::proc::typifier::TypeResolution>>::new Line | Count | Source | 39 | 57 | pub(crate) const fn new() -> Self { | 40 | 57 | Self { | 41 | 57 | inner: vec![], | 42 | 57 | as_keys: PhantomData, | 43 | 57 | } | 44 | 57 | } |
|
45 | | |
46 | 0 | pub(crate) fn with_capacity(capacity: usize) -> Self { |
47 | 0 | Self { |
48 | 0 | inner: Vec::with_capacity(capacity), |
49 | 0 | as_keys: PhantomData, |
50 | 0 | } |
51 | 0 | } Unexecuted instantiation: <naga::arena::handlevec::HandleVec<naga::ir::Expression, naga::arena::handle::Handle<naga::ir::Expression>>>::with_capacity Unexecuted instantiation: <naga::arena::handlevec::HandleVec<naga::ir::Expression, naga::proc::constant_evaluator::ExpressionKind>>::with_capacity Unexecuted instantiation: <naga::arena::handlevec::HandleVec<naga::ir::Override, naga::arena::handle::Handle<naga::ir::Constant>>>::with_capacity |
52 | | |
53 | 8.49M | pub(crate) const fn len(&self) -> usize { |
54 | 8.49M | self.inner.len() |
55 | 8.49M | } <naga::arena::handlevec::HandleVec<naga::ir::Expression, naga::proc::typifier::TypeResolution>>::len Line | Count | Source | 53 | 8.39M | pub(crate) const fn len(&self) -> usize { | 54 | 8.39M | self.inner.len() | 55 | 8.39M | } |
<naga::arena::handlevec::HandleVec<naga::ir::Type, naga::proc::layouter::TypeLayout>>::len Line | Count | Source | 53 | 96.5k | pub(crate) const fn len(&self) -> usize { | 54 | 96.5k | self.inner.len() | 55 | 96.5k | } |
|
56 | | |
57 | | /// Insert a mapping from `handle` to `value`. |
58 | | /// |
59 | | /// Unlike a [`HashMap`], a `HandleVec` can only have new entries inserted at |
60 | | /// the end, like [`Vec::push`]. So the index of `handle` must equal |
61 | | /// [`self.len()`]. |
62 | | /// |
63 | | /// [`HashMap`]: hashbrown::HashMap |
64 | | /// [`self.len()`]: HandleVec::len |
65 | 22.3M | pub(crate) fn insert(&mut self, handle: Handle<T>, value: U) { |
66 | 22.3M | assert_eq!(handle.index(), self.inner.len()); |
67 | 22.3M | self.inner.push(value); |
68 | 22.3M | } Unexecuted instantiation: <naga::arena::handlevec::HandleVec<naga::ir::Expression, naga::arena::handle::Handle<naga::ir::Expression>>>::insert <naga::arena::handlevec::HandleVec<naga::ir::Expression, naga::proc::constant_evaluator::ExpressionKind>>::insert Line | Count | Source | 65 | 13.1M | pub(crate) fn insert(&mut self, handle: Handle<T>, value: U) { | 66 | 13.1M | assert_eq!(handle.index(), self.inner.len()); | 67 | 13.1M | self.inner.push(value); | 68 | 13.1M | } |
<naga::arena::handlevec::HandleVec<naga::ir::Expression, naga::proc::typifier::TypeResolution>>::insert Line | Count | Source | 65 | 9.02M | pub(crate) fn insert(&mut self, handle: Handle<T>, value: U) { | 66 | 9.02M | assert_eq!(handle.index(), self.inner.len()); | 67 | 9.02M | self.inner.push(value); | 68 | 9.02M | } |
<naga::arena::handlevec::HandleVec<naga::ir::Type, naga::proc::layouter::TypeLayout>>::insert Line | Count | Source | 65 | 96.4k | pub(crate) fn insert(&mut self, handle: Handle<T>, value: U) { | 66 | 96.4k | assert_eq!(handle.index(), self.inner.len()); | 67 | 96.4k | self.inner.push(value); | 68 | 96.4k | } |
Unexecuted instantiation: <naga::arena::handlevec::HandleVec<naga::ir::Override, naga::arena::handle::Handle<naga::ir::Constant>>>::insert |
69 | | |
70 | 0 | pub(crate) fn get(&self, handle: Handle<T>) -> Option<&U> { |
71 | 0 | self.inner.get(handle.index()) |
72 | 0 | } |
73 | | |
74 | 22 | pub(crate) fn clear(&mut self) { |
75 | 22 | self.inner.clear() |
76 | 22 | } Unexecuted instantiation: <naga::arena::handlevec::HandleVec<naga::ir::Expression, naga::proc::typifier::TypeResolution>>::clear <naga::arena::handlevec::HandleVec<naga::ir::Type, naga::proc::layouter::TypeLayout>>::clear Line | Count | Source | 74 | 22 | pub(crate) fn clear(&mut self) { | 75 | 22 | self.inner.clear() | 76 | 22 | } |
|
77 | | |
78 | 0 | pub(crate) fn resize(&mut self, len: usize, fill: U) |
79 | 0 | where |
80 | 0 | U: Clone, |
81 | | { |
82 | 0 | self.inner.resize(len, fill); |
83 | 0 | } |
84 | | |
85 | 0 | pub(crate) fn iter(&self) -> impl Iterator<Item = &U> { |
86 | 0 | self.inner.iter() |
87 | 0 | } |
88 | | |
89 | 0 | pub(crate) fn iter_mut(&mut self) -> impl Iterator<Item = &mut U> { |
90 | 0 | self.inner.iter_mut() |
91 | 0 | } |
92 | | } |
93 | | |
94 | | impl<T, U> ops::Index<Handle<T>> for HandleVec<T, U> { |
95 | | type Output = U; |
96 | | |
97 | 24.4M | fn index(&self, handle: Handle<T>) -> &Self::Output { |
98 | 24.4M | &self.inner[handle.index()] |
99 | 24.4M | } Unexecuted instantiation: <naga::arena::handlevec::HandleVec<naga::ir::Expression, naga::arena::handle::Handle<naga::ir::Expression>> as core::ops::index::Index<naga::arena::handle::Handle<naga::ir::Expression>>>::index <naga::arena::handlevec::HandleVec<naga::ir::Expression, naga::proc::constant_evaluator::ExpressionKind> as core::ops::index::Index<naga::arena::handle::Handle<naga::ir::Expression>>>::index Line | Count | Source | 97 | 18.5M | fn index(&self, handle: Handle<T>) -> &Self::Output { | 98 | 18.5M | &self.inner[handle.index()] | 99 | 18.5M | } |
<naga::arena::handlevec::HandleVec<naga::ir::Expression, naga::proc::typifier::TypeResolution> as core::ops::index::Index<naga::arena::handle::Handle<naga::ir::Expression>>>::index Line | Count | Source | 97 | 5.74M | fn index(&self, handle: Handle<T>) -> &Self::Output { | 98 | 5.74M | &self.inner[handle.index()] | 99 | 5.74M | } |
<naga::arena::handlevec::HandleVec<naga::ir::Type, naga::proc::layouter::TypeLayout> as core::ops::index::Index<naga::arena::handle::Handle<naga::ir::Type>>>::index Line | Count | Source | 97 | 203k | fn index(&self, handle: Handle<T>) -> &Self::Output { | 98 | 203k | &self.inner[handle.index()] | 99 | 203k | } |
Unexecuted instantiation: <naga::arena::handlevec::HandleVec<naga::ir::Override, naga::arena::handle::Handle<naga::ir::Constant>> as core::ops::index::Index<naga::arena::handle::Handle<naga::ir::Override>>>::index |
100 | | } |
101 | | |
102 | | impl<T, U> ops::IndexMut<Handle<T>> for HandleVec<T, U> { |
103 | 2 | fn index_mut(&mut self, handle: Handle<T>) -> &mut Self::Output { |
104 | 2 | &mut self.inner[handle.index()] |
105 | 2 | } <naga::arena::handlevec::HandleVec<naga::ir::Expression, naga::proc::constant_evaluator::ExpressionKind> as core::ops::index::IndexMut<naga::arena::handle::Handle<naga::ir::Expression>>>::index_mut Line | Count | Source | 103 | 2 | fn index_mut(&mut self, handle: Handle<T>) -> &mut Self::Output { | 104 | 2 | &mut self.inner[handle.index()] | 105 | 2 | } |
Unexecuted instantiation: <naga::arena::handlevec::HandleVec<naga::ir::Expression, naga::proc::typifier::TypeResolution> as core::ops::index::IndexMut<naga::arena::handle::Handle<naga::ir::Expression>>>::index_mut |
106 | | } |