Skip to main content

bz2_rs/
lib.rs

1#![no_std]
2
3extern crate libbz2_rs_sys;
4
5use core::panic::PanicInfo;
6pub use libbz2_rs_sys::*;
7
8#[cfg(feature = "stdio")]
9struct StderrWritter;
10
11#[cfg(feature = "stdio")]
12impl core::fmt::Write for StderrWritter {
13    fn write_str(&mut self, s: &str) -> core::fmt::Result {
14        use core::ffi::c_void;
15        use libc::write;
16
17        unsafe { write(2, s.as_ptr() as *const c_void, s.len() as _) };
18
19        Ok(())
20    }
21}
22
23#[panic_handler]
24fn panic_handler(_info: &PanicInfo) -> ! {
25    #[cfg(feature = "stdio")]
26    {
27        use core::fmt::Write;
28        use libc::exit;
29
30        let _ = StderrWritter.write_str("libbzip2-rs: internal error:\n");
31        let _ = StderrWritter.write_fmt(format_args!("{}", _info.message()));
32
33        unsafe {
34            exit(3);
35        }
36    }
37
38    #[cfg(not(feature = "stdio"))]
39    {
40        use core::sync::atomic::Ordering;
41
42        extern "C" {
43            fn bz_internal_error(errcode: core::ffi::c_int);
44        }
45
46        // If the panic was triggered by handle_assert_failure ASSERT_CODE will contain the
47        // assertion code. Otherwise it will contain -1.
48        unsafe { bz_internal_error(ASSERT_CODE.load(Ordering::Relaxed)) }
49        loop {}
50    }
51}