From 825879124a42a793b3df2a9774999abc858a8904 Mon Sep 17 00:00:00 2001 From: sonodima Date: Sun, 6 Oct 2024 10:31:39 +0200 Subject: [PATCH] update examples with ret_val support --- examples/functions.rs | 40 +++++++++++++++++++++++----------------- examples/minimal.rs | 1 - examples/raii.rs | 1 - examples/registers.rs | 1 - 4 files changed, 23 insertions(+), 20 deletions(-) diff --git a/examples/functions.rs b/examples/functions.rs index 1ace4a6..057cb57 100755 --- a/examples/functions.rs +++ b/examples/functions.rs @@ -1,39 +1,45 @@ const INVALID_PTR: *mut i32 = core::mem::align_of::() as _; +unsafe fn unsafe_func() { + INVALID_PTR.write_volatile(0); +} fn rust_func() { - unsafe { - INVALID_PTR.write_volatile(0); - } + unsafe { unsafe_func() }; } extern "system" fn system_func() { - unsafe { - INVALID_PTR.read_volatile(); - } + rust_func(); } fn main() { // You can pass in closures: - let mut ex = microseh::try_seh(|| unsafe { - INVALID_PTR.write_volatile(0); - }); - + let ex = microseh::try_seh(|| unsafe { INVALID_PTR.write_volatile(0) }); if let Err(ex) = ex { - println!("{:?}", ex); + println!("closure: {:?}", ex); } // Or functions: - ex = microseh::try_seh(rust_func); - + let ex = microseh::try_seh(rust_func); if let Err(ex) = ex { - println!("{:?}", ex); + println!("rust_func: {:?}", ex); } - // And if you want to use it with FFI: - ex = microseh::try_seh(|| system_func()); + // But if you want to use it with FFI: + let ex = microseh::try_seh(|| system_func()); + if let Err(ex) = ex { + println!("system_func: {:?}", ex); + } + // Or you want to call an unsafe function: + let ex = microseh::try_seh(|| unsafe { unsafe_func() }); if let Err(ex) = ex { - println!("{:?}", ex); + println!("unsafe_func: {:?}", ex); + } + + // And you can also pass any return value: + let ex = microseh::try_seh(|| 1337); + if let Ok(val) = ex { + println!("ret_val: {}", val); } } diff --git a/examples/minimal.rs b/examples/minimal.rs index dea2192..0eedf0f 100755 --- a/examples/minimal.rs +++ b/examples/minimal.rs @@ -2,7 +2,6 @@ const INVALID_PTR: *mut i32 = core::mem::align_of::() as _; - fn with_propagation() -> Result<(), microseh::Exception> { // ... diff --git a/examples/raii.rs b/examples/raii.rs index 772f430..f1196fc 100755 --- a/examples/raii.rs +++ b/examples/raii.rs @@ -1,6 +1,5 @@ const INVALID_PTR: *mut i32 = core::mem::align_of::() as _; - struct Resource { data: i32, } diff --git a/examples/registers.rs b/examples/registers.rs index eb971a1..e333911 100755 --- a/examples/registers.rs +++ b/examples/registers.rs @@ -2,7 +2,6 @@ const INVALID_PTR: *mut i32 = core::mem::align_of::() as _; - fn main() { if let Err(ex) = microseh::try_seh(|| unsafe { INVALID_PTR.read_volatile();