1 //! A basic `Variable` implementation.
2 //!
3 //! Frontends can use any indexing scheme they see fit and
4 //! generate the appropriate `Variable` instances.
5 
6 use core::u32;
7 use cranelift_codegen::entity::EntityRef;
8 
9 ///! An opaque reference to a variable.
10 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
11 pub struct Variable(u32);
12 
13 impl Variable {
14     /// Create a new Variable with the given index.
15     pub fn with_u32(index: u32) -> Self {
16         debug_assert!(index < u32::MAX);
17         Self(index)
18     }
19 }
20 
21 impl EntityRef for Variable {
22     fn new(index: usize) -> Self {
23         debug_assert!(index < (u32::MAX as usize));
24         Self(index as u32)
25     }
26 
27     fn index(self) -> usize {
28         self.0 as usize
29     }
30 }
31