]> git.lizzy.rs Git - rust.git/blob - library/alloc/src/lib.rs
d7ae353282e799decf035238000f86db0420b5cd
[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_root_url = "https://doc.rust-lang.org/nightly/",
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 #![no_std]
68 #![needs_allocator]
69 #![warn(deprecated_in_future)]
70 #![warn(missing_docs)]
71 #![warn(missing_debug_implementations)]
72 #![allow(explicit_outlives_requirements)]
73 #![deny(unsafe_op_in_unsafe_fn)]
74 #![feature(rustc_allow_const_fn_unstable)]
75 #![cfg_attr(not(test), feature(generator_trait))]
76 #![cfg_attr(test, feature(test))]
77 #![cfg_attr(test, feature(new_uninit))]
78 #![feature(allocator_api)]
79 #![feature(array_chunks)]
80 #![feature(array_methods)]
81 #![feature(array_value_iter)]
82 #![feature(array_windows)]
83 #![feature(allow_internal_unstable)]
84 #![feature(arbitrary_self_types)]
85 #![feature(box_patterns)]
86 #![feature(box_syntax)]
87 #![feature(cfg_sanitize)]
88 #![feature(cfg_target_has_atomic)]
89 #![feature(coerce_unsized)]
90 #![feature(const_btree_new)]
91 #![feature(const_fn)]
92 #![feature(const_in_array_repeat_expressions)]
93 #![feature(cow_is_borrowed)]
94 #![feature(const_cow_is_borrowed)]
95 #![feature(dispatch_from_dyn)]
96 #![feature(core_intrinsics)]
97 #![feature(dropck_eyepatch)]
98 #![feature(exact_size_is_empty)]
99 #![feature(exclusive_range_pattern)]
100 #![feature(extend_one)]
101 #![feature(fmt_internals)]
102 #![feature(fn_traits)]
103 #![feature(fundamental)]
104 #![feature(inplace_iteration)]
105 #![feature(int_bits_const)]
106 #![feature(lang_items)]
107 #![feature(layout_for_ptr)]
108 #![feature(maybe_uninit_ref)]
109 #![feature(negative_impls)]
110 #![feature(never_type)]
111 #![feature(nll)]
112 #![feature(nonnull_slice_from_raw_parts)]
113 #![feature(auto_traits)]
114 #![feature(or_patterns)]
115 #![feature(pattern)]
116 #![feature(ptr_internals)]
117 #![feature(range_bounds_assert_len)]
118 #![feature(raw_ref_op)]
119 #![feature(rustc_attrs)]
120 #![feature(receiver_trait)]
121 #![cfg_attr(bootstrap, feature(min_const_generics))]
122 #![feature(min_specialization)]
123 #![feature(set_ptr_value)]
124 #![feature(slice_ptr_get)]
125 #![feature(slice_ptr_len)]
126 #![feature(staged_api)]
127 #![feature(str_internals)]
128 #![feature(trusted_len)]
129 #![feature(unboxed_closures)]
130 #![feature(unicode_internals)]
131 #![feature(unsafe_block_in_unsafe_fn)]
132 #![feature(unsize)]
133 #![feature(unsized_fn_params)]
134 #![feature(allocator_internals)]
135 #![feature(slice_partition_dedup)]
136 #![feature(maybe_uninit_extra, maybe_uninit_slice, maybe_uninit_uninit_array)]
137 #![feature(alloc_layout_extra)]
138 #![feature(trusted_random_access)]
139 #![feature(try_trait)]
140 #![feature(type_alias_impl_trait)]
141 #![feature(associated_type_bounds)]
142 #![feature(slice_group_by)]
143 #![feature(decl_macro)]
144 // Allow testing this library
145
146 #[cfg(test)]
147 #[macro_use]
148 extern crate std;
149 #[cfg(test)]
150 extern crate test;
151
152 // Module with internal macros used by other modules (needs to be included before other modules).
153 #[macro_use]
154 mod macros;
155
156 // Heaps provided for low-level allocation strategies
157
158 pub mod alloc;
159
160 // Primitive types using the heaps above
161
162 // Need to conditionally define the mod from `boxed.rs` to avoid
163 // duplicating the lang-items when building in test cfg; but also need
164 // to allow code to have `use boxed::Box;` declarations.
165 #[cfg(not(test))]
166 pub mod boxed;
167 #[cfg(test)]
168 mod boxed {
169     pub use std::boxed::Box;
170 }
171 pub mod borrow;
172 pub mod collections;
173 pub mod fmt;
174 pub mod prelude;
175 pub mod raw_vec;
176 pub mod rc;
177 pub mod slice;
178 pub mod str;
179 pub mod string;
180 #[cfg(target_has_atomic = "ptr")]
181 pub mod sync;
182 #[cfg(target_has_atomic = "ptr")]
183 pub mod task;
184 #[cfg(test)]
185 mod tests;
186 pub mod vec;
187
188 #[doc(hidden)]
189 #[unstable(feature = "liballoc_internals", issue = "none", reason = "implementation detail")]
190 pub mod __export {
191     pub use core::format_args;
192 }