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