GNU Linux-libre 6.9.1-gnu
[releases.git] / rust / alloc / lib.rs
1 // SPDX-License-Identifier: Apache-2.0 OR MIT
2
3 //! # The Rust core allocation and collections library
4 //!
5 //! This library provides smart pointers and collections for managing
6 //! heap-allocated values.
7 //!
8 //! This library, like core, normally doesn’t need to be used directly
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
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>.
63 // rustc itself never sets the feature, so this line has no effect there.
64 #![cfg(any(not(feature = "miri-test-libstd"), test, doctest))]
65 //
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),
79     not(no_rc),
80     not(no_sync),
81     target_has_atomic = "ptr"
82 ))]
83 #![doc(rust_logo)]
84 #![feature(rustdoc_internals)]
85 #![no_std]
86 #![needs_allocator]
87 // Lints:
88 #![deny(unsafe_op_in_unsafe_fn)]
89 #![deny(fuzzy_provenance_casts)]
90 #![warn(deprecated_in_future)]
91 #![warn(missing_debug_implementations)]
92 #![warn(missing_docs)]
93 #![allow(explicit_outlives_requirements)]
94 #![warn(multiple_supertrait_upcastable)]
95 #![allow(internal_features)]
96 #![allow(rustdoc::redundant_explicit_links)]
97 //
98 // Library features:
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))]
104 #![feature(alloc_layout_extra)]
105 #![feature(allocator_api)]
106 #![feature(array_chunks)]
107 #![feature(array_into_iter_constructors)]
108 #![feature(array_methods)]
109 #![feature(array_windows)]
110 #![feature(ascii_char)]
111 #![feature(assert_matches)]
112 #![feature(async_iterator)]
113 #![feature(coerce_unsized)]
114 #![feature(const_align_of_val)]
115 #![feature(const_box)]
116 #![cfg_attr(not(no_borrow), feature(const_cow_is_borrowed))]
117 #![feature(const_eval_select)]
118 #![feature(const_maybe_uninit_as_mut_ptr)]
119 #![feature(const_maybe_uninit_write)]
120 #![feature(const_pin)]
121 #![feature(const_refs_to_cell)]
122 #![feature(const_size_of_val)]
123 #![feature(const_waker)]
124 #![feature(core_intrinsics)]
125 #![feature(core_panic)]
126 #![feature(deprecated_suggestion)]
127 #![feature(dispatch_from_dyn)]
128 #![feature(error_generic_member_access)]
129 #![feature(error_in_core)]
130 #![feature(exact_size_is_empty)]
131 #![feature(extend_one)]
132 #![feature(fmt_internals)]
133 #![feature(fn_traits)]
134 #![feature(hasher_prefixfree_extras)]
135 #![feature(inline_const)]
136 #![feature(inplace_iteration)]
137 #![feature(iter_advance_by)]
138 #![feature(iter_next_chunk)]
139 #![feature(iter_repeat_n)]
140 #![feature(layout_for_ptr)]
141 #![feature(maybe_uninit_slice)]
142 #![feature(maybe_uninit_uninit_array)]
143 #![feature(maybe_uninit_uninit_array_transpose)]
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)]
150 #![feature(sized_type_properties)]
151 #![feature(slice_from_ptr_range)]
152 #![feature(slice_group_by)]
153 #![feature(slice_ptr_get)]
154 #![feature(slice_ptr_len)]
155 #![feature(slice_range)]
156 #![feature(std_internals)]
157 #![feature(str_internals)]
158 #![feature(strict_provenance)]
159 #![feature(trusted_fused)]
160 #![feature(trusted_len)]
161 #![feature(trusted_random_access)]
162 #![feature(try_trait_v2)]
163 #![feature(tuple_trait)]
164 #![feature(unchecked_math)]
165 #![feature(unicode_internals)]
166 #![feature(unsize)]
167 #![feature(utf8_chunks)]
168 // tidy-alphabetical-end
169 //
170 // Language features:
171 // tidy-alphabetical-start
172 #![cfg_attr(not(test), feature(coroutine_trait))]
173 #![cfg_attr(test, feature(panic_update_hook))]
174 #![cfg_attr(test, feature(test))]
175 #![feature(allocator_internals)]
176 #![feature(allow_internal_unstable)]
177 #![feature(associated_type_bounds)]
178 #![feature(c_unwind)]
179 #![feature(cfg_sanitize)]
180 #![feature(const_mut_refs)]
181 #![feature(const_precise_live_drops)]
182 #![feature(const_ptr_write)]
183 #![feature(const_trait_impl)]
184 #![feature(const_try)]
185 #![feature(dropck_eyepatch)]
186 #![feature(exclusive_range_pattern)]
187 #![feature(fundamental)]
188 #![feature(hashmap_internals)]
189 #![feature(lang_items)]
190 #![feature(min_specialization)]
191 #![feature(multiple_supertrait_upcastable)]
192 #![feature(negative_impls)]
193 #![feature(never_type)]
194 #![feature(pointer_is_aligned)]
195 #![feature(rustc_allow_const_fn_unstable)]
196 #![feature(rustc_attrs)]
197 #![feature(slice_internals)]
198 #![feature(staged_api)]
199 #![feature(stmt_expr_attributes)]
200 #![feature(unboxed_closures)]
201 #![feature(unsized_fn_params)]
202 #![feature(with_negative_coherence)]
203 // tidy-alphabetical-end
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]
217 extern crate std;
218 #[cfg(test)]
219 extern crate test;
220 #[cfg(test)]
221 mod testing;
222
223 // Module with internal macros used by other modules (needs to be included before other modules).
224 #[cfg(not(no_macros))]
225 #[macro_use]
226 mod macros;
227
228 mod raw_vec;
229
230 // Heaps provided for low-level allocation strategies
231
232 pub 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))]
240 pub mod boxed;
241 #[cfg(test)]
242 mod boxed {
243     pub use std::boxed::Box;
244 }
245 #[cfg(not(no_borrow))]
246 pub mod borrow;
247 pub mod collections;
248 #[cfg(all(not(no_rc), not(no_sync), not(no_global_oom_handling)))]
249 pub mod ffi;
250 #[cfg(not(no_fmt))]
251 pub mod fmt;
252 #[cfg(not(no_rc))]
253 pub mod rc;
254 pub mod slice;
255 #[cfg(not(no_str))]
256 pub mod str;
257 #[cfg(not(no_string))]
258 pub mod string;
259 #[cfg(all(not(no_rc), not(no_sync), target_has_atomic = "ptr"))]
260 pub mod sync;
261 #[cfg(all(not(no_global_oom_handling), not(no_rc), not(no_sync), target_has_atomic = "ptr"))]
262 pub mod task;
263 #[cfg(test)]
264 mod tests;
265 pub mod vec;
266
267 #[doc(hidden)]
268 #[unstable(feature = "liballoc_internals", issue = "none", reason = "implementation detail")]
269 pub mod __export {
270     pub use core::format_args;
271 }
272
273 #[cfg(test)]
274 #[allow(dead_code)] // Not used in all configurations
275 pub(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};
280         let mut hasher = std::hash::RandomState::new().build_hasher();
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 }