alloc/
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(incomplete_features)]
60#![allow(unused_attributes)]
61#![stable(feature = "alloc", since = "1.36.0")]
62#![doc(
63    html_playground_url = "https://play.rust-lang.org/",
64    issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/",
65    test(no_crate_inject, attr(allow(unused_variables), deny(warnings)))
66)]
67#![doc(auto_cfg(hide(no_global_oom_handling, no_rc, no_sync, target_has_atomic = "ptr")))]
68#![doc(rust_logo)]
69#![feature(rustdoc_internals)]
70#![no_std]
71#![needs_allocator]
72// Lints:
73#![deny(unsafe_op_in_unsafe_fn)]
74#![deny(fuzzy_provenance_casts)]
75#![warn(deprecated_in_future)]
76#![warn(missing_debug_implementations)]
77#![warn(missing_docs)]
78#![allow(explicit_outlives_requirements)]
79#![warn(multiple_supertrait_upcastable)]
80#![allow(internal_features)]
81#![allow(rustdoc::redundant_explicit_links)]
82#![warn(rustdoc::unescaped_backticks)]
83#![deny(ffi_unwind_calls)]
84#![warn(unreachable_pub)]
85//
86// Ferrocene addition: We removed the tidy directives for alphabetical ordering to reduce the number
87// of conflicts we have when merging main.
88//
89// Library features:
90// tidy-alphabetical-start
91#![cfg_attr(not(feature = "ferrocene_certified"), feature(alloc_layout_extra))]
92#![cfg_attr(not(feature = "ferrocene_certified"), feature(allocator_api))]
93#![cfg_attr(not(feature = "ferrocene_certified"), feature(array_into_iter_constructors))]
94#![cfg_attr(not(feature = "ferrocene_certified"), feature(array_windows))]
95#![cfg_attr(not(feature = "ferrocene_certified"), feature(ascii_char))]
96#![cfg_attr(not(feature = "ferrocene_certified"), feature(assert_matches))]
97#![cfg_attr(not(feature = "ferrocene_certified"), feature(async_fn_traits))]
98#![cfg_attr(not(feature = "ferrocene_certified"), feature(async_iterator))]
99#![cfg_attr(not(feature = "ferrocene_certified"), feature(bstr))]
100#![cfg_attr(not(feature = "ferrocene_certified"), feature(bstr_internals))]
101#![cfg_attr(not(feature = "ferrocene_certified"), feature(cast_maybe_uninit))]
102#![cfg_attr(not(feature = "ferrocene_certified"), feature(cell_get_cloned))]
103#![cfg_attr(not(feature = "ferrocene_certified"), feature(char_internals))]
104#![cfg_attr(not(feature = "ferrocene_certified"), feature(char_max_len))]
105#![cfg_attr(not(feature = "ferrocene_certified"), feature(clone_to_uninit))]
106#![cfg_attr(not(feature = "ferrocene_certified"), feature(coerce_unsized))]
107#![cfg_attr(not(feature = "ferrocene_certified"), feature(const_convert))]
108#![cfg_attr(not(feature = "ferrocene_certified"), feature(const_default))]
109#![cfg_attr(not(feature = "ferrocene_certified"), feature(const_eval_select))]
110#![cfg_attr(not(feature = "ferrocene_certified"), feature(const_heap))]
111#![cfg_attr(not(feature = "ferrocene_certified"), feature(core_intrinsics))]
112#![cfg_attr(not(feature = "ferrocene_certified"), feature(deprecated_suggestion))]
113#![cfg_attr(not(feature = "ferrocene_certified"), feature(deref_pure_trait))]
114#![cfg_attr(not(feature = "ferrocene_certified"), feature(dispatch_from_dyn))]
115#![cfg_attr(not(feature = "ferrocene_certified"), feature(ergonomic_clones))]
116#![cfg_attr(not(feature = "ferrocene_certified"), feature(error_generic_member_access))]
117#![cfg_attr(not(feature = "ferrocene_certified"), feature(exact_size_is_empty))]
118#![cfg_attr(not(feature = "ferrocene_certified"), feature(extend_one))]
119#![cfg_attr(not(feature = "ferrocene_certified"), feature(extend_one_unchecked))]
120#![cfg_attr(not(feature = "ferrocene_certified"), feature(fmt_internals))]
121#![cfg_attr(not(feature = "ferrocene_certified"), feature(fn_traits))]
122#![cfg_attr(not(feature = "ferrocene_certified"), feature(formatting_options))]
123#![cfg_attr(not(feature = "ferrocene_certified"), feature(generic_atomic))]
124#![cfg_attr(not(feature = "ferrocene_certified"), feature(hasher_prefixfree_extras))]
125#![cfg_attr(not(feature = "ferrocene_certified"), feature(inplace_iteration))]
126#![cfg_attr(not(feature = "ferrocene_certified"), feature(iter_advance_by))]
127#![cfg_attr(not(feature = "ferrocene_certified"), feature(iter_next_chunk))]
128#![cfg_attr(not(feature = "ferrocene_certified"), feature(layout_for_ptr))]
129#![cfg_attr(not(feature = "ferrocene_certified"), feature(legacy_receiver_trait))]
130#![cfg_attr(not(feature = "ferrocene_certified"), feature(local_waker))]
131#![cfg_attr(not(feature = "ferrocene_certified"), feature(maybe_uninit_slice))]
132#![cfg_attr(not(feature = "ferrocene_certified"), feature(maybe_uninit_uninit_array_transpose))]
133#![cfg_attr(not(feature = "ferrocene_certified"), feature(panic_internals))]
134#![cfg_attr(not(feature = "ferrocene_certified"), feature(pattern))]
135#![cfg_attr(not(feature = "ferrocene_certified"), feature(pin_coerce_unsized_trait))]
136#![cfg_attr(not(feature = "ferrocene_certified"), feature(ptr_alignment_type))]
137#![cfg_attr(not(feature = "ferrocene_certified"), feature(ptr_internals))]
138#![cfg_attr(not(feature = "ferrocene_certified"), feature(ptr_metadata))]
139#![cfg_attr(not(feature = "ferrocene_certified"), feature(set_ptr_value))]
140#![cfg_attr(not(feature = "ferrocene_certified"), feature(sized_type_properties))]
141#![cfg_attr(not(feature = "ferrocene_certified"), feature(slice_from_ptr_range))]
142#![cfg_attr(not(feature = "ferrocene_certified"), feature(slice_index_methods))]
143#![cfg_attr(not(feature = "ferrocene_certified"), feature(slice_iter_mut_as_mut_slice))]
144#![cfg_attr(not(feature = "ferrocene_certified"), feature(slice_ptr_get))]
145#![cfg_attr(not(feature = "ferrocene_certified"), feature(slice_range))]
146#![cfg_attr(not(feature = "ferrocene_certified"), feature(std_internals))]
147#![cfg_attr(not(feature = "ferrocene_certified"), feature(str_internals))]
148#![cfg_attr(not(feature = "ferrocene_certified"), feature(temporary_niche_types))]
149#![cfg_attr(not(feature = "ferrocene_certified"), feature(trusted_fused))]
150#![cfg_attr(not(feature = "ferrocene_certified"), feature(trusted_len))]
151#![cfg_attr(not(feature = "ferrocene_certified"), feature(trusted_random_access))]
152#![cfg_attr(not(feature = "ferrocene_certified"), feature(try_trait_v2))]
153#![cfg_attr(not(feature = "ferrocene_certified"), feature(try_with_capacity))]
154#![cfg_attr(not(feature = "ferrocene_certified"), feature(tuple_trait))]
155#![cfg_attr(not(feature = "ferrocene_certified"), feature(ub_checks))]
156#![cfg_attr(not(feature = "ferrocene_certified"), feature(unicode_internals))]
157#![cfg_attr(not(feature = "ferrocene_certified"), feature(unsize))]
158#![cfg_attr(not(feature = "ferrocene_certified"), feature(unwrap_infallible))]
159#![cfg_attr(not(feature = "ferrocene_certified"), feature(wtf8_internals))]
160// tidy-alphabetical-end
161//
162// Language features:
163// not-alphbetical-start
164#![feature(allocator_internals)]
165#![cfg_attr(not(feature = "ferrocene_certified"), feature(allow_internal_unstable))]
166#![cfg_attr(not(feature = "ferrocene_certified"), feature(cfg_sanitize))]
167#![cfg_attr(not(feature = "ferrocene_certified"), feature(const_precise_live_drops))]
168#![cfg_attr(not(feature = "ferrocene_certified"), feature(const_trait_impl))]
169#![cfg_attr(not(feature = "ferrocene_certified"), feature(coroutine_trait))]
170#![cfg_attr(not(feature = "ferrocene_certified"), feature(decl_macro))]
171#![cfg_attr(not(feature = "ferrocene_certified"), feature(dropck_eyepatch))]
172#![cfg_attr(not(feature = "ferrocene_certified"), feature(fundamental))]
173#![cfg_attr(not(feature = "ferrocene_certified"), feature(hashmap_internals))]
174#![cfg_attr(not(feature = "ferrocene_certified"), feature(intrinsics))]
175#![cfg_attr(not(feature = "ferrocene_certified"), feature(lang_items))]
176#![cfg_attr(not(feature = "ferrocene_certified"), feature(min_specialization))]
177#![feature(multiple_supertrait_upcastable)]
178#![cfg_attr(not(feature = "ferrocene_certified"), feature(negative_impls))]
179#![cfg_attr(not(feature = "ferrocene_certified"), feature(never_type))]
180#![cfg_attr(not(feature = "ferrocene_certified"), feature(optimize_attribute))]
181#![cfg_attr(not(feature = "ferrocene_certified"), feature(rustc_allow_const_fn_unstable))]
182#![cfg_attr(not(feature = "ferrocene_certified"), feature(rustc_attrs))]
183#![cfg_attr(not(feature = "ferrocene_certified"), feature(slice_internals))]
184#![feature(staged_api)]
185#![cfg_attr(not(feature = "ferrocene_certified"), feature(stmt_expr_attributes))]
186#![feature(strict_provenance_lints)]
187#![cfg_attr(not(feature = "ferrocene_certified"), feature(unboxed_closures))]
188#![cfg_attr(not(feature = "ferrocene_certified"), feature(unsized_fn_params))]
189#![cfg_attr(not(feature = "ferrocene_certified"), feature(with_negative_coherence))]
190#![cfg_attr(not(feature = "ferrocene_certified"), rustc_preserve_ub_checks)]
191// not-alphbetical-end
192//
193// Rustdoc features:
194#![feature(doc_cfg)]
195// Technically, this is a bug in rustdoc: rustdoc sees the documentation on `#[lang = slice_alloc]`
196// blocks is for `&[T]`, which also has documentation using this feature in `core`, and gets mad
197// that the feature-gate isn't enabled. Ideally, it wouldn't check for the feature gate for docs
198// from other crates, but since this can only appear for lang items, it doesn't seem worth fixing.
199#![cfg_attr(not(feature = "ferrocene_certified"), feature(intra_doc_pointers))]
200//
201// Ferrocene lints/features:
202#![cfg_attr(feature = "ferrocene_certified", allow(rustdoc::broken_intra_doc_links))]
203
204// Module with internal macros used by other modules (needs to be included before other modules).
205#[macro_use]
206#[cfg(not(feature = "ferrocene_certified"))]
207mod macros;
208
209#[cfg(not(feature = "ferrocene_certified"))]
210mod raw_vec;
211
212// Heaps provided for low-level allocation strategies
213#[cfg(not(feature = "ferrocene_certified"))]
214pub mod alloc;
215
216// Primitive types using the heaps above
217
218// Need to conditionally define the mod from `boxed.rs` to avoid
219// duplicating the lang-items when building in test cfg; but also need
220// to allow code to have `use boxed::Box;` declarations.
221#[cfg(not(feature = "ferrocene_certified"))]
222pub mod borrow;
223#[cfg(not(feature = "ferrocene_certified"))]
224pub mod boxed;
225#[unstable(feature = "bstr", issue = "134915")]
226#[cfg(not(feature = "ferrocene_certified"))]
227pub mod bstr;
228#[cfg(not(feature = "ferrocene_certified"))]
229pub mod collections;
230#[cfg(all(not(no_rc), not(no_sync), not(no_global_oom_handling)))]
231#[cfg(not(feature = "ferrocene_certified"))]
232pub mod ffi;
233#[cfg(not(feature = "ferrocene_certified"))]
234pub mod fmt;
235#[cfg(not(no_rc))]
236#[cfg(not(feature = "ferrocene_certified"))]
237pub mod rc;
238#[cfg(not(feature = "ferrocene_certified"))]
239pub mod slice;
240#[cfg(not(feature = "ferrocene_certified"))]
241pub mod str;
242#[cfg(not(feature = "ferrocene_certified"))]
243pub mod string;
244#[cfg(all(not(no_rc), not(no_sync), target_has_atomic = "ptr"))]
245#[cfg(not(feature = "ferrocene_certified"))]
246pub mod sync;
247#[cfg(all(not(no_global_oom_handling), not(no_rc), not(no_sync)))]
248#[cfg(not(feature = "ferrocene_certified"))]
249pub mod task;
250#[cfg(not(feature = "ferrocene_certified"))]
251pub mod vec;
252#[cfg(all(not(no_rc), not(no_sync), not(no_global_oom_handling)))]
253#[cfg(not(feature = "ferrocene_certified"))]
254pub mod wtf8;
255
256#[doc(hidden)]
257#[unstable(feature = "liballoc_internals", issue = "none", reason = "implementation detail")]
258#[cfg(not(feature = "ferrocene_certified"))]
259pub mod __export {
260    pub use core::format_args;
261    pub use core::hint::must_use;
262}