Skip to main content

dyn_safe/
lib.rs

1/*!
2# `::dyn_safe`
3
4[![Repository](https://img.shields.io/badge/repository-GitHub-brightgreen.svg)](https://github.com/danielhenrymantilla/dyn_safe.rs)
5[![Latest version](https://img.shields.io/crates/v/dyn_safe.svg)](https://crates.io/crates/dyn_safe)
6[![Documentation](https://docs.rs/dyn_safe/badge.svg)](https://docs.rs/dyn_safe)
7[![MSRV](https://img.shields.io/badge/MSRV-1.42.0-white)](https://gist.github.com/danielhenrymantilla/8e5b721b3929084562f8f65668920c33)
8[![unsafe forbidden](https://img.shields.io/badge/unsafe-forbidden-success.svg)](https://github.com/rust-secure-code/safety-dance/)
9[![License](https://img.shields.io/crates/l/dyn_safe.svg)](https://github.com/danielhenrymantilla/dyn_safe.rs/blob/master/LICENSE-ZLIB)
10<!-- [![CI](https://github.com/danielhenrymantilla/dyn_safe.rs/workflows/CI/badge.svg)](https://github.com/danielhenrymantilla/dyn_safe.rs/actions) -->
11
12### Take control of the Semver hazard of the `dyn` safety of your traits!
13
14##### Usage
15
16 1. `cargo add dyn_safe`, or add the following to your `Cargo.toml` file:
17
18    ```toml
19    [dependencies]
20    dyn_safe = "x.y.z"
21    ```
22
23      - where you can find the version using `cargo search dyn_safe`
24
25 1. Add the following to your `lib.rs` file:
26
27    ```rust,ignore
28    #[macro_use]
29    extern crate dyn_safe;
30    ```
31
32 1. Use `#[dyn_safe(true)]` or `#[dyn_safe(false)]` to, respectively,
33    assert that the trait object is `dyn`-safe or that the trait
34    object should not be `dyn`-safe.
35
36      - ```rust,compile_fail
37        # use ::dyn_safe::dyn_safe; macro_rules! ignore {($($t:tt)*) => ()} ignore! {
38        #[macro_use]
39        extern crate dyn_safe;
40        # }
41
42        #[dyn_safe(true)]
43        pub trait Foo {
44            fn whoops ();
45        }
46        ```
47
48        ```rust
49        # use ::dyn_safe::dyn_safe; macro_rules! ignore {($($t:tt)*) => ()} ignore! {
50        #[macro_use]
51        extern crate dyn_safe;
52        # }
53
54        #[dyn_safe(false)]
55        pub trait Foo {
56            fn ok ();
57        }
58        ```
59
60      - ```rust,compile_fail
61        # use ::dyn_safe::dyn_safe; macro_rules! ignore {($($t:tt)*) => ()} ignore! {
62        #[macro_use]
63        extern crate dyn_safe;
64        # }
65
66        #[dyn_safe(false)]
67        pub trait Foo {
68            // …
69        }
70
71        let _: dyn Foo; // Whoops
72        ```
73
74        ```rust
75        # use ::dyn_safe::dyn_safe; macro_rules! ignore {($($t:tt)*) => ()} ignore! {
76        #[macro_use]
77        extern crate dyn_safe;
78        # }
79
80        #[dyn_safe(true)]
81        pub trait Foo {
82            // …
83        }
84
85        let _: dyn Foo; // Ok
86        ```
87*/
88
89#![no_std]
90#![forbid(unsafe_code)]
91
92/// For a bug when cross-compiling
93extern crate proc_macros;
94
95pub use ::proc_macros::dyn_safe;
96
97mod __ { pub struct Hidden(()); }
98
99/// "Marker" (super-)trait used to opt-out of object safety.
100pub
101trait NotObjectSafe {
102    #[doc(hidden)]
103    fn __opted_out_of_object_safety<T>(_: __::Hidden) {
104        impl<T : ?::core::marker::Sized> NotObjectSafe for T {}
105    }
106}