create rust implementation

This commit is contained in:
Quinn
2025-04-12 20:43:21 +02:00
committed by Quinn
parent 1a3f555fca
commit 87117931f7
3 changed files with 49 additions and 7 deletions

33
src/error.rs Normal file
View File

@@ -0,0 +1,33 @@
unsafe extern "C" {
pub fn debug(fmt: *const u8, ...);
pub fn info(fmt: *const u8, ...);
pub fn warn(fmt: *const u8, ...);
pub fn error(fmt: *const u8, ...);
pub fn fatal(fmt: *const u8, ...);
}
#[macro_export]
macro_rules! debug {
($fmt:expr) => {{ $crate::error::debug($fmt.as_ptr()); }};
($fmt:expr, $($arg:expr),*) => {{ $crate::error::debug($fmt.as_ptr(), ($($arg),*)); }};
}
#[macro_export]
macro_rules! info {
($fmt:expr) => {{ $crate::error::info($fmt.as_ptr()); }};
($fmt:expr, $($arg:expr),*) => {{ $crate::error::info($fmt.as_ptr(), ($($arg),*)); }};
}
#[macro_export]
macro_rules! warn {
($fmt:expr) => {{ $crate::error::warn($fmt.as_ptr()); }};
($fmt:expr, $($arg:expr),*) => {{ $crate::error::warn($fmt.as_ptr(), ($($arg),*)); }};
}
#[macro_export]
macro_rules! error {
($fmt:expr) => {{ $crate::error::error($fmt.as_ptr()); }};
($fmt:expr, $($arg:expr),*) => {{ $crate::error::error($fmt.as_ptr(), ($($arg),*)); }};
}
#[macro_export]
macro_rules! fatal {
($fmt:expr) => {{ $crate::error::fatal($fmt.as_ptr()); }};
($fmt:expr, $($arg:expr),*) => {{ $crate::error::fatal($fmt.as_ptr(), ($($arg),*)); }};
}

View File

@@ -6,8 +6,16 @@ fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {} loop {}
} }
#[unsafe(no_mangle)] // contains all publicly facing functions
pub unsafe extern "C" fn get_str() -> *const u8 {
let str = "Hello, World\0"; mod error;
return str.as_ptr();
pub extern "C" fn test() {
unsafe {
debug!("%s", "hi");
info!("%s", "hi");
warn!("%s", "hi");
error!("%s", "hi");
fatal!("%s", "hi");
}
} }

View File

@@ -1,8 +1,9 @@
#include <stdio.h> #include "error.h"
extern char const* get_str(void); extern void test(void);
int main(int argc, char** argv) { int main(int argc, char** argv) {
(void)argc, (void)argv; (void)argc, (void)argv;
printf("%s\n", get_str()); test();
debug("owo");
} }