Skip to content

Commit

Permalink
Add an example using c-gull and std::unwind::catch_unwind.
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfishcode committed Oct 20, 2024
1 parent be1cdad commit cb6562c
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 1 deletion.
2 changes: 2 additions & 0 deletions example-crates/c-gull-unwinding/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
Cargo.lock
21 changes: 21 additions & 0 deletions example-crates/c-gull-unwinding/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "c-gull-unwinding"
version = "0.0.0"
edition = "2021"
publish = false

[dependencies.libc]
path = "../../c-gull"
default-features = false
features = [
"take-charge",
"std",
"thread",
"call-main",
"malloc-via-crates",
"threadsafe-setenv"
]
package = "c-gull"

# This is just an example crate, and not part of the c-ward workspace.
[workspace]
6 changes: 6 additions & 0 deletions example-crates/c-gull-unwinding/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
This crate demonstrates the use of c-gull in "take-charge" mode, and includes
unwinding panic support.

It's the same as [c-gull-example], but it performs an unwind.

[c-gull-example]: https://github.com/sunfishcode/c-ward/tree/main/example-crates/c-gull-example#readme
4 changes: 4 additions & 0 deletions example-crates/c-gull-unwinding/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
// Pass -nostartfiles to the linker.
println!("cargo:rustc-link-arg=-nostartfiles");
}
19 changes: 19 additions & 0 deletions example-crates/c-gull-unwinding/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//! A simple example using `std::panic::catch_unwind`.
fn main() {
// Panic and catch it.
std::panic::catch_unwind(|| call_do_panic()).unwrap_err();

println!("Hello, world!");
unsafe {
libc::printf("Hello world using libc `printf`!\n\0".as_ptr().cast());
}
}

fn call_do_panic() {
do_panic()
}

fn do_panic() {
panic!("catch me!");
}
2 changes: 1 addition & 1 deletion example-crates/c-scape-unwinding/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ and `no_std`, and includes unwinding panic support.

It's the same as [c-scape-example], but enables the "eh-personality" and
"panic-handler" features instead of defining stub `#[panic_handler]` and
`#[lang = "eh_personality"]` functions, and performs an unwind.
`#[lang = "eh_personality"]` functions, and it performs an unwind.

[c-scape-example]: https://github.com/sunfishcode/c-ward/tree/main/example-crates/c-scape-example#readme
12 changes: 12 additions & 0 deletions tests/example_crates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ fn example_crate_c_scape_unwinding() {
);
}

#[test]
fn example_crate_c_gull_unwinding() {
test_crate(
"c-gull-unwinding",
&[],
&[("RUST_BACKTRACE", "0")],
"Hello, world!\nHello world using libc `printf`!\n",
"thread 'main' panicked at src/main.rs:18:5:\ncatch me!\nnote: run with `RUST_BACKTRACE=1` environment variable to display a backtrace\n",
None,
);
}

#[test]
fn example_crate_dns() {
test_crate(
Expand Down

0 comments on commit cb6562c

Please sign in to comment.