]> git.lizzy.rs Git - rust.git/blob - library/alloc/src/lib.rs
Rollup merge of #100753 - LuisCardosoOliveira:translation-migrate-session, r=davidtwco
[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 libcore, 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     target_has_atomic = "ptr"
73 ))]
74 #![no_std]
75 #![needs_allocator]
76 // To run liballoc tests without x.py without ending up with two copies of liballoc, Miri needs to be
77 // able to "empty" this crate. See <https://github.com/rust-lang/miri-test-libstd/issues/4>.
78 // rustc itself never sets the feature, so this line has no affect there.
79 #![cfg(any(not(feature = "miri-test-libstd"), test, doctest))]
80 //
81 // Lints:
82 #![deny(unsafe_op_in_unsafe_fn)]
83 #![warn(deprecated_in_future)]
84 #![warn(missing_debug_implementations)]
85 #![warn(missing_docs)]
86 #![allow(explicit_outlives_requirements)]
87 //
88 // Library features:
89 #![feature(alloc_layout_extra)]
90 #![feature(allocator_api)]
91 #![feature(array_chunks)]
92 #![feature(array_into_iter_constructors)]
93 #![feature(array_methods)]
94 #![feature(array_windows)]
95 #![feature(assert_matches)]
96 #![feature(async_iterator)]
97 #![feature(coerce_unsized)]
98 #![cfg_attr(not(no_global_oom_handling), feature(const_alloc_error))]
99 #![feature(const_box)]
100 #![cfg_attr(not(no_global_oom_handling), feature(const_btree_new))]
101 #![feature(const_cow_is_borrowed)]
102 #![feature(const_convert)]
103 #![feature(const_size_of_val)]
104 #![feature(const_align_of_val)]
105 #![feature(const_ptr_read)]
106 #![feature(const_maybe_uninit_write)]
107 #![feature(const_maybe_uninit_as_mut_ptr)]
108 #![feature(const_refs_to_cell)]
109 #![feature(core_intrinsics)]
110 #![feature(const_eval_select)]
111 #![feature(const_pin)]
112 #![feature(cstr_from_bytes_until_nul)]
113 #![feature(dispatch_from_dyn)]
114 #![cfg_attr(not(bootstrap), feature(error_generic_member_access))]
115 #![cfg_attr(not(bootstrap), feature(error_in_core))]
116 #![feature(exact_size_is_empty)]
117 #![feature(extend_one)]
118 #![feature(fmt_internals)]
119 #![feature(fn_traits)]
120 #![feature(hasher_prefixfree_extras)]
121 #![feature(inplace_iteration)]
122 #![feature(iter_advance_by)]
123 #![feature(iter_next_chunk)]
124 #![feature(layout_for_ptr)]
125 #![feature(maybe_uninit_array_assume_init)]
126 #![feature(maybe_uninit_slice)]
127 #![feature(maybe_uninit_uninit_array)]
128 #![cfg_attr(test, feature(new_uninit))]
129 #![feature(nonnull_slice_from_raw_parts)]
130 #![feature(pattern)]
131 #![feature(pointer_byte_offsets)]
132 #![cfg_attr(not(bootstrap), feature(provide_any))]
133 #![feature(ptr_internals)]
134 #![feature(ptr_metadata)]
135 #![feature(ptr_sub_ptr)]
136 #![feature(receiver_trait)]
137 #![feature(set_ptr_value)]
138 #![feature(slice_from_ptr_range)]
139 #![feature(slice_group_by)]
140 #![feature(slice_ptr_get)]
141 #![feature(slice_ptr_len)]
142 #![feature(slice_range)]
143 #![feature(str_internals)]
144 #![feature(strict_provenance)]
145 #![feature(trusted_len)]
146 #![feature(trusted_random_access)]
147 #![feature(try_trait_v2)]
148 #![feature(unchecked_math)]
149 #![feature(unicode_internals)]
150 #![feature(unsize)]
151 #![feature(utf8_chunks)]
152 #![feature(std_internals)]
153 //
154 // Language features:
155 #![feature(allocator_internals)]
156 #![feature(allow_internal_unstable)]
157 #![feature(associated_type_bounds)]
158 #![feature(cfg_sanitize)]
159 #![feature(const_deref)]
160 #![feature(const_mut_refs)]
161 #![feature(const_ptr_write)]
162 #![feature(const_precise_live_drops)]
163 #![feature(const_trait_impl)]
164 #![feature(const_try)]
165 #![feature(dropck_eyepatch)]
166 #![feature(exclusive_range_pattern)]
167 #![feature(fundamental)]
168 #![cfg_attr(not(test), feature(generator_trait))]
169 #![feature(hashmap_internals)]
170 #![feature(lang_items)]
171 #![feature(let_else)]
172 #![feature(min_specialization)]
173 #![feature(negative_impls)]
174 #![feature(never_type)]
175 #![feature(rustc_allow_const_fn_unstable)]
176 #![feature(rustc_attrs)]
177 #![feature(pointer_is_aligned)]
178 #![feature(slice_internals)]
179 #![feature(staged_api)]
180 #![feature(stmt_expr_attributes)]
181 #![cfg_attr(test, feature(test))]
182 #![feature(unboxed_closures)]
183 #![feature(unsized_fn_params)]
184 #![feature(c_unwind)]
185 #![feature(with_negative_coherence)]
186 //
187 // Rustdoc features:
188 #![feature(doc_cfg)]
189 #![feature(doc_cfg_hide)]
190 // Technically, this is a bug in rustdoc: rustdoc sees the documentation on `#[lang = slice_alloc]`
191 // blocks is for `&[T]`, which also has documentation using this feature in `core`, and gets mad
192 // that the feature-gate isn't enabled. Ideally, it wouldn't check for the feature gate for docs
193 // from other crates, but since this can only appear for lang items, it doesn't seem worth fixing.
194 #![feature(intra_doc_pointers)]
195
196 // Allow testing this library
197 #[cfg(test)]
198 #[macro_use]
199 extern crate std;
200 #[cfg(test)]
201 extern crate test;
202
203 // Module with internal macros used by other modules (needs to be included before other modules).
204 #[macro_use]
205 mod macros;
206
207 mod raw_vec;
208
209 // Heaps provided for low-level allocation strategies
210
211 pub mod alloc;
212
213 // Primitive types using the heaps above
214
215 // Need to conditionally define the mod from `boxed.rs` to avoid
216 // duplicating the lang-items when building in test cfg; but also need
217 // to allow code to have `use boxed::Box;` declarations.
218 #[cfg(not(test))]
219 pub mod boxed;
220 #[cfg(test)]
221 mod boxed {
222     pub use std::boxed::Box;
223 }
224 pub mod borrow;
225 pub mod collections;
226 #[cfg(not(no_global_oom_handling))]
227 pub mod ffi;
228 pub mod fmt;
229 pub mod rc;
230 pub mod slice;
231 pub mod str;
232 pub mod string;
233 #[cfg(target_has_atomic = "ptr")]
234 pub mod sync;
235 #[cfg(all(not(no_global_oom_handling), target_has_atomic = "ptr"))]
236 pub mod task;
237 #[cfg(test)]
238 mod tests;
239 pub mod vec;
240
241 #[doc(hidden)]
242 #[unstable(feature = "liballoc_internals", issue = "none", reason = "implementation detail")]
243 pub mod __export {
244     pub use core::format_args;
245 }