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- **`serde`**<br>
30 Implement `serde::{Serialize, Deserialize}` for `Arc`.
31
32 Note:
33 - The MSRV when this feature is enabled is the one coming from serde, as of now it's Rust 1.56
34
35<!-- TODO: https://github.com/taiki-e/portable-atomic/issues/1
36- **`generic`**<br>
37 Provides generic `Atomic<T>` type.
38-->
39
40[portable-atomic]: https://github.com/taiki-e/portable-atomic
41[portable-atomic#1]: https://github.com/taiki-e/portable-atomic/issues/1
42
43## Optional cfg
44
45One of the ways to enable cfg is to set [rustflags in the cargo config](https://doc.rust-lang.org/cargo/reference/config.html#targettriplerustflags):
46
47```toml
48# .cargo/config.toml
49[target.<target>]
50rustflags = ["--cfg", "portable_atomic_unstable_coerce_unsized"]
51```
52
53Or set environment variable:
54
55```sh
56RUSTFLAGS="--cfg portable_atomic_unstable_coerce_unsized" cargo ...
57```
58
59- <a name="optional-cfg-unstable-coerce-unsized"></a>**`--cfg portable_atomic_unstable_coerce_unsized`**<br>
60 Support coercing of `Arc<T>` to `Arc<U>` as in `std::sync::Arc`.
61
62 <!-- TODO: add coercing of `Weak<T>` to `Weak<U>` as well, with testing & documentation updates -->
63
64 This cfg requires Rust nightly because this coercing requires [unstable `CoerceUnsized` trait](https://doc.rust-lang.org/nightly/core/ops/trait.CoerceUnsized.html).
65
66 See [this issue comment](https://github.com/taiki-e/portable-atomic/issues/143#issuecomment-1866488569) for another known workaround.
67
68 **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.
69
70## Related Projects
71
72- [portable-atomic]: Portable atomic types including support for 128-bit atomics, atomic float, etc.
73- [atomic-maybe-uninit]: Atomic operations on potentially uninitialized integers.
74- [atomic-memcpy]: Byte-wise atomic memcpy.
75
76[atomic-maybe-uninit]: https://github.com/taiki-e/atomic-maybe-uninit
77[atomic-memcpy]: https://github.com/taiki-e/atomic-memcpy
78
79<!-- tidy:sync-markdown-to-rustdoc:end -->
80*/
81
82#![no_std]
83#![doc(test(
84 no_crate_inject,
85 attr(allow(
86 dead_code,
87 unused_variables,
88 clippy::undocumented_unsafe_blocks,
89 clippy::unused_trait_names,
90 ))
91))]
92#![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
93#![cfg_attr(portable_atomic_no_unsafe_op_in_unsafe_fn, allow(unused_unsafe))]
94#![warn(
95 // Lints that may help when writing public library.
96 missing_debug_implementations,
97 missing_docs,
98 clippy::alloc_instead_of_core,
99 clippy::exhaustive_enums,
100 clippy::exhaustive_structs,
101 clippy::impl_trait_in_params,
102 clippy::std_instead_of_alloc,
103 clippy::std_instead_of_core,
104 // clippy::missing_inline_in_public_items,
105)]
106#![cfg_attr(portable_atomic_no_strict_provenance, allow(unstable_name_collisions))]
107#![allow(clippy::inline_always)]
108// docs.rs only (cfg is enabled by docs.rs, not build script)
109#![cfg_attr(docsrs, feature(doc_cfg))]
110#![cfg_attr(docsrs, doc(auto_cfg = false))]
111// Enable custom unsized coercions if the user explicitly opts-in to unstable cfg
112#![cfg_attr(portable_atomic_unstable_coerce_unsized, feature(coerce_unsized, unsize))]
113
114#[cfg(feature = "alloc")]
115extern crate alloc;
116#[cfg(feature = "std")]
117extern crate std;
118
119#[macro_use]
120mod utils;
121
122#[cfg(feature = "alloc")]
123mod arc;
124#[cfg(feature = "alloc")]
125#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
126pub use self::arc::{Arc, Weak};
127
128#[cfg(feature = "alloc")]
129#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
130pub mod task;