1use std::time::{Duration, SystemTime, UNIX_EPOCH};
16
17pub(crate) const INVALID_REGISTER: u32 = 0xffff_ffff;
18
19macro_rules! ffi_wrap {
20 ($n:expr, $b:expr) => {{
21 use std::panic;
22 use std::process;
23
24 panic::catch_unwind(|| $b).unwrap_or_else(|_| {
25 ::tracing::error!("ffi callback caught panic: {}", $n);
26 process::abort()
27 })
28 }};
29}
30
31pub(crate) fn time_from_bn(timestamp: u64) -> SystemTime {
32 let m = Duration::from_secs(timestamp);
33 UNIX_EPOCH + m
34}
35
36pub(crate) unsafe fn slice_from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
37 if len == 0 {
38 &[]
44 } else {
45 unsafe { std::slice::from_raw_parts(data, len) }
46 }
47}
48
49#[macro_export]
50macro_rules! ffi_span {
51 ($name:expr, $bv:expr) => {{
52 #[allow(unused_imports)]
53 use $crate::file_metadata::FileMetadata;
54 ::tracing::info_span!($name, session_id = $bv.file().session_id().0).entered()
55 }};
56 ($name:expr) => {
57 ::tracing::info_span!($name).entered()
58 };
59}
60
61macro_rules! new_id_type {
62 ($(#[$meta:meta])* $name:ident, $inner_type:ty $(, $ffi_type:ty, $ffi_field:ident)?) => {
63 $(#[$meta])*
64 #[derive(std::fmt::Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
65 pub struct $name(pub $inner_type);
66
67 impl From<$inner_type> for $name {
68 fn from(value: $inner_type) -> Self {
69 Self(value)
70 }
71 }
72
73 impl From<$name> for $inner_type {
74 fn from(value: $name) -> Self {
75 value.0
76 }
77 }
78
79 impl std::fmt::Display for $name {
80 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81 write!(f, "{}", self.0)
82 }
83 }
84
85 $(
86 impl From<$ffi_type> for $name {
87 fn from(value: $ffi_type) -> Self {
88 Self(value.$ffi_field)
89 }
90 }
91
92 impl From<$name> for $ffi_type {
93 fn from(value: $name) -> Self {
94 Self { $ffi_field: value.0 }
95 }
96 }
97 )?
98 };
99}