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 ($name:ident, $inner_type:ty) => {
63 #[derive(std::fmt::Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
64 pub struct $name(pub $inner_type);
65
66 impl From<$inner_type> for $name {
67 fn from(value: $inner_type) -> Self {
68 Self(value)
69 }
70 }
71
72 impl From<$name> for $inner_type {
73 fn from(value: $name) -> Self {
74 value.0
75 }
76 }
77
78 impl std::fmt::Display for $name {
79 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80 write!(f, "{}", self.0)
81 }
82 }
83 };
84}