not_send/lib.rs
1#![cfg_attr(not(test), no_std)]
2
3use core::marker::PhantomData;
4
5#[allow(rustdoc::invalid_rust_codeblocks)]
6/// Types with PhantomNotSend won't automatically implement Send
7///
8/// # Example
9///
10/// ```compile_fail
11/// let not_send = not_send::PhantomNotSend;
12/// std::thread::spawn(move || {
13/// drop(not_send);
14/// }).join().unwrap();
15/// ```
16///
17/// ```ignore
18/// error[E0277]: `*mut ()` cannot be sent between threads safely
19/// --> src/lib.rs:13:20
20/// |
21/// 6 | std::thread::spawn(move || {
22/// | ------------------ ^------
23/// | | |
24/// | _|__________________within this `{closure@src/lib.rs:6:20: 6:27}`
25/// | | |
26/// | | required by a bound introduced by this call
27/// 7 | | drop(not_send);
28/// 8 | | }).join().unwrap();
29/// | |_^ `*mut ()` cannot be sent between threads safely
30/// |
31/// = help: within `{closure@src/lib.rs:6:20: 6:27}`, the trait `Send` is not implemented for `*mut ()`, which is required by `{closure@src/lib.rs:6:20: 6:27}: Send`
32/// note: required because it appears within the type `PhantomData<*mut ()>`
33/// --> /Users/bajix/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/marker.rs:753:12
34/// |
35/// 753 | pub struct PhantomData<T: ?Sized>;
36/// | ^^^^^^^^^^^
37/// note: required because it appears within the type `PhantomNotSend`
38/// --> /Users/bajix/Projects/not-send/src/lib.rs:57:12
39/// |
40/// 57 | pub struct PhantomNotSend {
41/// | ^^^^^^^^^^^^^^
42/// note: required because it's used within this closure
43/// --> src/lib.rs:13:20
44/// |
45/// 6 | std::thread::spawn(move || {
46/// | ^^^^^^^
47/// note: required by a bound in `spawn`
48/// --> /Users/bajix/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/std/src/thread/mod.rs:707:8
49/// |
50/// 704 | pub fn spawn<F, T>(f: F) -> JoinHandle<T>
51/// | ----- required by a bound in this function
52/// ...
53/// 707 | F: Send + 'static,
54/// | ^^^^ required by this bound in `spawn`
55/// ```
56#[derive(Default, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
57pub struct PhantomNotSend {
58 _marker: PhantomData<*mut ()>,
59}
60
61#[allow(non_upper_case_globals)]
62pub const PhantomNotSend: PhantomNotSend = PhantomNotSend {
63 _marker: PhantomData,
64};
65
66unsafe impl Sync for PhantomNotSend {}