use platify::{sys_enum, sys_function, sys_struct, sys_trait_function};
use std::cell::RefCell;
struct SimpleMath;
impl SimpleMath {
#[sys_function]
pub fn add(&self, a: i32, b: i32) -> i32;
fn add_impl(&self, a: i32, b: i32) -> i32 {
a + b
}
#[sys_function]
pub fn increment(&self, counter: &RefCell<i32>);
fn increment_impl(&self, counter: &RefCell<i32>) {
*counter.borrow_mut() += 1;
}
}
#[test]
fn test_basic_dispatch() {
let math = SimpleMath;
assert_eq!(math.add(10, 20), 30);
let counter = RefCell::new(5);
math.increment(&counter);
assert_eq!(*counter.borrow(), 6);
}
struct AdvancedHandler;
impl AdvancedHandler {
#[sys_function]
fn wrap_and_trim<'a, T: Clone>(&self, item: T, mut text: &'a str) -> (T, &'a str);
fn wrap_and_trim_impl<'a, T: Clone>(&self, item: T, text: &'a str) -> (T, &'a str) {
(item, text.trim())
}
}
#[test]
fn test_advanced_signatures() {
let handler = AdvancedHandler;
let (item, text) = handler.wrap_and_trim(42_u64, " hello ");
assert_eq!(item, 42);
assert_eq!(text, "hello");
}
struct SystemLowLevel;
impl SystemLowLevel {
#[sys_function]
async fn get_code(&self) -> u8;
async fn get_code_impl(&self) -> u8 {
42
}
#[sys_function]
unsafe fn raw_access(&self) -> bool;
unsafe fn raw_access_impl(&self) -> bool {
true
}
}
#[tokio::test]
async fn test_async_and_unsafe() {
let sys = SystemLowLevel;
assert_eq!(sys.get_code().await, 42);
assert!(unsafe { sys.raw_access() });
}
#[sys_struct(traits(Send, Sync))]
struct ThreadSafeData<T: Send + Sync> {
inner: T,
}
#[sys_enum(traits(Clone, Copy))]
#[derive(Clone, Copy)]
enum SimpleStatus {
Ok,
Error,
}
#[test]
fn test_trait_assertions() {
let _data = ThreadSafeData { inner: 100 };
let _status = SimpleStatus::Ok;
}
trait CrossPlatformDevice {
#[sys_trait_function(include(linux, macos))]
fn unix_id(&self) -> u32;
#[sys_trait_function(include(windows))]
fn win_id(&self) -> u32;
}
struct MyDevice;
impl CrossPlatformDevice for MyDevice {
#[cfg(unix)]
fn unix_id(&self) -> u32 {
1
}
#[cfg(windows)]
fn win_id(&self) -> u32 {
2
}
}
#[test]
fn test_trait_gating() {
let dev = MyDevice;
#[cfg(unix)]
assert_eq!(dev.unix_id(), 1);
#[cfg(windows)]
assert_eq!(dev.win_id(), 2);
}
mod internal {
use platify::sys_function;
pub struct Hidden;
impl Hidden {
#[sys_function]
pub(crate) fn internal_call(&self) -> bool;
fn internal_call_impl(&self) -> bool {
true
}
}
}
#[test]
fn test_visibility() {
let h = internal::Hidden;
assert!(h.internal_call());
}
struct LogicTest;
impl LogicTest {
#[sys_function(exclude(windows, macos))]
fn only_linux(&self) -> bool;
fn only_linux_impl(&self) -> bool {
true
}
}
#[test]
fn test_exclusion_logic() {
let l = LogicTest;
#[cfg(target_os = "linux")]
assert!(l.only_linux());
}