near_vm_compiler/
sourceloc.rs1use crate::lib::std::fmt;
11
12#[derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive, Debug, Clone, Copy, PartialEq, Eq)]
17#[repr(transparent)]
18pub struct SourceLoc(u32);
19
20impl SourceLoc {
21 pub fn new(bits: u32) -> Self {
23 Self(bits)
24 }
25
26 pub fn is_default(self) -> bool {
28 self == Default::default()
29 }
30
31 pub fn bits(self) -> u32 {
33 self.0
34 }
35}
36
37impl Default for SourceLoc {
38 fn default() -> Self {
39 Self(!0)
40 }
41}
42
43impl fmt::Display for SourceLoc {
44 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
45 if self.is_default() { write!(f, "0x-") } else { write!(f, "0x{:04x}", self.0) }
46 }
47}
48
49#[cfg(test)]
50mod tests {
51 use super::SourceLoc;
52 use crate::lib::std::string::ToString;
53
54 #[test]
55 fn display() {
56 assert_eq!(SourceLoc::default().to_string(), "0x-");
57 assert_eq!(SourceLoc::new(0).to_string(), "0x0000");
58 assert_eq!(SourceLoc::new(16).to_string(), "0x0010");
59 assert_eq!(SourceLoc::new(0xabcdef).to_string(), "0xabcdef");
60 }
61}