Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update examples with ret_val support #22

Merged
merged 1 commit into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading