Rename UniqueArena methods to more closely resemble HashSet.

`UniqueArena::fetch_or_append` becomes `insert`.

`UniqueArena::try_get` becomes `get_handle`, by analogy with `get`, that takes a
type as a key.
This commit is contained in:
Jim Blandy
2021-09-24 01:25:08 -07:00
committed by Dzmitry Malyshau
parent 944a693ae5
commit 6e4401ae96
18 changed files with 53 additions and 55 deletions

View File

@@ -476,7 +476,7 @@ impl<T: Eq + hash::Hash> UniqueArena<T> {
///
/// [`Handle<T>`]: Handle
/// [`get_span`]: UniqueArena::get_span
pub fn fetch_or_append(&mut self, value: T, span: Span) -> Handle<T> {
pub fn insert(&mut self, value: T, span: Span) -> Handle<T> {
let (index, added) = self.set.insert_full(value);
#[cfg(feature = "span")]
@@ -503,7 +503,7 @@ impl<T: Eq + hash::Hash> UniqueArena<T> {
}
/// Return this arena's value at `handle`, if that is a valid handle.
pub fn try_get(&self, handle: Handle<T>) -> Option<&T> {
pub fn get_handle(&self, handle: Handle<T>) -> Option<&T> {
self.set.get_index(handle.index())
}
}

View File

@@ -16,7 +16,7 @@ impl Module {
let mut parameters_info = Vec::with_capacity(args.len());
for arg in args {
parameters.push(self.types.fetch_or_append(
parameters.push(self.types.insert(
Type {
name: None,
inner: arg,
@@ -1224,7 +1224,7 @@ fn inject_common_builtin(
_ => Some(VectorSize::Quad),
};
let ty = module.types.fetch_or_append(
let ty = module.types.insert(
Type {
name: None,
inner: match size {
@@ -1937,7 +1937,7 @@ pub fn sampled_to_depth(
arrayed,
} => match class {
ImageClass::Sampled { multi, .. } => {
*ty = module.types.fetch_or_append(
*ty = module.types.insert(
Type {
name: None,
inner: TypeInner::Image {

View File

@@ -83,7 +83,7 @@ impl<'a> ConstantSolver<'a> {
let ty = match self.constants[value_constant].inner {
ConstantInner::Scalar { ref value, width } => {
let kind = value.scalar_kind();
self.types.fetch_or_append(
self.types.insert(
Type {
name: None,
inner: TypeInner::Vector { size, kind, width },
@@ -127,7 +127,7 @@ impl<'a> ConstantSolver<'a> {
kind,
width,
} => {
let dst_ty = self.types.fetch_or_append(
let dst_ty = self.types.insert(
Type {
name: None,
inner: crate::TypeInner::Vector { size, kind, width },
@@ -542,7 +542,7 @@ mod tests {
let mut expressions = Arena::new();
let mut constants = Arena::new();
let vec_ty = types.fetch_or_append(
let vec_ty = types.insert(
Type {
name: None,
inner: TypeInner::Vector {
@@ -720,7 +720,7 @@ mod tests {
let mut expressions = Arena::new();
let mut constants = Arena::new();
let matrix_ty = types.fetch_or_append(
let matrix_ty = types.insert(
Type {
name: None,
inner: TypeInner::Matrix {
@@ -732,7 +732,7 @@ mod tests {
Default::default(),
);
let vec_ty = types.fetch_or_append(
let vec_ty = types.insert(
Type {
name: None,
inner: TypeInner::Vector {

View File

@@ -257,7 +257,7 @@ impl Context {
if qualifier.is_lhs() {
let span = parser.module.types.get_span(arg.ty);
arg.ty = parser.module.types.fetch_or_append(
arg.ty = parser.module.types.insert(
Type {
name: None,
inner: TypeInner::Pointer {
@@ -818,7 +818,7 @@ impl Context {
self.add_expression(Expression::Splat { size, value: right }, meta, body);
if let Some(cols) = columns {
let ty = parser.module.types.fetch_or_append(
let ty = parser.module.types.insert(
Type {
name: None,
inner: ty_inner,

View File

@@ -149,7 +149,7 @@ impl Parser {
// value is used to initialize all the values along the diagonal of
// the matrix; the rest are given zeros.
let mut components = Vec::with_capacity(columns as usize);
let vector_ty = self.module.types.fetch_or_append(
let vector_ty = self.module.types.insert(
Type {
name: None,
inner: TypeInner::Vector {
@@ -296,7 +296,7 @@ impl Parser {
}
}
let ty = self.module.types.fetch_or_append(
let ty = self.module.types.insert(
Type {
name: None,
inner: TypeInner::Vector {
@@ -555,7 +555,7 @@ impl Parser {
if let TypeInner::Vector { size, kind, width } =
*self.resolve_type(ctx, handle, meta)?
{
let ty = self.module.types.fetch_or_append(
let ty = self.module.types.insert(
Type {
name: None,
inner: TypeInner::Vector { size, kind, width },
@@ -900,7 +900,7 @@ impl Parser {
}
let (ty, value) = if !components.is_empty() {
let ty = self.module.types.fetch_or_append(
let ty = self.module.types.insert(
Type {
name: None,
inner: TypeInner::Struct {

View File

@@ -85,7 +85,7 @@ pub fn calculate_offset(
};
let ty_span = types.get_span(ty);
ty = types.fetch_or_append(
ty = types.insert(
Type {
name,
inner: TypeInner::Array {
@@ -145,7 +145,7 @@ pub fn calculate_offset(
span = align_up(span, align);
let ty_span = types.get_span(ty);
ty = types.fetch_or_append(
ty = types.insert(
Type {
name,
inner: TypeInner::Struct {

View File

@@ -497,7 +497,7 @@ impl<'source> ParsingContext<'source> {
let span = self.parse_struct_declaration_list(parser, &mut members, layout)?;
self.expect(parser, TokenValue::RightBrace)?;
let mut ty = parser.module.types.fetch_or_append(
let mut ty = parser.module.types.insert(
Type {
name: Some(ty_name),
inner: TypeInner::Struct {

View File

@@ -147,7 +147,7 @@ impl<'source> ParsingContext<'source> {
},
Span::default(),
);
handle = parser.module.types.fetch_or_append(
handle = parser.module.types.insert(
Type {
name: None,
inner: TypeInner::Array {

View File

@@ -37,7 +37,7 @@ impl<'source> ParsingContext<'source> {
let token = self.bump(parser)?;
let handle = match token.value {
TokenValue::Void => None,
TokenValue::TypeName(ty) => Some(parser.module.types.fetch_or_append(ty, token.meta)),
TokenValue::TypeName(ty) => Some(parser.module.types.insert(ty, token.meta)),
TokenValue::Struct => {
let mut meta = token.meta;
let ty_name = self.expect_ident(parser)?.0;
@@ -47,7 +47,7 @@ impl<'source> ParsingContext<'source> {
self.parse_struct_declaration_list(parser, &mut members, StructLayout::Std140)?;
let end_meta = self.expect(parser, TokenValue::RightBrace)?.meta;
meta.subsume(end_meta);
let ty = parser.module.types.fetch_or_append(
let ty = parser.module.types.insert(
Type {
name: Some(ty_name.clone()),
inner: TypeInner::Struct {

View File

@@ -262,7 +262,7 @@ impl Parser {
array_specifier
.map(|(size, size_meta)| {
meta.subsume(size_meta);
self.module.types.fetch_or_append(
self.module.types.insert(
Type {
name: None,
inner: TypeInner::Array {

View File

@@ -58,7 +58,7 @@ impl Parser {
data: BuiltInData,
meta: Span,
) -> Option<VariableReference> {
let ty = self.module.types.fetch_or_append(
let ty = self.module.types.insert(
Type {
name: None,
inner: data.inner,
@@ -188,7 +188,7 @@ impl Parser {
storage: StorageQualifier::Output,
},
"gl_ClipDistance" | "gl_CullDistance" => {
let base = self.module.types.fetch_or_append(
let base = self.module.types.insert(
Type {
name: None,
inner: TypeInner::Scalar {

View File

@@ -477,7 +477,7 @@ impl<I: Iterator<Item = u32>> super::Parser<I> {
let span = crate::Span::total_span(
components.iter().map(|h| function.expressions.get_span(*h)),
);
let ty = module.types.fetch_or_append(
let ty = module.types.insert(
crate::Type {
name: None,
inner: crate::TypeInner::Struct {

View File

@@ -211,7 +211,7 @@ pub(super) fn patch_comparison_type(
};
let name = original_ty.name.clone();
var.ty = arena.fetch_or_append(
var.ty = arena.insert(
crate::Type {
name,
inner: ty_inner,

View File

@@ -3388,7 +3388,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
self.lookup_type.insert(
id,
LookupType {
handle: module.types.fetch_or_append(
handle: module.types.insert(
crate::Type {
name: self.future_decor.remove(&id).and_then(|dec| dec.name),
inner,
@@ -3423,7 +3423,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
self.lookup_type.insert(
id,
LookupType {
handle: module.types.fetch_or_append(
handle: module.types.insert(
crate::Type {
name: self.future_decor.remove(&id).and_then(|dec| dec.name),
inner,
@@ -3453,7 +3453,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
self.lookup_type.insert(
id,
LookupType {
handle: module.types.fetch_or_append(
handle: module.types.insert(
crate::Type {
name: self.future_decor.remove(&id).and_then(|dec| dec.name),
inner,
@@ -3490,7 +3490,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
self.lookup_type.insert(
id,
LookupType {
handle: module.types.fetch_or_append(
handle: module.types.insert(
crate::Type {
name: self.future_decor.remove(&id).and_then(|dec| dec.name),
inner,
@@ -3529,7 +3529,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
self.lookup_type.insert(
id,
LookupType {
handle: module.types.fetch_or_append(
handle: module.types.insert(
crate::Type {
name: decor.and_then(|dec| dec.name),
inner,
@@ -3593,7 +3593,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
base_lookup_ty.clone()
} else {
LookupType {
handle: module.types.fetch_or_append(
handle: module.types.insert(
crate::Type {
name: decor.and_then(|dec| dec.name),
inner: crate::TypeInner::Pointer {
@@ -3636,7 +3636,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
self.lookup_type.insert(
id,
LookupType {
handle: module.types.fetch_or_append(
handle: module.types.insert(
crate::Type {
name: decor.name,
inner,
@@ -3673,7 +3673,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
self.lookup_type.insert(
id,
LookupType {
handle: module.types.fetch_or_append(
handle: module.types.insert(
crate::Type {
name: decor.name,
inner,
@@ -3777,7 +3777,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
members,
};
let ty_handle = module.types.fetch_or_append(
let ty_handle = module.types.insert(
crate::Type {
name: parent_decor.and_then(|dec| dec.name),
inner,
@@ -3825,7 +3825,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
let decor = self.future_decor.remove(&id).unwrap_or_default();
// ensure there is a type for texture coordinate without extra components
module.types.fetch_or_append(
module.types.insert(
crate::Type {
name: None,
inner: {
@@ -3862,7 +3862,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
arrayed: is_array,
};
let handle = module.types.fetch_or_append(
let handle = module.types.insert(
crate::Type {
name: decor.name,
inner,
@@ -3905,7 +3905,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
inst.expect(2)?;
let id = self.next()?;
let decor = self.future_decor.remove(&id).unwrap_or_default();
let handle = module.types.fetch_or_append(
let handle = module.types.insert(
crate::Type {
name: decor.name,
inner: crate::TypeInner::Sampler { comparison: false },
@@ -4151,7 +4151,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
class: crate::ImageClass::Storage { format, access },
},
};
effective_ty = module.types.fetch_or_append(ty, Default::default());
effective_ty = module.types.insert(ty, Default::default());
}
let ext_class = match self.lookup_storage_buffer_types.get(&effective_ty) {
@@ -4215,7 +4215,7 @@ impl<I: Iterator<Item = u32>> Parser<I> {
) {
unsigned_ty = module
.types
.fetch_or_append(crate::Type { name: None, inner }, Default::default());
.insert(crate::Type { name: None, inner }, Default::default());
}
}

View File

@@ -602,7 +602,7 @@ mod type_inner_tests {
Default::default(),
);
let mytype1 = types.fetch_or_append(
let mytype1 = types.insert(
crate::Type {
name: Some("MyType1".to_string()),
inner: crate::TypeInner::Struct {
@@ -613,7 +613,7 @@ mod type_inner_tests {
},
Default::default(),
);
let mytype2 = types.fetch_or_append(
let mytype2 = types.insert(
crate::Type {
name: Some("MyType2".to_string()),
inner: crate::TypeInner::Struct {
@@ -1982,7 +1982,7 @@ impl Parser {
TypeResolution::Handle(handle) => handle,
TypeResolution::Value(inner) => ctx
.types
.fetch_or_append(crate::Type { name: None, inner }, Default::default()),
.insert(crate::Type { name: None, inner }, Default::default()),
};
components.push(last_component);
crate::Expression::Compose { ty, components }
@@ -3047,7 +3047,7 @@ impl Parser {
match self.parse_type_decl_impl(lexer, attribute, name, type_arena, const_arena)? {
Some(inner) => {
let span = name_span.start..lexer.current_byte_offset();
type_arena.fetch_or_append(
type_arena.insert(
crate::Type {
name: debug_name.map(|s| s.to_string()),
inner,
@@ -3308,12 +3308,10 @@ impl Parser {
// register the type, if needed
match context.typifier[value].clone() {
TypeResolution::Handle(ty) => ty,
TypeResolution::Value(inner) => {
context.types.fetch_or_append(
crate::Type { name: None, inner },
Default::default(),
)
}
TypeResolution::Value(inner) => context.types.insert(
crate::Type { name: None, inner },
Default::default(),
),
}
}
};
@@ -4004,7 +4002,7 @@ impl Parser {
let (members, span) =
self.parse_struct_body(lexer, &mut module.types, &mut module.constants)?;
let type_span = NagaSpan::from(lexer.span_from(start));
let ty = module.types.fetch_or_append(
let ty = module.types.insert(
crate::Type {
name: Some(name.to_string()),
inner: crate::TypeInner::Struct {

View File

@@ -870,7 +870,7 @@ fn uniform_control_flow() {
Default::default(),
);
let mut type_arena = crate::UniqueArena::new();
let ty = type_arena.fetch_or_append(
let ty = type_arena.insert(
crate::Type {
name: None,
inner: crate::TypeInner::Vector {

View File

@@ -25,7 +25,7 @@ pub fn validate_compose(
use crate::TypeInner as Ti;
let self_ty = type_arena
.try_get(self_ty_handle)
.get_handle(self_ty_handle)
.ok_or(ComposeError::TypeDoesntExist(self_ty_handle))?;
match self_ty.inner {
// vectors are composed from scalars or other vectors

View File

@@ -1224,7 +1224,7 @@ impl super::Validator {
if let Some(&Ti::Array {
size: crate::ArraySize::Dynamic,
..
}) = resolver.types.try_get(base).map(|ty| &ty.inner)
}) = resolver.types.get_handle(base).map(|ty| &ty.inner)
{
ShaderStages::all()
} else {