Improve some doc comments relating to type-safe indexing

This commit is contained in:
Mark McCaskey
2019-12-17 18:45:07 -05:00
parent f9bb579c05
commit d165a85743
2 changed files with 24 additions and 10 deletions

View File

@ -174,16 +174,21 @@ pub struct ImportName {
pub name_index: NameIndex, pub name_index: NameIndex,
} }
/// Kinds of export indexes. /// A wrapper around the [`TypedIndex`]es for Wasm functions, Wasm memories,
/// Wasm globals, and Wasm tables.
///
/// Used in [`ModuleInfo`] to access function signatures ([`SigIndex`]s,
/// [`FuncSig`]), [`GlobalInit`]s, [`MemoryDescriptor`]s, and
/// [`TableDescriptor`]s.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)] #[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExportIndex { pub enum ExportIndex {
/// Function export index. /// Function export index. Used to map to things relating to Wasm functions.
Func(FuncIndex), Func(FuncIndex),
/// Memory export index. /// Memory export index. Used to map to things relating to Wasm memories.
Memory(MemoryIndex), Memory(MemoryIndex),
/// Global export index. /// Global export index. Used to map to things relating to Wasm globals.
Global(GlobalIndex), Global(GlobalIndex),
/// Table export index. /// Table export index. Used to map to things relating to Wasm tables.
Table(TableIndex), Table(TableIndex),
} }
@ -218,7 +223,7 @@ pub struct StringTableBuilder<K: TypedIndex> {
} }
impl<K: TypedIndex> StringTableBuilder<K> { impl<K: TypedIndex> StringTableBuilder<K> {
/// Creates a new `StringTableBuilder`. /// Creates a new [`StringTableBuilder`].
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
map: IndexMap::new(), map: IndexMap::new(),
@ -250,7 +255,7 @@ impl<K: TypedIndex> StringTableBuilder<K> {
} }
} }
/// Finish building the `StringTable`. /// Finish building the [`StringTable`].
pub fn finish(self) -> StringTable<K> { pub fn finish(self) -> StringTable<K> {
let table = self let table = self
.map .map
@ -291,7 +296,7 @@ impl<K: TypedIndex> StringTable<K> {
} }
} }
/// Namespace index. /// A type-safe way to get namespaces from a [`StringTable`].
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, Hash)] #[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct NamespaceIndex(u32); pub struct NamespaceIndex(u32);
@ -307,7 +312,7 @@ impl TypedIndex for NamespaceIndex {
} }
} }
/// Name index. /// A type-safe way to get names from a [`StringTable`].
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, Hash)] #[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct NameIndex(u32); pub struct NameIndex(u32);

View File

@ -7,10 +7,19 @@ pub use self::boxed::BoxedMap;
pub use self::map::{Iter, IterMut, Map}; pub use self::map::{Iter, IterMut, Map};
pub use self::slice::SliceMap; pub use self::slice::SliceMap;
/// Represents a typed index. /// A trait for dealing with type-safe indices into associative data structures
/// like [`Map`]s.
///
/// Through the use of this trait, we get compile time checks that we are not
/// using the wrong type of index into our data structures.
///
/// It acts as a thin wrapper over `usize` and in most usage patterns has no
/// runtime overhead.
pub trait TypedIndex: Copy + Clone { pub trait TypedIndex: Copy + Clone {
/// Create a new instance of [`TypedIndex`] from a raw index value
#[doc(hidden)] #[doc(hidden)]
fn new(index: usize) -> Self; fn new(index: usize) -> Self;
/// Get the raw index value from the [`TypedIndex`]
#[doc(hidden)] #[doc(hidden)]
fn index(&self) -> usize; fn index(&self) -> usize;
} }