cranelift_frontend/
variable.rs

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//! Note: The `Variable` is used by Cranelift to index into densely allocated
7//! arrays containing information about your mutable variables
8//! Thus, make sure that Variable's indexes are allocated contiguously and
9//! starting at `0`.
10
11use core::u32;
12use cranelift_codegen::entity::EntityRef;
13
14///! An opaque reference to a variable.
15#[derive(Copy, Clone, PartialEq, Eq, Debug)]
16pub struct Variable(u32);
17
18impl Variable {
19    /// Create a new Variable with the given index.
20    pub fn with_u32(index: u32) -> Self {
21        debug_assert!(index < u32::MAX);
22        Self(index)
23    }
24}
25
26impl EntityRef for Variable {
27    fn new(index: usize) -> Self {
28        debug_assert!(index < (u32::MAX as usize));
29        Self(index as u32)
30    }
31
32    fn index(self) -> usize {
33        self.0 as usize
34    }
35}