]> git.lizzy.rs Git - rust.git/blob - library/alloc/src/lib.rs
Auto merge of #107443 - cjgillot:generator-less-query, r=compiler-errors
[rust.git] / library / alloc / src / lib.rs
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 #![allow(unused_attributes)]
60 #![stable(feature = "alloc", since = "1.36.0")]
61 #![doc(
62     html_playground_url = "https://play.rust-lang.org/",
63     issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/",
64     test(no_crate_inject, attr(allow(unused_variables), deny(warnings)))
65 )]
66 #![doc(cfg_hide(
67     not(test),
68     not(any(test, bootstrap)),
69     any(not(feature = "miri-test-libstd"), test, doctest),
70     no_global_oom_handling,
71     not(no_global_oom_handling),
72     not(no_rc),
73     not(no_sync),
74     target_has_atomic = "ptr"
75 ))]
76 #![no_std]
77 #![needs_allocator]
78 // To run alloc tests without x.py without ending up with two copies of alloc, Miri needs to be
79 // able to "empty" this crate. See <https://github.com/rust-lang/miri-test-libstd/issues/4>.
80 // rustc itself never sets the feature, so this line has no affect there.
81 #![cfg(any(not(feature = "miri-test-libstd"), test, doctest))]
82 //
83 // Lints:
84 #![deny(unsafe_op_in_unsafe_fn)]
85 #![deny(fuzzy_provenance_casts)]
86 #![warn(deprecated_in_future)]
87 #![warn(missing_debug_implementations)]
88 #![warn(missing_docs)]
89 #![allow(explicit_outlives_requirements)]
90 #![cfg_attr(not(bootstrap), warn(multiple_supertrait_upcastable))]
91 //
92 // Library features:
93 #![feature(alloc_layout_extra)]
94 #![feature(allocator_api)]
95 #![feature(array_chunks)]
96 #![feature(array_into_iter_constructors)]
97 #![feature(array_methods)]
98 #![feature(array_windows)]
99 #![feature(assert_matches)]
100 #![feature(async_iterator)]
101 #![feature(coerce_unsized)]
102 #![cfg_attr(not(no_global_oom_handling), feature(const_alloc_error))]
103 #![feature(const_box)]
104 #![cfg_attr(not(no_global_oom_handling), feature(const_btree_len))]
105 #![feature(const_cow_is_borrowed)]
106 #![feature(const_convert)]
107 #![feature(const_size_of_val)]
108 #![feature(const_align_of_val)]
109 #![feature(const_ptr_read)]
110 #![feature(const_maybe_uninit_zeroed)]
111 #![feature(const_maybe_uninit_write)]
112 #![feature(const_maybe_uninit_as_mut_ptr)]
113 #![feature(const_refs_to_cell)]
114 #![feature(core_intrinsics)]
115 #![feature(core_panic)]
116 #![feature(const_eval_select)]
117 #![feature(const_pin)]
118 #![feature(const_waker)]
119 #![feature(cstr_from_bytes_until_nul)]
120 #![feature(dispatch_from_dyn)]
121 #![feature(error_generic_member_access)]
122 #![feature(error_in_core)]
123 #![feature(exact_size_is_empty)]
124 #![feature(extend_one)]
125 #![feature(fmt_internals)]
126 #![feature(fn_traits)]
127 #![feature(hasher_prefixfree_extras)]
128 #![feature(inline_const)]
129 #![feature(inplace_iteration)]
130 #![cfg_attr(test, feature(is_sorted))]
131 #![feature(iter_advance_by)]
132 #![feature(iter_next_chunk)]
133 #![feature(iter_repeat_n)]
134 #![feature(layout_for_ptr)]
135 #![feature(maybe_uninit_slice)]
136 #![feature(maybe_uninit_uninit_array)]
137 #![feature(maybe_uninit_uninit_array_transpose)]
138 #![cfg_attr(test, feature(new_uninit))]
139 #![feature(nonnull_slice_from_raw_parts)]
140 #![feature(pattern)]
141 #![feature(pointer_byte_offsets)]
142 #![feature(provide_any)]
143 #![feature(ptr_internals)]
144 #![feature(ptr_metadata)]
145 #![feature(ptr_sub_ptr)]
146 #![feature(receiver_trait)]
147 #![feature(saturating_int_impl)]
148 #![feature(set_ptr_value)]
149 #![feature(sized_type_properties)]
150 #![feature(slice_from_ptr_range)]
151 #![feature(slice_group_by)]
152 #![feature(slice_ptr_get)]
153 #![feature(slice_ptr_len)]
154 #![feature(slice_range)]
155 #![feature(str_internals)]
156 #![feature(strict_provenance)]
157 #![feature(trusted_len)]
158 #![feature(trusted_random_access)]
159 #![feature(try_trait_v2)]
160 #![feature(tuple_trait)]
161 #![feature(unchecked_math)]
162 #![feature(unicode_internals)]
163 #![feature(unsize)]
164 #![feature(utf8_chunks)]
165 #![feature(std_internals)]
166 //
167 // Language features:
168 #![feature(allocator_internals)]
169 #![feature(allow_internal_unstable)]
170 #![feature(associated_type_bounds)]
171 #![feature(cfg_sanitize)]
172 #![feature(const_deref)]
173 #![feature(const_mut_refs)]
174 #![feature(const_ptr_write)]
175 #![feature(const_precise_live_drops)]
176 #![feature(const_trait_impl)]
177 #![feature(const_try)]
178 #![feature(dropck_eyepatch)]
179 #![feature(exclusive_range_pattern)]
180 #![feature(fundamental)]
181 #![cfg_attr(not(test), feature(generator_trait))]
182 #![feature(hashmap_internals)]
183 #![feature(lang_items)]
184 #![feature(min_specialization)]
185 #![feature(negative_impls)]
186 #![feature(never_type)]
187 #![feature(rustc_allow_const_fn_unstable)]
188 #![feature(rustc_attrs)]
189 #![feature(pointer_is_aligned)]
190 #![feature(slice_internals)]
191 #![feature(staged_api)]
192 #![feature(stmt_expr_attributes)]
193 #![cfg_attr(test, feature(test))]
194 #![feature(unboxed_closures)]
195 #![feature(unsized_fn_params)]
196 #![feature(c_unwind)]
197 #![feature(with_negative_coherence)]
198 #![cfg_attr(test, feature(panic_update_hook))]
199 #![cfg_attr(not(bootstrap), feature(multiple_supertrait_upcastable))]
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 #[macro_use]
221 mod macros;
222
223 mod raw_vec;
224
225 // Heaps provided for low-level allocation strategies
226
227 pub mod alloc;
228
229 // Primitive types using the heaps above
230
231 // Need to conditionally define the mod from `boxed.rs` to avoid
232 // duplicating the lang-items when building in test cfg; but also need
233 // to allow code to have `use boxed::Box;` declarations.
234 #[cfg(not(test))]
235 pub mod boxed;
236 #[cfg(test)]
237 mod boxed {
238     pub use std::boxed::Box;
239 }
240 pub mod borrow;
241 pub mod collections;
242 #[cfg(all(not(no_rc), not(no_sync), not(no_global_oom_handling)))]
243 pub mod ffi;
244 pub mod fmt;
245 #[cfg(not(no_rc))]
246 pub mod rc;
247 pub mod slice;
248 pub mod str;
249 pub mod string;
250 #[cfg(all(not(no_rc), not(no_sync), target_has_atomic = "ptr"))]
251 pub mod sync;
252 #[cfg(all(not(no_global_oom_handling), not(no_rc), not(no_sync), target_has_atomic = "ptr"))]
253 pub mod task;
254 #[cfg(test)]
255 mod tests;
256 pub mod vec;
257
258 #[doc(hidden)]
259 #[unstable(feature = "liballoc_internals", issue = "none", reason = "implementation detail")]
260 pub mod __export {
261     pub use core::format_args;
262 }
263
264 #[cfg(test)]
265 #[allow(dead_code)] // Not used in all configurations
266 pub(crate) mod test_helpers {
267     /// Copied from `std::test_helpers::test_rng`, since these tests rely on the
268     /// seed not being the same for every RNG invocation too.
269     pub(crate) fn test_rng() -> rand_xorshift::XorShiftRng {
270         use std::hash::{BuildHasher, Hash, Hasher};
271         let mut hasher = std::collections::hash_map::RandomState::new().build_hasher();
272         std::panic::Location::caller().hash(&mut hasher);
273         let hc64 = hasher.finish();
274         let seed_vec =
275             hc64.to_le_bytes().into_iter().chain(0u8..8).collect::<crate::vec::Vec<u8>>();
276         let seed: [u8; 16] = seed_vec.as_slice().try_into().unwrap();
277         rand::SeedableRng::from_seed(seed)
278     }
279 }