macro_rules! doc_op {
(
short: $short:literal,
syscall: $syscall:literal,
doc_link: $url:literal,
$($rest:tt)*
) => {
#[doc = $short]
#[doc = concat!("\n\nEquivalent to the [`", $syscall, "`](", $url, ") syscall.\n")]
$($rest)*
};
(
short: $short:literal,
syscall: $syscall:literal,
$($rest:tt)*
) => {
#[doc = $short]
#[doc = concat!("\n\nEquivalent to the `", $syscall, "` syscall.\n")]
$($rest)*
};
(
short: $short:literal,
$($rest:tt)*
) => {
#[doc = $short]
$($rest)*
};
(
syscall: $syscall:literal,
doc_link: $url:literal,
$($rest:tt)*
) => {
#[doc = concat!("\n\nEquivalent to the [`", $syscall, "`](", $url, ") syscall.\n")]
$($rest)*
};
(
syscall: $syscall:literal,
$($rest:tt)*
) => {
#[doc = concat!("\n\nEquivalent to the `", $syscall, "` syscall.\n")]
$($rest)*
};
(
$($rest:tt)*
) => {
$($rest)*
};
}
macro_rules! syscall {
(raw $fn: ident ( $($arg: expr),* $(,)* ) ? ) => {{
let val = syscall!(raw $fn ($($arg),*));
if val < 0 {
return val;
}
val
}};
(raw $fn: ident ( $($arg: expr),* $(,)* ) ) => {{
#[allow(unused_unsafe)]
{
let res = unsafe { libc::$fn($($arg, )*) };
if res != -1 {
res as isize
}
else {
#[cfg(target_os = "linux")]
{
let errno = unsafe { *libc::__errno_location() };
-(errno as isize)
}
#[cfg(any(target_os = "macos", target_os = "freebsd"))]
{
let errno = unsafe { *libc::__error() };
-(errno as isize)
}
#[cfg(windows)]
{
let last_error = unsafe { windows_sys::Win32::Foundation::GetLastError() };
-(last_error as isize)
}
}
}
}};
($fn: ident ( $($arg: expr),* $(,)* ) ) => {{
let result = syscall!(raw $fn($($arg),*));
if result >= 0 {
Ok(result as i32)
} else {
Err(std::io::Error::from_raw_os_error(-(result as i32)))
}
}};
}
#[cfg(test)]
pub(crate) mod __internal {
pub const MAX_OP_SIZE: usize = 144;
}
macro_rules! assert_op_max_size {
($op_type:ty) => {
#[test]
fn test_op_size() {
let size = std::mem::size_of::<$op_type>();
assert!(
size <= crate::macros::__internal::MAX_OP_SIZE,
concat!(
"\n\n",
"╔════════════════════════════════════════════════════════════╗\n",
"║ OPERATION SIZE LIMIT EXCEEDED ║\n",
"╠════════════════════════════════════════════════════════════╣\n",
"║ Operation: ",
stringify!($op_type),
"\n",
"║ Size: {} bytes\n",
"║ Maximum allowed: {} bytes\n",
"║\n",
"║ This operation exceeds the bump allocator inline storage\n",
"║ and will cause heap allocations.\n",
"║\n",
"║ To fix this:\n",
"║ 1. Reduce operation struct size (preferred), OR\n",
"║ 2. Increase MAX_OP_SIZE in macros.rs if intentional\n",
"╚════════════════════════════════════════════════════════════╝\n"
),
size,
crate::macros::__internal::MAX_OP_SIZE
);
}
};
}