Merge tag 'x86-asm-2024-03-11' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
[linux-2.6-block.git] / rust / alloc / lib.rs
CommitLineData
057b8d25
MO
1// SPDX-License-Identifier: Apache-2.0 OR MIT
2
753dece8
MO
3//! # The Rust core allocation and collections library
4//!
5//! This library provides smart pointers and collections for managing
6//! heap-allocated values.
7//!
3ed03f4d 8//! This library, like core, normally doesn’t need to be used directly
753dece8
MO
9//! since its contents are re-exported in the [`std` crate](../std/index.html).
10//! Crates that use the `#![no_std]` attribute however will typically
11//! not depend on `std`, so they’d use this crate instead.
12//!
13//! ## Boxed values
14//!
15//! The [`Box`] type is a smart pointer type. There can only be one owner of a
16//! [`Box`], and the owner can decide to mutate the contents, which live on the
17//! heap.
18//!
19//! This type can be sent among threads efficiently as the size of a `Box` value
20//! is the same as that of a pointer. Tree-like data structures are often built
21//! with boxes because each node often has only one owner, the parent.
22//!
23//! ## Reference counted pointers
24//!
25//! The [`Rc`] type is a non-threadsafe reference-counted pointer type intended
26//! for sharing memory within a thread. An [`Rc`] pointer wraps a type, `T`, and
27//! only allows access to `&T`, a shared reference.
28//!
29//! This type is useful when inherited mutability (such as using [`Box`]) is too
30//! constraining for an application, and is often paired with the [`Cell`] or
31//! [`RefCell`] types in order to allow mutation.
32//!
33//! ## Atomically reference counted pointers
34//!
35//! The [`Arc`] type is the threadsafe equivalent of the [`Rc`] type. It
36//! provides all the same functionality of [`Rc`], except it requires that the
37//! contained type `T` is shareable. Additionally, [`Arc<T>`][`Arc`] is itself
38//! sendable while [`Rc<T>`][`Rc`] is not.
39//!
40//! This type allows for shared access to the contained data, and is often
41//! paired with synchronization primitives such as mutexes to allow mutation of
42//! shared resources.
43//!
44//! ## Collections
45//!
46//! Implementations of the most common general purpose data structures are
47//! defined in this library. They are re-exported through the
48//! [standard collections library](../std/collections/index.html).
49//!
50//! ## Heap interfaces
51//!
52//! The [`alloc`](alloc/index.html) module defines the low-level interface to the
53//! default global allocator. It is not compatible with the libc allocator API.
54//!
55//! [`Arc`]: sync
56//! [`Box`]: boxed
57//! [`Cell`]: core::cell
58//! [`Rc`]: rc
59//! [`RefCell`]: core::cell
60
ae6df65d
MO
61// To run alloc tests without x.py without ending up with two copies of alloc, Miri needs to be
62// able to "empty" this crate. See <https://github.com/rust-lang/miri-test-libstd/issues/4>.
e08ff622 63// rustc itself never sets the feature, so this line has no effect there.
ae6df65d
MO
64#![cfg(any(not(feature = "miri-test-libstd"), test, doctest))]
65//
753dece8
MO
66#![allow(unused_attributes)]
67#![stable(feature = "alloc", since = "1.36.0")]
68#![doc(
69 html_playground_url = "https://play.rust-lang.org/",
70 issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/",
71 test(no_crate_inject, attr(allow(unused_variables), deny(warnings)))
72)]
73#![doc(cfg_hide(
74 not(test),
75 not(any(test, bootstrap)),
76 any(not(feature = "miri-test-libstd"), test, doctest),
77 no_global_oom_handling,
78 not(no_global_oom_handling),
3ed03f4d
MO
79 not(no_rc),
80 not(no_sync),
753dece8
MO
81 target_has_atomic = "ptr"
82))]
768409cf
MO
83#![doc(rust_logo)]
84#![feature(rustdoc_internals)]
753dece8
MO
85#![no_std]
86#![needs_allocator]
753dece8
MO
87// Lints:
88#![deny(unsafe_op_in_unsafe_fn)]
3ed03f4d 89#![deny(fuzzy_provenance_casts)]
753dece8
MO
90#![warn(deprecated_in_future)]
91#![warn(missing_debug_implementations)]
92#![warn(missing_docs)]
93#![allow(explicit_outlives_requirements)]
89eed1ab 94#![warn(multiple_supertrait_upcastable)]
80fe9e51
MO
95#![allow(internal_features)]
96#![allow(rustdoc::redundant_explicit_links)]
753dece8
MO
97//
98// Library features:
89eed1ab
MO
99// tidy-alphabetical-start
100#![cfg_attr(not(no_global_oom_handling), feature(const_alloc_error))]
101#![cfg_attr(not(no_global_oom_handling), feature(const_btree_len))]
102#![cfg_attr(test, feature(is_sorted))]
103#![cfg_attr(test, feature(new_uninit))]
753dece8
MO
104#![feature(alloc_layout_extra)]
105#![feature(allocator_api)]
106#![feature(array_chunks)]
3ed03f4d 107#![feature(array_into_iter_constructors)]
753dece8
MO
108#![feature(array_methods)]
109#![feature(array_windows)]
89eed1ab 110#![feature(ascii_char)]
753dece8
MO
111#![feature(assert_matches)]
112#![feature(async_iterator)]
113#![feature(coerce_unsized)]
89eed1ab 114#![feature(const_align_of_val)]
753dece8 115#![feature(const_box)]
8909a80e 116#![cfg_attr(not(no_borrow), feature(const_cow_is_borrowed))]
89eed1ab 117#![feature(const_eval_select)]
753dece8 118#![feature(const_maybe_uninit_as_mut_ptr)]
89eed1ab 119#![feature(const_maybe_uninit_write)]
89eed1ab 120#![feature(const_pin)]
753dece8 121#![feature(const_refs_to_cell)]
89eed1ab
MO
122#![feature(const_size_of_val)]
123#![feature(const_waker)]
753dece8 124#![feature(core_intrinsics)]
3ed03f4d 125#![feature(core_panic)]
80fe9e51 126#![feature(deprecated_suggestion)]
753dece8 127#![feature(dispatch_from_dyn)]
3ed03f4d
MO
128#![feature(error_generic_member_access)]
129#![feature(error_in_core)]
753dece8
MO
130#![feature(exact_size_is_empty)]
131#![feature(extend_one)]
132#![feature(fmt_internals)]
133#![feature(fn_traits)]
134#![feature(hasher_prefixfree_extras)]
3ed03f4d 135#![feature(inline_const)]
753dece8
MO
136#![feature(inplace_iteration)]
137#![feature(iter_advance_by)]
3ed03f4d
MO
138#![feature(iter_next_chunk)]
139#![feature(iter_repeat_n)]
753dece8
MO
140#![feature(layout_for_ptr)]
141#![feature(maybe_uninit_slice)]
3ed03f4d
MO
142#![feature(maybe_uninit_uninit_array)]
143#![feature(maybe_uninit_uninit_array_transpose)]
753dece8
MO
144#![feature(pattern)]
145#![feature(ptr_internals)]
146#![feature(ptr_metadata)]
147#![feature(ptr_sub_ptr)]
148#![feature(receiver_trait)]
149#![feature(set_ptr_value)]
3ed03f4d
MO
150#![feature(sized_type_properties)]
151#![feature(slice_from_ptr_range)]
753dece8
MO
152#![feature(slice_group_by)]
153#![feature(slice_ptr_get)]
154#![feature(slice_ptr_len)]
155#![feature(slice_range)]
89eed1ab 156#![feature(std_internals)]
753dece8
MO
157#![feature(str_internals)]
158#![feature(strict_provenance)]
768409cf 159#![feature(trusted_fused)]
753dece8
MO
160#![feature(trusted_len)]
161#![feature(trusted_random_access)]
162#![feature(try_trait_v2)]
3ed03f4d 163#![feature(tuple_trait)]
753dece8
MO
164#![feature(unchecked_math)]
165#![feature(unicode_internals)]
166#![feature(unsize)]
3ed03f4d 167#![feature(utf8_chunks)]
89eed1ab 168// tidy-alphabetical-end
753dece8
MO
169//
170// Language features:
89eed1ab 171// tidy-alphabetical-start
c5fed8ce 172#![cfg_attr(not(test), feature(coroutine_trait))]
89eed1ab
MO
173#![cfg_attr(test, feature(panic_update_hook))]
174#![cfg_attr(test, feature(test))]
753dece8
MO
175#![feature(allocator_internals)]
176#![feature(allow_internal_unstable)]
177#![feature(associated_type_bounds)]
89eed1ab 178#![feature(c_unwind)]
753dece8 179#![feature(cfg_sanitize)]
753dece8 180#![feature(const_mut_refs)]
753dece8 181#![feature(const_precise_live_drops)]
89eed1ab 182#![feature(const_ptr_write)]
753dece8
MO
183#![feature(const_trait_impl)]
184#![feature(const_try)]
185#![feature(dropck_eyepatch)]
186#![feature(exclusive_range_pattern)]
187#![feature(fundamental)]
753dece8
MO
188#![feature(hashmap_internals)]
189#![feature(lang_items)]
753dece8 190#![feature(min_specialization)]
89eed1ab 191#![feature(multiple_supertrait_upcastable)]
753dece8
MO
192#![feature(negative_impls)]
193#![feature(never_type)]
89eed1ab 194#![feature(pointer_is_aligned)]
753dece8
MO
195#![feature(rustc_allow_const_fn_unstable)]
196#![feature(rustc_attrs)]
197#![feature(slice_internals)]
198#![feature(staged_api)]
3ed03f4d 199#![feature(stmt_expr_attributes)]
753dece8
MO
200#![feature(unboxed_closures)]
201#![feature(unsized_fn_params)]
3ed03f4d 202#![feature(with_negative_coherence)]
89eed1ab 203// tidy-alphabetical-end
753dece8
MO
204//
205// Rustdoc features:
206#![feature(doc_cfg)]
207#![feature(doc_cfg_hide)]
208// Technically, this is a bug in rustdoc: rustdoc sees the documentation on `#[lang = slice_alloc]`
209// blocks is for `&[T]`, which also has documentation using this feature in `core`, and gets mad
210// that the feature-gate isn't enabled. Ideally, it wouldn't check for the feature gate for docs
211// from other crates, but since this can only appear for lang items, it doesn't seem worth fixing.
212#![feature(intra_doc_pointers)]
213
214// Allow testing this library
215#[cfg(test)]
216#[macro_use]
217extern crate std;
218#[cfg(test)]
219extern crate test;
3ed03f4d
MO
220#[cfg(test)]
221mod testing;
753dece8
MO
222
223// Module with internal macros used by other modules (needs to be included before other modules).
057b8d25 224#[cfg(not(no_macros))]
753dece8
MO
225#[macro_use]
226mod macros;
227
228mod raw_vec;
229
230// Heaps provided for low-level allocation strategies
231
232pub mod alloc;
233
234// Primitive types using the heaps above
235
236// Need to conditionally define the mod from `boxed.rs` to avoid
237// duplicating the lang-items when building in test cfg; but also need
238// to allow code to have `use boxed::Box;` declarations.
239#[cfg(not(test))]
240pub mod boxed;
241#[cfg(test)]
242mod boxed {
243 pub use std::boxed::Box;
244}
8909a80e 245#[cfg(not(no_borrow))]
753dece8
MO
246pub mod borrow;
247pub mod collections;
3ed03f4d 248#[cfg(all(not(no_rc), not(no_sync), not(no_global_oom_handling)))]
753dece8 249pub mod ffi;
057b8d25 250#[cfg(not(no_fmt))]
753dece8 251pub mod fmt;
057b8d25 252#[cfg(not(no_rc))]
753dece8
MO
253pub mod rc;
254pub mod slice;
057b8d25 255#[cfg(not(no_str))]
753dece8 256pub mod str;
057b8d25 257#[cfg(not(no_string))]
753dece8 258pub mod string;
3ed03f4d 259#[cfg(all(not(no_rc), not(no_sync), target_has_atomic = "ptr"))]
753dece8 260pub mod sync;
3ed03f4d 261#[cfg(all(not(no_global_oom_handling), not(no_rc), not(no_sync), target_has_atomic = "ptr"))]
753dece8
MO
262pub mod task;
263#[cfg(test)]
264mod tests;
265pub mod vec;
266
267#[doc(hidden)]
268#[unstable(feature = "liballoc_internals", issue = "none", reason = "implementation detail")]
269pub mod __export {
270 pub use core::format_args;
271}
3ed03f4d
MO
272
273#[cfg(test)]
274#[allow(dead_code)] // Not used in all configurations
275pub(crate) mod test_helpers {
276 /// Copied from `std::test_helpers::test_rng`, since these tests rely on the
277 /// seed not being the same for every RNG invocation too.
278 pub(crate) fn test_rng() -> rand_xorshift::XorShiftRng {
279 use std::hash::{BuildHasher, Hash, Hasher};
768409cf 280 let mut hasher = std::hash::RandomState::new().build_hasher();
3ed03f4d
MO
281 std::panic::Location::caller().hash(&mut hasher);
282 let hc64 = hasher.finish();
283 let seed_vec =
284 hc64.to_le_bytes().into_iter().chain(0u8..8).collect::<crate::vec::Vec<u8>>();
285 let seed: [u8; 16] = seed_vec.as_slice().try_into().unwrap();
286 rand::SeedableRng::from_seed(seed)
287 }
288}