Skip to main content

vrm_runtime/
id.rs

1use std::fmt;
2
3macro_rules! define_id {
4    ($name:ident, $label:literal) => {
5        #[doc = concat!("A stable identifier for an avatar ", $label, ".")]
6        #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
7        pub struct $name(u32);
8
9        impl $name {
10            /// Creates an identifier from an index supplied to an asset builder.
11            ///
12            /// [`crate::AvatarAssetBuilder::build`] rejects identifiers that do
13            /// not resolve inside the completed asset.
14            pub const fn from_index(index: usize) -> Self {
15                Self(index as u32)
16            }
17
18            /// Returns the index used by the immutable asset storage.
19            pub const fn index(self) -> usize {
20                self.0 as usize
21            }
22        }
23
24        impl fmt::Display for $name {
25            fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
26                self.0.fmt(formatter)
27            }
28        }
29    };
30}
31
32define_id!(NodeId, "node");
33define_id!(PrimitiveId, "render primitive");
34define_id!(MaterialId, "material");
35define_id!(TextureId, "texture");
36define_id!(ImageId, "image");
37define_id!(SkinId, "skin");