Skip to content

Commit

Permalink
Merge pull request #22 from sonodima/chore/update-examples
Browse files Browse the repository at this point in the history
update examples with ret_val support
  • Loading branch information
sonodima authored Oct 6, 2024
2 parents 5543522 + 8258791 commit 763e09e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
40 changes: 23 additions & 17 deletions examples/functions.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,45 @@
const INVALID_PTR: *mut i32 = core::mem::align_of::<i32>() 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);
}
}
1 change: 0 additions & 1 deletion examples/minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const INVALID_PTR: *mut i32 = core::mem::align_of::<i32>() as _;


fn with_propagation() -> Result<(), microseh::Exception> {
// ...

Expand Down
1 change: 0 additions & 1 deletion examples/raii.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const INVALID_PTR: *mut i32 = core::mem::align_of::<i32>() as _;


struct Resource {
data: i32,
}
Expand Down
1 change: 0 additions & 1 deletion examples/registers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const INVALID_PTR: *mut i32 = core::mem::align_of::<i32>() as _;


fn main() {
if let Err(ex) = microseh::try_seh(|| unsafe {
INVALID_PTR.read_volatile();
Expand Down

0 comments on commit 763e09e

Please sign in to comment.