]> git.lizzy.rs Git - rust.git/blob - src/liballoc/lib.rs
rustc: Implement the #[global_allocator] attribute
[rust.git] / src / liballoc / lib.rs
1 // Copyright 2014-2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! # The Rust core allocation and collections library
12 //!
13 //! This library provides smart pointers and collections for managing
14 //! heap-allocated values.
15 //!
16 //! This library, like libcore, is not intended for general usage, but rather as
17 //! a building block of other libraries. The types and interfaces in this
18 //! library are reexported through the [standard library](../std/index.html),
19 //! and should not be used through this library.
20 //!
21 //! ## Boxed values
22 //!
23 //! The [`Box`](boxed/index.html) type is a smart pointer type. There can
24 //! only be one owner of a `Box`, and the owner can decide to mutate the
25 //! contents, which live on the heap.
26 //!
27 //! This type can be sent among threads efficiently as the size of a `Box` value
28 //! is the same as that of a pointer. Tree-like data structures are often built
29 //! with boxes because each node often has only one owner, the parent.
30 //!
31 //! ## Reference counted pointers
32 //!
33 //! The [`Rc`](rc/index.html) type is a non-threadsafe reference-counted pointer
34 //! type intended for sharing memory within a thread. An `Rc` pointer wraps a
35 //! type, `T`, and only allows access to `&T`, a shared reference.
36 //!
37 //! This type is useful when inherited mutability (such as using `Box`) is too
38 //! constraining for an application, and is often paired with the `Cell` or
39 //! `RefCell` types in order to allow mutation.
40 //!
41 //! ## Atomically reference counted pointers
42 //!
43 //! The [`Arc`](arc/index.html) type is the threadsafe equivalent of the `Rc`
44 //! type. It provides all the same functionality of `Rc`, except it requires
45 //! that the contained type `T` is shareable. Additionally, `Arc<T>` is itself
46 //! sendable while `Rc<T>` is not.
47 //!
48 //! This type allows for shared access to the contained data, and is often
49 //! paired with synchronization primitives such as mutexes to allow mutation of
50 //! shared resources.
51 //!
52 //! ## Collections
53 //!
54 //! Implementations of the most common general purpose data structures are
55 //! defined in this library. They are reexported through the
56 //! [standard collections library](../std/collections/index.html).
57 //!
58 //! ## Heap interfaces
59 //!
60 //! The [`heap`](heap/index.html) module defines the low-level interface to the
61 //! default global allocator. It is not compatible with the libc allocator API.
62
63 #![crate_name = "alloc"]
64 #![crate_type = "rlib"]
65 #![allow(unused_attributes)]
66 #![unstable(feature = "alloc",
67             reason = "this library is unlikely to be stabilized in its current \
68                       form or name",
69             issue = "27783")]
70 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
71        html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
72        html_root_url = "https://doc.rust-lang.org/nightly/",
73        issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/",
74        test(no_crate_inject, attr(allow(unused_variables), deny(warnings))))]
75 #![no_std]
76 #![needs_allocator]
77 #![deny(warnings)]
78 #![deny(missing_debug_implementations)]
79
80 #![cfg_attr(test, allow(deprecated))] // rand
81 #![cfg_attr(test, feature(placement_in))]
82 #![cfg_attr(not(test), feature(char_escape_debug))]
83 #![cfg_attr(not(test), feature(core_float))]
84 #![cfg_attr(not(test), feature(exact_size_is_empty))]
85 #![cfg_attr(not(test), feature(slice_rotate))]
86 #![cfg_attr(not(test), feature(str_checked_slicing))]
87 #![cfg_attr(test, feature(rand, test))]
88 #![cfg_attr(stage0, feature(allocator))]
89 #![feature(allow_internal_unstable)]
90 #![feature(box_patterns)]
91 #![feature(box_syntax)]
92 #![feature(cfg_target_has_atomic)]
93 #![feature(coerce_unsized)]
94 #![feature(const_fn)]
95 #![feature(core_intrinsics)]
96 #![feature(custom_attribute)]
97 #![feature(dropck_eyepatch)]
98 #![feature(exact_size_is_empty)]
99 #![feature(fmt_internals)]
100 #![feature(fundamental)]
101 #![feature(fused)]
102 #![feature(generic_param_attrs)]
103 #![feature(i128_type)]
104 #![feature(inclusive_range)]
105 #![feature(lang_items)]
106 #![feature(manually_drop)]
107 #![feature(needs_allocator)]
108 #![feature(nonzero)]
109 #![feature(offset_to)]
110 #![feature(optin_builtin_traits)]
111 #![feature(pattern)]
112 #![feature(placement_in_syntax)]
113 #![feature(placement_new_protocol)]
114 #![feature(shared)]
115 #![feature(slice_get_slice)]
116 #![feature(slice_patterns)]
117 #![feature(slice_rsplit)]
118 #![feature(specialization)]
119 #![feature(staged_api)]
120 #![feature(str_internals)]
121 #![feature(str_mut_extras)]
122 #![feature(trusted_len)]
123 #![feature(unboxed_closures)]
124 #![feature(unicode)]
125 #![feature(unique)]
126 #![feature(unsize)]
127 #![cfg_attr(not(stage0), feature(allocator_internals))]
128
129 #![cfg_attr(not(test), feature(fused, fn_traits, placement_new_protocol))]
130 #![cfg_attr(test, feature(test, box_heap))]
131
132 // Allow testing this library
133
134 #[cfg(test)]
135 #[macro_use]
136 extern crate std;
137 #[cfg(test)]
138 extern crate test;
139
140 extern crate std_unicode;
141
142 // Module with internal macros used by other modules (needs to be included before other modules).
143 #[macro_use]
144 mod macros;
145
146 // Allocator trait and helper struct definitions
147
148 pub mod allocator;
149
150 // Heaps provided for low-level allocation strategies
151
152 pub mod heap;
153
154 // Primitive types using the heaps above
155
156 // Need to conditionally define the mod from `boxed.rs` to avoid
157 // duplicating the lang-items when building in test cfg; but also need
158 // to allow code to have `use boxed::HEAP;`
159 // and `use boxed::Box;` declarations.
160 #[cfg(not(test))]
161 pub mod boxed;
162 #[cfg(test)]
163 mod boxed {
164     pub use std::boxed::{Box, IntermediateBox, HEAP};
165 }
166 #[cfg(test)]
167 mod boxed_test;
168 #[cfg(target_has_atomic = "ptr")]
169 pub mod arc;
170 pub mod rc;
171 pub mod raw_vec;
172
173 // collections modules
174 pub mod binary_heap;
175 mod btree;
176 pub mod borrow;
177 pub mod fmt;
178 pub mod linked_list;
179 pub mod range;
180 pub mod slice;
181 pub mod str;
182 pub mod string;
183 pub mod vec;
184 pub mod vec_deque;
185
186 #[stable(feature = "rust1", since = "1.0.0")]
187 pub mod btree_map {
188     //! A map based on a B-Tree.
189     #[stable(feature = "rust1", since = "1.0.0")]
190     pub use btree::map::*;
191 }
192
193 #[stable(feature = "rust1", since = "1.0.0")]
194 pub mod btree_set {
195     //! A set based on a B-Tree.
196     #[stable(feature = "rust1", since = "1.0.0")]
197     pub use btree::set::*;
198 }
199
200 #[cfg(not(test))]
201 mod std {
202     pub use core::ops;      // RangeFull
203 }
204
205 /// An endpoint of a range of keys.
206 ///
207 /// # Examples
208 ///
209 /// `Bound`s are range endpoints:
210 ///
211 /// ```
212 /// #![feature(collections_range)]
213 ///
214 /// use std::collections::range::RangeArgument;
215 /// use std::collections::Bound::*;
216 ///
217 /// assert_eq!((..100).start(), Unbounded);
218 /// assert_eq!((1..12).start(), Included(&1));
219 /// assert_eq!((1..12).end(), Excluded(&12));
220 /// ```
221 ///
222 /// Using a tuple of `Bound`s as an argument to [`BTreeMap::range`].
223 /// Note that in most cases, it's better to use range syntax (`1..5`) instead.
224 ///
225 /// ```
226 /// use std::collections::BTreeMap;
227 /// use std::collections::Bound::{Excluded, Included, Unbounded};
228 ///
229 /// let mut map = BTreeMap::new();
230 /// map.insert(3, "a");
231 /// map.insert(5, "b");
232 /// map.insert(8, "c");
233 ///
234 /// for (key, value) in map.range((Excluded(3), Included(8))) {
235 ///     println!("{}: {}", key, value);
236 /// }
237 ///
238 /// assert_eq!(Some((&3, &"a")), map.range((Unbounded, Included(5))).next());
239 /// ```
240 ///
241 /// [`BTreeMap::range`]: btree_map/struct.BTreeMap.html#method.range
242 #[stable(feature = "collections_bound", since = "1.17.0")]
243 #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
244 pub enum Bound<T> {
245     /// An inclusive bound.
246     #[stable(feature = "collections_bound", since = "1.17.0")]
247     Included(T),
248     /// An exclusive bound.
249     #[stable(feature = "collections_bound", since = "1.17.0")]
250     Excluded(T),
251     /// An infinite endpoint. Indicates that there is no bound in this direction.
252     #[stable(feature = "collections_bound", since = "1.17.0")]
253     Unbounded,
254 }
255
256 /// An intermediate trait for specialization of `Extend`.
257 #[doc(hidden)]
258 trait SpecExtend<I: IntoIterator> {
259     /// Extends `self` with the contents of the given iterator.
260     fn spec_extend(&mut self, iter: I);
261 }
262
263 #[doc(no_inline)]
264 pub use binary_heap::BinaryHeap;
265 #[doc(no_inline)]
266 pub use btree_map::BTreeMap;
267 #[doc(no_inline)]
268 pub use btree_set::BTreeSet;
269 #[doc(no_inline)]
270 pub use linked_list::LinkedList;
271 #[doc(no_inline)]
272 pub use vec_deque::VecDeque;
273 #[doc(no_inline)]
274 pub use string::String;
275 #[doc(no_inline)]
276 pub use vec::Vec;