]> git.lizzy.rs Git - rust.git/blob - src/liballoc/lib.rs
Merge branch 'refactor-select' of https://github.com/aravind-pg/rust into update...
[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 re-exported 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 re-exported 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 #![allow(unused_attributes)]
64 #![unstable(feature = "alloc",
65             reason = "this library is unlikely to be stabilized in its current \
66                       form or name",
67             issue = "27783")]
68 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
69        html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
70        html_root_url = "https://doc.rust-lang.org/nightly/",
71        issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/",
72        test(no_crate_inject, attr(allow(unused_variables), deny(warnings))))]
73 #![no_std]
74 #![needs_allocator]
75 #![deny(warnings)]
76 #![deny(missing_debug_implementations)]
77
78 #![cfg_attr(test, allow(deprecated))] // rand
79 #![cfg_attr(test, feature(placement_in))]
80 #![cfg_attr(not(test), feature(core_float))]
81 #![cfg_attr(not(test), feature(exact_size_is_empty))]
82 #![cfg_attr(not(test), feature(generator_trait))]
83 #![cfg_attr(test, feature(rand, test))]
84 #![feature(allow_internal_unstable)]
85 #![feature(ascii_ctype)]
86 #![feature(box_into_raw_non_null)]
87 #![feature(box_patterns)]
88 #![feature(box_syntax)]
89 #![feature(cfg_target_has_atomic)]
90 #![feature(coerce_unsized)]
91 #![feature(const_fn)]
92 #![feature(core_intrinsics)]
93 #![feature(custom_attribute)]
94 #![feature(dropck_eyepatch)]
95 #![feature(exact_size_is_empty)]
96 #![feature(fmt_internals)]
97 #![feature(from_ref)]
98 #![feature(fundamental)]
99 #![feature(generic_param_attrs)]
100 #![feature(i128_type)]
101 #![feature(inclusive_range)]
102 #![feature(iter_rfold)]
103 #![feature(lang_items)]
104 #![feature(needs_allocator)]
105 #![feature(nonzero)]
106 #![feature(offset_to)]
107 #![feature(optin_builtin_traits)]
108 #![feature(pattern)]
109 #![feature(placement_in_syntax)]
110 #![feature(placement_new_protocol)]
111 #![feature(ptr_internals)]
112 #![feature(rustc_attrs)]
113 #![feature(slice_get_slice)]
114 #![feature(slice_patterns)]
115 #![feature(slice_rsplit)]
116 #![feature(specialization)]
117 #![feature(staged_api)]
118 #![feature(str_internals)]
119 #![feature(trusted_len)]
120 #![feature(unboxed_closures)]
121 #![feature(unicode)]
122 #![feature(unsize)]
123 #![feature(allocator_internals)]
124 #![feature(on_unimplemented)]
125 #![feature(exact_chunks)]
126 #![feature(pointer_methods)]
127
128 #![cfg_attr(not(test), feature(fn_traits, placement_new_protocol, swap_with_slice, i128))]
129 #![cfg_attr(test, feature(test, box_heap))]
130
131 // Allow testing this library
132
133 #[cfg(test)]
134 #[macro_use]
135 extern crate std;
136 #[cfg(test)]
137 extern crate test;
138 #[cfg(test)]
139 extern crate rand;
140
141 extern crate std_unicode;
142
143 // Module with internal macros used by other modules (needs to be included before other modules).
144 #[macro_use]
145 mod macros;
146
147 // Allocator trait and helper struct definitions
148
149 pub mod allocator;
150
151 // Heaps provided for low-level allocation strategies
152
153 pub mod heap;
154
155 // Primitive types using the heaps above
156
157 // Need to conditionally define the mod from `boxed.rs` to avoid
158 // duplicating the lang-items when building in test cfg; but also need
159 // to allow code to have `use boxed::HEAP;`
160 // and `use boxed::Box;` declarations.
161 #[cfg(not(test))]
162 pub mod boxed;
163 #[cfg(test)]
164 mod boxed {
165     pub use std::boxed::{Box, IntermediateBox, HEAP};
166 }
167 #[cfg(test)]
168 mod boxed_test;
169 #[cfg(target_has_atomic = "ptr")]
170 pub mod arc;
171 pub mod rc;
172 pub mod raw_vec;
173
174 // collections modules
175 pub mod binary_heap;
176 mod btree;
177 pub mod borrow;
178 pub mod fmt;
179 pub mod linked_list;
180 pub mod range;
181 pub mod slice;
182 pub mod str;
183 pub mod string;
184 pub mod vec;
185 pub mod vec_deque;
186
187 #[stable(feature = "rust1", since = "1.0.0")]
188 pub mod btree_map {
189     //! A map based on a B-Tree.
190     #[stable(feature = "rust1", since = "1.0.0")]
191     pub use btree::map::*;
192 }
193
194 #[stable(feature = "rust1", since = "1.0.0")]
195 pub mod btree_set {
196     //! A set based on a B-Tree.
197     #[stable(feature = "rust1", since = "1.0.0")]
198     pub use btree::set::*;
199 }
200
201 #[cfg(not(test))]
202 mod std {
203     pub use core::ops;      // RangeFull
204 }
205
206 /// An endpoint of a range of keys.
207 ///
208 /// # Examples
209 ///
210 /// `Bound`s are range endpoints:
211 ///
212 /// ```
213 /// #![feature(collections_range)]
214 ///
215 /// use std::collections::range::RangeArgument;
216 /// use std::collections::Bound::*;
217 ///
218 /// assert_eq!((..100).start(), Unbounded);
219 /// assert_eq!((1..12).start(), Included(&1));
220 /// assert_eq!((1..12).end(), Excluded(&12));
221 /// ```
222 ///
223 /// Using a tuple of `Bound`s as an argument to [`BTreeMap::range`].
224 /// Note that in most cases, it's better to use range syntax (`1..5`) instead.
225 ///
226 /// ```
227 /// use std::collections::BTreeMap;
228 /// use std::collections::Bound::{Excluded, Included, Unbounded};
229 ///
230 /// let mut map = BTreeMap::new();
231 /// map.insert(3, "a");
232 /// map.insert(5, "b");
233 /// map.insert(8, "c");
234 ///
235 /// for (key, value) in map.range((Excluded(3), Included(8))) {
236 ///     println!("{}: {}", key, value);
237 /// }
238 ///
239 /// assert_eq!(Some((&3, &"a")), map.range((Unbounded, Included(5))).next());
240 /// ```
241 ///
242 /// [`BTreeMap::range`]: btree_map/struct.BTreeMap.html#method.range
243 #[stable(feature = "collections_bound", since = "1.17.0")]
244 #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
245 pub enum Bound<T> {
246     /// An inclusive bound.
247     #[stable(feature = "collections_bound", since = "1.17.0")]
248     Included(#[stable(feature = "collections_bound", since = "1.17.0")] T),
249     /// An exclusive bound.
250     #[stable(feature = "collections_bound", since = "1.17.0")]
251     Excluded(#[stable(feature = "collections_bound", since = "1.17.0")] T),
252     /// An infinite endpoint. Indicates that there is no bound in this direction.
253     #[stable(feature = "collections_bound", since = "1.17.0")]
254     Unbounded,
255 }
256
257 /// An intermediate trait for specialization of `Extend`.
258 #[doc(hidden)]
259 trait SpecExtend<I: IntoIterator> {
260     /// Extends `self` with the contents of the given iterator.
261     fn spec_extend(&mut self, iter: I);
262 }
263
264 #[doc(no_inline)]
265 pub use binary_heap::BinaryHeap;
266 #[doc(no_inline)]
267 pub use btree_map::BTreeMap;
268 #[doc(no_inline)]
269 pub use btree_set::BTreeSet;
270 #[doc(no_inline)]
271 pub use linked_list::LinkedList;
272 #[doc(no_inline)]
273 pub use vec_deque::VecDeque;
274 #[doc(no_inline)]
275 pub use string::String;
276 #[doc(no_inline)]
277 pub use vec::Vec;