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 #![allow(unused_attributes)] 62 #![stable(feature = "alloc", since = "1.36.0")] 63 #![doc( 64 html_playground_url = "https://play.rust-lang.org/", 65 issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/", 66 test(no_crate_inject, attr(allow(unused_variables), deny(warnings))) 67 )] 68 #![doc(cfg_hide( 69 not(test), 70 not(any(test, bootstrap)), 71 any(not(feature = "miri-test-libstd"), test, doctest), 72 no_global_oom_handling, 73 not(no_global_oom_handling), 74 not(no_rc), 75 not(no_sync), 76 target_has_atomic = "ptr" 77 ))] 78 #![no_std] 79 #![needs_allocator] 80 // To run alloc tests without x.py without ending up with two copies of alloc, Miri needs to be 81 // able to "empty" this crate. See <https://github.com/rust-lang/miri-test-libstd/issues/4>. 82 // rustc itself never sets the feature, so this line has no affect there. 83 #![cfg(any(not(feature = "miri-test-libstd"), test, doctest))] 84 // 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 // 93 // Library features: 94 #![feature(alloc_layout_extra)] 95 #![feature(allocator_api)] 96 #![feature(array_chunks)] 97 #![feature(array_into_iter_constructors)] 98 #![feature(array_methods)] 99 #![feature(array_windows)] 100 #![feature(assert_matches)] 101 #![feature(async_iterator)] 102 #![feature(coerce_unsized)] 103 #![cfg_attr(not(no_global_oom_handling), feature(const_alloc_error))] 104 #![feature(const_box)] 105 #![cfg_attr(not(no_global_oom_handling), feature(const_btree_len))] 106 #![cfg_attr(not(no_borrow), feature(const_cow_is_borrowed))] 107 #![feature(const_convert)] 108 #![feature(const_size_of_val)] 109 #![feature(const_align_of_val)] 110 #![feature(const_ptr_read)] 111 #![feature(const_maybe_uninit_zeroed)] 112 #![feature(const_maybe_uninit_write)] 113 #![feature(const_maybe_uninit_as_mut_ptr)] 114 #![feature(const_refs_to_cell)] 115 #![feature(core_intrinsics)] 116 #![feature(core_panic)] 117 #![feature(const_eval_select)] 118 #![feature(const_pin)] 119 #![feature(const_waker)] 120 #![feature(cstr_from_bytes_until_nul)] 121 #![feature(dispatch_from_dyn)] 122 #![feature(error_generic_member_access)] 123 #![feature(error_in_core)] 124 #![feature(exact_size_is_empty)] 125 #![feature(extend_one)] 126 #![feature(fmt_internals)] 127 #![feature(fn_traits)] 128 #![feature(hasher_prefixfree_extras)] 129 #![feature(inline_const)] 130 #![feature(inplace_iteration)] 131 #![cfg_attr(test, feature(is_sorted))] 132 #![feature(iter_advance_by)] 133 #![feature(iter_next_chunk)] 134 #![feature(iter_repeat_n)] 135 #![feature(layout_for_ptr)] 136 #![feature(maybe_uninit_slice)] 137 #![feature(maybe_uninit_uninit_array)] 138 #![feature(maybe_uninit_uninit_array_transpose)] 139 #![cfg_attr(test, feature(new_uninit))] 140 #![feature(nonnull_slice_from_raw_parts)] 141 #![feature(pattern)] 142 #![feature(pointer_byte_offsets)] 143 #![feature(provide_any)] 144 #![feature(ptr_internals)] 145 #![feature(ptr_metadata)] 146 #![feature(ptr_sub_ptr)] 147 #![feature(receiver_trait)] 148 #![feature(saturating_int_impl)] 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(str_internals)] 157 #![feature(strict_provenance)] 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 #![feature(std_internals)] 167 // 168 // Language features: 169 #![feature(allocator_internals)] 170 #![feature(allow_internal_unstable)] 171 #![feature(associated_type_bounds)] 172 #![feature(cfg_sanitize)] 173 #![feature(const_deref)] 174 #![feature(const_mut_refs)] 175 #![feature(const_ptr_write)] 176 #![feature(const_precise_live_drops)] 177 #![feature(const_trait_impl)] 178 #![feature(const_try)] 179 #![feature(dropck_eyepatch)] 180 #![feature(exclusive_range_pattern)] 181 #![feature(fundamental)] 182 #![cfg_attr(not(test), feature(generator_trait))] 183 #![feature(hashmap_internals)] 184 #![feature(lang_items)] 185 #![feature(min_specialization)] 186 #![feature(negative_impls)] 187 #![feature(never_type)] 188 #![feature(rustc_allow_const_fn_unstable)] 189 #![feature(rustc_attrs)] 190 #![feature(pointer_is_aligned)] 191 #![feature(slice_internals)] 192 #![feature(staged_api)] 193 #![feature(stmt_expr_attributes)] 194 #![cfg_attr(test, feature(test))] 195 #![feature(unboxed_closures)] 196 #![feature(unsized_fn_params)] 197 #![feature(c_unwind)] 198 #![feature(with_negative_coherence)] 199 #![cfg_attr(test, feature(panic_update_hook))] 200 // 201 // Rustdoc features: 202 #![feature(doc_cfg)] 203 #![feature(doc_cfg_hide)] 204 // Technically, this is a bug in rustdoc: rustdoc sees the documentation on `#[lang = slice_alloc]` 205 // blocks is for `&[T]`, which also has documentation using this feature in `core`, and gets mad 206 // that the feature-gate isn't enabled. Ideally, it wouldn't check for the feature gate for docs 207 // from other crates, but since this can only appear for lang items, it doesn't seem worth fixing. 208 #![feature(intra_doc_pointers)] 209 210 // Allow testing this library 211 #[cfg(test)] 212 #[macro_use] 213 extern crate std; 214 #[cfg(test)] 215 extern crate test; 216 #[cfg(test)] 217 mod testing; 218 219 // Module with internal macros used by other modules (needs to be included before other modules). 220 #[cfg(not(no_macros))] 221 #[macro_use] 222 mod macros; 223 224 mod raw_vec; 225 226 // Heaps provided for low-level allocation strategies 227 228 pub mod alloc; 229 230 // Primitive types using the heaps above 231 232 // Need to conditionally define the mod from `boxed.rs` to avoid 233 // duplicating the lang-items when building in test cfg; but also need 234 // to allow code to have `use boxed::Box;` declarations. 235 #[cfg(not(test))] 236 pub mod boxed; 237 #[cfg(test)] 238 mod boxed { 239 pub use std::boxed::Box; 240 } 241 #[cfg(not(no_borrow))] 242 pub mod borrow; 243 pub mod collections; 244 #[cfg(all(not(no_rc), not(no_sync), not(no_global_oom_handling)))] 245 pub mod ffi; 246 #[cfg(not(no_fmt))] 247 pub mod fmt; 248 #[cfg(not(no_rc))] 249 pub mod rc; 250 pub mod slice; 251 #[cfg(not(no_str))] 252 pub mod str; 253 #[cfg(not(no_string))] 254 pub mod string; 255 #[cfg(all(not(no_rc), not(no_sync), target_has_atomic = "ptr"))] 256 pub mod sync; 257 #[cfg(all(not(no_global_oom_handling), not(no_rc), not(no_sync), target_has_atomic = "ptr"))] 258 pub mod task; 259 #[cfg(test)] 260 mod tests; 261 pub mod vec; 262 263 #[doc(hidden)] 264 #[unstable(feature = "liballoc_internals", issue = "none", reason = "implementation detail")] 265 pub mod __export { 266 pub use core::format_args; 267 } 268 269 #[cfg(test)] 270 #[allow(dead_code)] // Not used in all configurations 271 pub(crate) mod test_helpers { 272 /// Copied from `std::test_helpers::test_rng`, since these tests rely on the 273 /// seed not being the same for every RNG invocation too. 274 pub(crate) fn test_rng() -> rand_xorshift::XorShiftRng { 275 use std::hash::{BuildHasher, Hash, Hasher}; 276 let mut hasher = std::collections::hash_map::RandomState::new().build_hasher(); 277 std::panic::Location::caller().hash(&mut hasher); 278 let hc64 = hasher.finish(); 279 let seed_vec = 280 hc64.to_le_bytes().into_iter().chain(0u8..8).collect::<crate::vec::Vec<u8>>(); 281 let seed: [u8; 16] = seed_vec.as_slice().try_into().unwrap(); 282 rand::SeedableRng::from_seed(seed) 283 } 284 } 285