-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from sonodima/chore/update-examples
update examples with ret_val support
- Loading branch information
Showing
4 changed files
with
23 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters