musli/
no_std.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//! Trait fills for `#[no_std]` environments.
//!
//! * [`ToOwned`] - if the `alloc` feature is enabled, this is an alias for
//!   `alloc::borrow::ToOwned`.

#[doc(inline)]
pub use musli_core::no_std::ToOwned;

/// A somewhat portable, but also noisy abort implementation for no_std
/// environments.
///
/// While this should ultimately cause the process to abort, it will first cause
/// the process to panic and report it through the panic hook.
#[cold]
#[cfg(not(feature = "std"))]
pub(crate) fn abort(s: &'static str) -> ! {
    struct Abort;

    // A panic during an unwinding drop leads to an abort.
    impl Drop for Abort {
        #[inline(always)]
        fn drop(&mut self) {
            panic!()
        }
    }

    let _a = Abort;
    panic!("{s}")
}

#[cfg(feature = "std")]
#[cold]
pub(crate) fn abort(_: &'static str) -> ! {
    ::std::process::abort();
}