1use std::fmt::{Debug, Display};
6
7pub type Offset = u64;
9
10pub type Tag = u64;
12
13#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd)]
15pub struct VariantIdx(pub u32);
16
17#[derive(Debug, Clone, Copy, PartialEq)]
23pub enum Immediate<'a> {
24 Null,
26 Bool(bool),
27 Int64(i64),
28 Float(f64),
29 String(&'a str),
31 Bytes(&'a [u8]),
33 Variant0(VariantIdx),
35 Ref(Offset),
37 Pointer(Offset),
39}
40
41impl<'a> Default for Immediate<'a> {
42 fn default() -> Self {
43 Immediate::Null
44 }
45}
46
47macro_rules! impl_from {
48 ($variant:path, $typ:ty, $typ_real:ty) => {
49 impl<'a> From<$typ_real> for Immediate<'a> {
50 fn from(v: $typ_real) -> Immediate<'a> {
51 $variant(v as $typ)
52 }
53 }
54 };
55}
56
57impl<'a> From<()> for Immediate<'a> {
58 fn from(_v: ()) -> Self {
59 Immediate::Null
60 }
61}
62
63impl_from!(Immediate::Bool, bool, bool);
64impl_from!(Immediate::Int64, i64, i64);
65impl_from!(Immediate::Int64, i64, i32);
66impl_from!(Immediate::Float, f64, f64);
67impl_from!(Immediate::Float, f64, f32);
68impl_from!(Immediate::String, &'a str, &'a str);
69impl_from!(Immediate::Bytes, &'a [u8], &'a [u8]);
70impl_from!(Immediate::Pointer, Offset, Offset);
71
72#[derive(Debug, Clone, Copy)]
73pub struct Error {
74 pub msg: &'static str,
76 pub off: Offset,
78}
79
80pub type Result<T> = core::result::Result<T, Error>;
81
82impl Display for Error {
83 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
84 write!(
85 f,
86 "Twine error: {} at offset=0x{:x} ({})",
87 self.msg, self.off, self.off
88 )
89 }
90}
91
92impl std::error::Error for Error {}