Skip to main content

portable_atomic_util/
lib.rs

1// SPDX-License-Identifier: Apache-2.0 OR MIT
2
3/*!
4<!-- Note: Document from sync-markdown-to-rustdoc:start through sync-markdown-to-rustdoc:end
5     is synchronized from README.md. Any changes to that range are not preserved. -->
6<!-- tidy:sync-markdown-to-rustdoc:start -->
7
8Synchronization primitives built with [portable-atomic].
9
10- Provide `Arc`. (optional, requires the `std` or `alloc` feature)
11- Provide `task::Wake`. (optional, requires the `std` or `alloc` feature)
12<!-- - Provide generic `Atomic<T>` type. (optional, requires the `generic` feature) -->
13
14See [portable-atomic#1] for other primitives being considered for addition to this crate.
15
16This crate was originally [part of the portable-atomic repository](https://github.com/taiki-e/portable-atomic/tree/cbbee0c0d202a5944f7d66aaafaac6ed76e6f599/portable-atomic-util) and was extracted into its own repository.
17
18## Optional features
19
20- **`std`**<br>
21  Use `std`.
22
23  Note:
24  - This implicitly enables the `alloc` feature.
25
26- **`alloc`**<br>
27  Use `alloc`.
28
29  Note:
30  - The MSRV when this feature is enabled and the `std` feature is *not* enabled is Rust 1.36 that `alloc` crate stabilized.
31
32<!-- TODO: https://github.com/taiki-e/portable-atomic/issues/1
33- **`generic`**<br>
34  Provides generic `Atomic<T>` type.
35-->
36
37[portable-atomic]: https://github.com/taiki-e/portable-atomic
38[portable-atomic#1]: https://github.com/taiki-e/portable-atomic/issues/1
39
40## Optional cfg
41
42One of the ways to enable cfg is to set [rustflags in the cargo config](https://doc.rust-lang.org/cargo/reference/config.html#targettriplerustflags):
43
44```toml
45# .cargo/config.toml
46[target.<target>]
47rustflags = ["--cfg", "portable_atomic_unstable_coerce_unsized"]
48```
49
50Or set environment variable:
51
52```sh
53RUSTFLAGS="--cfg portable_atomic_unstable_coerce_unsized" cargo ...
54```
55
56- <a name="optional-cfg-unstable-coerce-unsized"></a>**`--cfg portable_atomic_unstable_coerce_unsized`**<br>
57  Support coercing of `Arc<T>` to `Arc<U>` as in `std::sync::Arc`.
58
59  <!-- TODO: add coercing of `Weak<T>` to `Weak<U>` as well, with testing & documentation updates -->
60
61  This cfg requires Rust nightly because this coercing requires [unstable `CoerceUnsized` trait](https://doc.rust-lang.org/nightly/core/ops/trait.CoerceUnsized.html).
62
63  See [this issue comment](https://github.com/taiki-e/portable-atomic/issues/143#issuecomment-1866488569) for another known workaround.
64
65  **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.
66
67## Related Projects
68
69- [portable-atomic]: Portable atomic types including support for 128-bit atomics, atomic float, etc.
70- [atomic-maybe-uninit]: Atomic operations on potentially uninitialized integers.
71- [atomic-memcpy]: Byte-wise atomic memcpy.
72
73[atomic-maybe-uninit]: https://github.com/taiki-e/atomic-maybe-uninit
74[atomic-memcpy]: https://github.com/taiki-e/atomic-memcpy
75
76<!-- tidy:sync-markdown-to-rustdoc:end -->
77*/
78
79#![no_std]
80#![doc(test(
81    no_crate_inject,
82    attr(allow(
83        dead_code,
84        unused_variables,
85        clippy::undocumented_unsafe_blocks,
86        clippy::unused_trait_names,
87    ))
88))]
89#![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
90#![cfg_attr(portable_atomic_no_unsafe_op_in_unsafe_fn, allow(unused_unsafe))]
91#![warn(
92    // Lints that may help when writing public library.
93    missing_debug_implementations,
94    missing_docs,
95    clippy::alloc_instead_of_core,
96    clippy::exhaustive_enums,
97    clippy::exhaustive_structs,
98    clippy::impl_trait_in_params,
99    clippy::std_instead_of_alloc,
100    clippy::std_instead_of_core,
101    // clippy::missing_inline_in_public_items,
102)]
103#![cfg_attr(portable_atomic_no_strict_provenance, allow(unstable_name_collisions))]
104#![allow(clippy::inline_always)]
105// docs.rs only (cfg is enabled by docs.rs, not build script)
106#![cfg_attr(docsrs, feature(doc_cfg))]
107#![cfg_attr(docsrs, doc(auto_cfg = false))]
108// Enable custom unsized coercions if the user explicitly opts-in to unstable cfg
109#![cfg_attr(portable_atomic_unstable_coerce_unsized, feature(coerce_unsized, unsize))]
110
111#[cfg(all(feature = "alloc", not(portable_atomic_no_alloc)))]
112extern crate alloc;
113#[cfg(feature = "std")]
114extern crate std;
115#[cfg(all(feature = "std", portable_atomic_no_alloc))]
116extern crate std as alloc;
117
118#[macro_use]
119mod utils;
120
121#[cfg(any(all(feature = "alloc", not(portable_atomic_no_alloc)), feature = "std"))]
122#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
123mod arc;
124#[cfg(any(all(feature = "alloc", not(portable_atomic_no_alloc)), feature = "std"))]
125pub use self::arc::{Arc, Weak};
126
127#[cfg(not(portable_atomic_no_futures_api))]
128#[cfg(any(all(feature = "alloc", not(portable_atomic_no_alloc)), feature = "std"))]
129#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
130pub mod task;