Add an example

This commit is contained in:
Gary Guo 2021-08-26 11:06:45 +01:00
parent af45cc8701
commit 2fc17fc631
3 changed files with 61 additions and 1 deletions

View File

@ -5,7 +5,7 @@ authors = ["Gary Guo <gary@garyguo.net>"]
edition = "2018" edition = "2018"
[workspace] [workspace]
members = ["cdylib"] members = ["cdylib", "example"]
[dependencies] [dependencies]
gimli = { version = "0.25.0", default-features = false, features = ["read"] } gimli = { version = "0.25.0", default-features = false, features = ["read"] }

8
example/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "example"
version = "0.1.0"
edition = "2018"
[dependencies]
unwind = { path = "../", features = ["system-alloc", "personality", "panic-handler"] }
libc = "0.2"

52
example/src/main.rs Normal file
View File

@ -0,0 +1,52 @@
#![no_std]
#![feature(start)]
#![feature(default_alloc_error_handler)]
extern crate alloc;
extern crate unwind;
use alloc::{borrow::ToOwned, string::String};
use unwind::print::*;
#[link(name = "c")]
extern "C" {}
struct PrintOnDrop(String);
impl Drop for PrintOnDrop {
fn drop(&mut self) {
println!("dropped: {:?}", self.0);
}
}
struct PanicOnDrop;
impl Drop for PanicOnDrop {
fn drop(&mut self) {
panic!("panic on drop");
}
}
fn foo() {
panic!("panic");
}
fn bar() {
let _p = PrintOnDrop("string".to_owned());
foo()
}
#[start]
fn main(_argc: isize, _argv: *const *const u8) -> isize {
unwind::panic::catch_unwind(|| {
let _ = unwind::panic::catch_unwind(|| {
bar();
println!("done");
});
println!("caught");
let _p = PanicOnDrop;
foo();
0
})
.unwrap_or(101)
}