Skip to main content

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