portable_atomic_util/
lib.rs

1// SPDX-License-Identifier: Apache-2.0 OR MIT
2
3/*!
4<!-- tidy:crate-doc:start -->
5Synchronization primitives built with [portable-atomic].
6
7- Provide `Arc`. (optional, requires the `std` or `alloc` feature)
8- Provide `task::Wake`. (optional, requires the `std` or `alloc` feature)
9<!-- - Provide generic `Atomic<T>` type. (optional, requires the `generic` feature) -->
10
11See [#1] for other primitives being considered for addition to this crate.
12
13## Optional features
14
15- **`std`**<br>
16  Use `std`.
17
18  Note:
19  - This implicitly enables the `alloc` feature.
20
21- **`alloc`**<br>
22  Use `alloc`.
23
24  Note:
25  - The MSRV when this feature is enabled and the `std` feature is *not* enabled is Rust 1.36 that `alloc` crate stabilized.
26
27<!-- TODO: https://github.com/taiki-e/portable-atomic/issues/1
28- **`generic`**<br>
29  Provides generic `Atomic<T>` type.
30-->
31
32[portable-atomic]: https://github.com/taiki-e/portable-atomic
33[#1]: https://github.com/taiki-e/portable-atomic/issues/1
34
35## Optional cfg
36
37One of the ways to enable cfg is to set [rustflags in the cargo config](https://doc.rust-lang.org/cargo/reference/config.html#targettriplerustflags):
38
39```toml
40# .cargo/config.toml
41[target.<target>]
42rustflags = ["--cfg", "portable_atomic_unstable_coerce_unsized"]
43```
44
45Or set environment variable:
46
47```sh
48RUSTFLAGS="--cfg portable_atomic_unstable_coerce_unsized" cargo ...
49```
50
51- <a name="optional-cfg-unstable-coerce-unsized"></a>**`--cfg portable_atomic_unstable_coerce_unsized`**<br>
52  Support coercing of `Arc<T>` to `Arc<U>` as in `std::sync::Arc`.
53
54  <!-- TODO: add coercing of `Weak<T>` to `Weak<U>` as well, with testing & documentation updates -->
55
56  This cfg requires Rust nightly because this coercing requires [unstable `CoerceUnsized` trait](https://doc.rust-lang.org/nightly/core/ops/trait.CoerceUnsized.html).
57
58  See [this issue comment](https://github.com/taiki-e/portable-atomic/issues/143#issuecomment-1866488569) for another known workaround.
59
60  **Note:** This cfg is unstable and outside of the normal semver guarantees and minor or patch versions of portable-atomic-util may make breaking changes to them at any time.
61
62<!-- tidy:crate-doc:end -->
63*/
64
65#![no_std]
66#![doc(test(
67    no_crate_inject,
68    attr(
69        deny(warnings, rust_2018_idioms, single_use_lifetimes),
70        allow(dead_code, unused_variables)
71    )
72))]
73#![cfg_attr(not(portable_atomic_no_unsafe_op_in_unsafe_fn), warn(unsafe_op_in_unsafe_fn))] // unsafe_op_in_unsafe_fn requires Rust 1.52
74#![cfg_attr(portable_atomic_no_unsafe_op_in_unsafe_fn, allow(unused_unsafe))]
75#![warn(
76    // Lints that may help when writing public library.
77    missing_debug_implementations,
78    missing_docs,
79    clippy::alloc_instead_of_core,
80    clippy::exhaustive_enums,
81    clippy::exhaustive_structs,
82    clippy::impl_trait_in_params,
83    // clippy::missing_inline_in_public_items,
84    clippy::std_instead_of_alloc,
85    clippy::std_instead_of_core,
86)]
87#![allow(clippy::inline_always)]
88// docs.rs only (cfg is enabled by docs.rs, not build script)
89#![cfg_attr(docsrs, feature(doc_cfg))]
90// Enable custom unsized coercions if the user explicitly opts-in to unstable cfg
91#![cfg_attr(portable_atomic_unstable_coerce_unsized, feature(coerce_unsized, unsize))]
92
93#[cfg(all(feature = "alloc", not(portable_atomic_no_alloc)))]
94extern crate alloc;
95#[cfg(feature = "std")]
96extern crate std;
97#[cfg(all(feature = "std", portable_atomic_no_alloc))]
98extern crate std as alloc;
99
100#[cfg(any(all(feature = "alloc", not(portable_atomic_no_alloc)), feature = "std"))]
101#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
102mod arc;
103#[cfg(any(all(feature = "alloc", not(portable_atomic_no_alloc)), feature = "std"))]
104pub use self::arc::{Arc, Weak};
105
106#[cfg(not(portable_atomic_no_futures_api))]
107#[cfg(any(all(feature = "alloc", not(portable_atomic_no_alloc)), feature = "std"))]
108#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
109pub mod task;