unwind_unsafe/
lib.rs

1// Copyright 2024 Daniel Fox Franke
2// SPDX-License-Identifier: MIT-0
3#![no_std]
4#![forbid(unsafe_code)]
5
6//! This crate provides zero-sized marker types which do not implement
7//! [`UnwindSafe`] or [`RefUnwindSafe`]. They can be added to types to prevent
8//! the respective auto traits from being derived.
9//!
10//! These types can be instantiated via their [`Default`] implementation or from
11//! their associated constant `DEFAULT`.
12//!
13//! This crate has no dependencies, no unsafe code, and is `no_std` by default.
14
15use core::fmt::Debug;
16use core::panic::{RefUnwindSafe, UnwindSafe};
17
18#[allow(unused)]
19trait NoImplementers {}
20
21#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
22struct NotUwsInner<T>(T);
23
24#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
25struct NotRuwsInner<T>(T);
26
27impl<T> UnwindSafe for NotUwsInner<T> where T: NoImplementers {}
28impl<T> RefUnwindSafe for NotRuwsInner<T> where T: NoImplementers {}
29
30/// A marker type which does not implement [`UnwindSafe`].
31///
32/// If a type contains a `PhantomUnwindUnsafe`, it will not implement
33/// `UnwindSafe` by default.
34#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
35pub struct PhantomUnwindUnsafe(NotUwsInner<()>);
36
37/// A marker type which does not implement [`RefUnwindSafe`].
38///
39/// If a type contains a `PhantomRefUnwindUnsafe`, it will not implement
40/// `RefUnwindSafe` by default.
41#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
42pub struct PhantomRefUnwindUnsafe(NotRuwsInner<()>);
43
44impl PhantomUnwindUnsafe {
45    pub const DEFAULT: Self = Self(NotUwsInner(()));
46}
47
48impl PhantomRefUnwindUnsafe {
49    pub const DEFAULT: Self = Self(NotRuwsInner(()));
50}
51
52impl Debug for PhantomUnwindUnsafe {
53    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
54        f.write_str("PhantomUnwindUnsafe")
55    }
56}
57
58impl Debug for PhantomRefUnwindUnsafe {
59    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
60        f.write_str("PhantomRefUnwindUnsafe")
61    }
62}