code_gen/rust/var/
rust_primitive.rs

1use crate::rust::RustType;
2use crate::{CodeBuffer, Expression, WithName};
3
4/// A Rust primitive.
5#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
6pub enum RustPrimitive {
7    UnsignedInt8,
8    UnsignedInt16,
9    UnsignedInt32,
10    UnsignedInt64,
11    UnsignedInt128,
12    UnsignedIntSize,
13    SignedInt8,
14    SignedInt16,
15    SignedInt32,
16    SignedInt64,
17    SignedInt128,
18    SignedIntSize,
19    Float32,
20    Float64,
21    Boolean,
22    Character,
23}
24
25impl WithName for RustPrimitive {
26    fn name(&self) -> &str {
27        match self {
28            Self::UnsignedInt8 => "u8",
29            Self::UnsignedInt16 => "u16",
30            Self::UnsignedInt32 => "u32",
31            Self::UnsignedInt64 => "u64",
32            Self::UnsignedInt128 => "u128",
33            Self::UnsignedIntSize => "usize",
34            Self::SignedInt8 => "i8",
35            Self::SignedInt16 => "i16",
36            Self::SignedInt32 => "i32",
37            Self::SignedInt64 => "i64",
38            Self::SignedInt128 => "i128",
39            Self::SignedIntSize => "isize",
40            Self::Float32 => "f32",
41            Self::Float64 => "f64",
42            Self::Boolean => "bool",
43            Self::Character => "char",
44        }
45    }
46}
47
48impl RustPrimitive {
49    //! Rust Types
50
51    /// Converts the Rust primitive to a Rust type.
52    pub const fn to_type_tag(&self) -> RustType {
53        RustType::Primitive(*self)
54    }
55}
56
57impl Expression for RustPrimitive {
58    fn write(&self, b: &mut CodeBuffer) {
59        self.write_name(b);
60    }
61}