2017-06-13 15:52:59 -07:00
|
|
|
// Copyright 2014-2017 The Rust Project Developers. See the COPYRIGHT
|
2014-05-13 16:10:05 -07:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2017-06-13 15:52:59 -07:00
|
|
|
//! # The Rust core allocation and collections library
|
2014-05-13 16:10:05 -07:00
|
|
|
//!
|
2017-06-13 15:52:59 -07:00
|
|
|
//! This library provides smart pointers and collections for managing
|
|
|
|
//! heap-allocated values.
|
2014-05-13 16:10:05 -07:00
|
|
|
//!
|
|
|
|
//! This library, like libcore, is not intended for general usage, but rather as
|
|
|
|
//! a building block of other libraries. The types and interfaces in this
|
2018-01-12 16:41:25 -05:00
|
|
|
//! library are re-exported through the [standard library](../std/index.html),
|
2014-05-13 16:10:05 -07:00
|
|
|
//! and should not be used through this library.
|
|
|
|
//!
|
2014-07-10 14:19:17 -07:00
|
|
|
//! ## Boxed values
|
2014-05-13 16:10:05 -07:00
|
|
|
//!
|
2015-06-09 16:26:21 -04:00
|
|
|
//! The [`Box`](boxed/index.html) type is a smart pointer type. There can
|
|
|
|
//! only be one owner of a `Box`, and the owner can decide to mutate the
|
|
|
|
//! contents, which live on the heap.
|
2014-05-13 16:10:05 -07:00
|
|
|
//!
|
2015-05-09 00:12:29 +09:00
|
|
|
//! This type can be sent among threads efficiently as the size of a `Box` value
|
2014-08-04 22:48:39 +12:00
|
|
|
//! is the same as that of a pointer. Tree-like data structures are often built
|
|
|
|
//! with boxes because each node often has only one owner, the parent.
|
2014-05-13 16:10:05 -07:00
|
|
|
//!
|
|
|
|
//! ## Reference counted pointers
|
|
|
|
//!
|
|
|
|
//! The [`Rc`](rc/index.html) type is a non-threadsafe reference-counted pointer
|
2015-05-09 00:12:29 +09:00
|
|
|
//! type intended for sharing memory within a thread. An `Rc` pointer wraps a
|
2014-05-13 16:10:05 -07:00
|
|
|
//! type, `T`, and only allows access to `&T`, a shared reference.
|
|
|
|
//!
|
2014-08-04 22:48:39 +12:00
|
|
|
//! This type is useful when inherited mutability (such as using `Box`) is too
|
|
|
|
//! constraining for an application, and is often paired with the `Cell` or
|
2014-05-13 16:10:05 -07:00
|
|
|
//! `RefCell` types in order to allow mutation.
|
|
|
|
//!
|
|
|
|
//! ## Atomically reference counted pointers
|
|
|
|
//!
|
|
|
|
//! The [`Arc`](arc/index.html) type is the threadsafe equivalent of the `Rc`
|
|
|
|
//! type. It provides all the same functionality of `Rc`, except it requires
|
|
|
|
//! that the contained type `T` is shareable. Additionally, `Arc<T>` is itself
|
|
|
|
//! sendable while `Rc<T>` is not.
|
|
|
|
//!
|
2017-01-28 22:16:16 +00:00
|
|
|
//! This type allows for shared access to the contained data, and is often
|
2014-05-13 16:10:05 -07:00
|
|
|
//! paired with synchronization primitives such as mutexes to allow mutation of
|
|
|
|
//! shared resources.
|
|
|
|
//!
|
2017-06-13 15:52:59 -07:00
|
|
|
//! ## Collections
|
|
|
|
//!
|
|
|
|
//! Implementations of the most common general purpose data structures are
|
2018-01-12 16:41:25 -05:00
|
|
|
//! defined in this library. They are re-exported through the
|
2017-06-13 15:52:59 -07:00
|
|
|
//! [standard collections library](../std/collections/index.html).
|
|
|
|
//!
|
2014-05-13 16:10:05 -07:00
|
|
|
//! ## Heap interfaces
|
|
|
|
//!
|
2014-10-24 17:34:57 -04:00
|
|
|
//! The [`heap`](heap/index.html) module defines the low-level interface to the
|
|
|
|
//! default global allocator. It is not compatible with the libc allocator API.
|
2014-05-13 16:10:05 -07:00
|
|
|
|
2015-06-25 10:07:01 -07:00
|
|
|
#![allow(unused_attributes)]
|
2015-06-09 11:52:41 -07:00
|
|
|
#![unstable(feature = "alloc",
|
|
|
|
reason = "this library is unlikely to be stabilized in its current \
|
2015-08-12 22:19:08 -07:00
|
|
|
form or name",
|
|
|
|
issue = "27783")]
|
2015-08-09 14:15:05 -07:00
|
|
|
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
|
2015-05-15 16:04:01 -07:00
|
|
|
html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
|
2015-08-09 14:15:05 -07:00
|
|
|
html_root_url = "https://doc.rust-lang.org/nightly/",
|
2015-08-16 17:22:50 +02:00
|
|
|
issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/",
|
2015-11-03 14:02:48 +00:00
|
|
|
test(no_crate_inject, attr(allow(unused_variables), deny(warnings))))]
|
2014-05-13 16:10:05 -07:00
|
|
|
#![no_std]
|
2015-12-11 13:07:11 -08:00
|
|
|
#![needs_allocator]
|
2016-12-29 09:47:34 -08:00
|
|
|
#![deny(warnings)]
|
2017-06-13 15:52:59 -07:00
|
|
|
#![deny(missing_debug_implementations)]
|
2015-06-09 11:18:03 -07:00
|
|
|
|
2017-06-13 15:52:59 -07:00
|
|
|
#![cfg_attr(test, allow(deprecated))] // rand
|
|
|
|
#![cfg_attr(test, feature(placement_in))]
|
|
|
|
#![cfg_attr(not(test), feature(core_float))]
|
|
|
|
#![cfg_attr(not(test), feature(exact_size_is_empty))]
|
|
|
|
#![cfg_attr(not(test), feature(slice_rotate))]
|
2017-07-07 15:31:03 -07:00
|
|
|
#![cfg_attr(not(test), feature(generator_trait))]
|
2017-06-13 15:52:59 -07:00
|
|
|
#![cfg_attr(test, feature(rand, test))]
|
|
|
|
#![feature(allow_internal_unstable)]
|
2017-10-03 17:39:31 +02:00
|
|
|
#![feature(ascii_ctype)]
|
2018-01-10 21:11:55 +01:00
|
|
|
#![feature(box_into_raw_non_null)]
|
2017-06-13 15:52:59 -07:00
|
|
|
#![feature(box_patterns)]
|
2015-06-09 11:18:03 -07:00
|
|
|
#![feature(box_syntax)]
|
2016-10-30 22:14:05 -05:00
|
|
|
#![feature(cfg_target_has_atomic)]
|
2015-06-09 11:18:03 -07:00
|
|
|
#![feature(coerce_unsized)]
|
2016-01-22 23:49:57 -08:00
|
|
|
#![feature(const_fn)]
|
2015-06-09 11:18:03 -07:00
|
|
|
#![feature(core_intrinsics)]
|
2015-03-30 17:52:00 -04:00
|
|
|
#![feature(custom_attribute)]
|
2016-12-28 17:47:10 -05:00
|
|
|
#![feature(dropck_eyepatch)]
|
2017-06-13 15:52:59 -07:00
|
|
|
#![feature(exact_size_is_empty)]
|
|
|
|
#![feature(fmt_internals)]
|
2017-11-01 21:56:17 +00:00
|
|
|
#![feature(from_ref)]
|
2015-03-30 17:52:00 -04:00
|
|
|
#![feature(fundamental)]
|
2017-06-13 15:52:59 -07:00
|
|
|
#![feature(fused)]
|
2016-12-28 17:47:10 -05:00
|
|
|
#![feature(generic_param_attrs)]
|
2017-06-13 15:52:59 -07:00
|
|
|
#![feature(i128_type)]
|
|
|
|
#![feature(inclusive_range)]
|
2017-09-18 21:52:13 +02:00
|
|
|
#![feature(iter_rfold)]
|
2015-04-27 14:10:49 -07:00
|
|
|
#![feature(lang_items)]
|
2016-01-22 23:49:57 -08:00
|
|
|
#![feature(needs_allocator)]
|
2017-06-13 15:52:59 -07:00
|
|
|
#![feature(nonzero)]
|
|
|
|
#![feature(offset_to)]
|
2015-01-11 11:09:53 +01:00
|
|
|
#![feature(optin_builtin_traits)]
|
2017-06-13 15:52:59 -07:00
|
|
|
#![feature(pattern)]
|
2015-02-12 11:30:16 +01:00
|
|
|
#![feature(placement_in_syntax)]
|
2017-06-13 15:52:59 -07:00
|
|
|
#![feature(placement_new_protocol)]
|
2017-12-22 19:29:16 +01:00
|
|
|
#![feature(ptr_internals)]
|
2017-08-22 14:36:49 -07:00
|
|
|
#![feature(rustc_attrs)]
|
2017-06-13 15:52:59 -07:00
|
|
|
#![feature(slice_get_slice)]
|
|
|
|
#![feature(slice_patterns)]
|
|
|
|
#![feature(slice_rsplit)]
|
|
|
|
#![feature(specialization)]
|
2015-06-09 11:18:03 -07:00
|
|
|
#![feature(staged_api)]
|
2017-06-13 15:52:59 -07:00
|
|
|
#![feature(str_internals)]
|
|
|
|
#![feature(trusted_len)]
|
2015-01-26 14:39:58 -05:00
|
|
|
#![feature(unboxed_closures)]
|
2017-06-13 15:52:59 -07:00
|
|
|
#![feature(unicode)]
|
2015-06-09 11:18:03 -07:00
|
|
|
#![feature(unsize)]
|
2017-07-17 09:32:08 -07:00
|
|
|
#![feature(allocator_internals)]
|
2017-10-09 14:45:41 +02:00
|
|
|
#![feature(on_unimplemented)]
|
2018-01-02 02:13:20 +02:00
|
|
|
#![feature(exact_chunks)]
|
2015-06-09 11:52:41 -07:00
|
|
|
|
2017-08-21 22:15:02 +08:00
|
|
|
#![cfg_attr(not(test), feature(fused, fn_traits, placement_new_protocol, swap_with_slice, i128))]
|
2016-03-01 22:00:18 -08:00
|
|
|
#![cfg_attr(test, feature(test, box_heap))]
|
2015-02-05 10:51:17 -05:00
|
|
|
|
2014-05-13 16:10:05 -07:00
|
|
|
// Allow testing this library
|
|
|
|
|
2015-09-24 10:00:54 +12:00
|
|
|
#[cfg(test)]
|
|
|
|
#[macro_use]
|
|
|
|
extern crate std;
|
2017-06-13 15:52:59 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
extern crate test;
|
2017-11-01 12:32:13 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
extern crate rand;
|
2017-06-13 15:52:59 -07:00
|
|
|
|
|
|
|
extern crate std_unicode;
|
2014-05-13 16:10:05 -07:00
|
|
|
|
2016-10-15 16:32:14 +01:00
|
|
|
// Module with internal macros used by other modules (needs to be included before other modules).
|
|
|
|
#[macro_use]
|
|
|
|
mod macros;
|
|
|
|
|
Add API for `Alloc` trait.
Includes `alloc_zeroed` method that `RawVec` has come to depend on.
Exposed private `Layout::from_size_align` ctor to be `pub`, and added
explicit conditions for when it will panic (namely, when `align` is
not power-of-two, or if rounding up `size` to a multiple of `align`
overflows). Normalized all `Layout` construction to go through
`Layout::from_size_align`.
Addressed review feedback regarding `struct Layout` and zero-sized
layouts.
Restrict specification for `dealloc`, adding additional constraint
that the given alignment has to match that used to allocate the block.
(This is a maximally conservative constraint on the alignment. An open
question to resolve (before stabilization) is whether we can return to
a looser constraint such as the one previously specified.)
Split `fn realloc_in_place` into separate `fn grow_in_place` and `fn
shrink_in_place` methods, which have default impls that check against
usable_size for reuse. Make `realloc` default impl try `grow_in_place`
or `shrink_in_place` as appropriate before fallback on
alloc+copy+dealloc.
Drive-by: When reviewing calls to `padding_needed_for`, discovered
what I think was an over-conservative choice for its argument
alignment. Namely, in `fn extend`, we automatically realign the whole
resulting layout to satisfy both old (self) and new alignments. When
the old alignment exceeds the new, this means we would insert
unnecessary padding. So I changed the code to pass in `next.align`
instead of `new_align` to `padding_needed_for`.
Replaced ref to `realloc_in_place` with `grow_in_place`/`shrink_in_place`.
Revised docs replacing my idiosyncratic style of `fn foo` with just
`foo` when referring to the function or method `foo`.
(Alpha-renamed `Allocator` to `Alloc`.)
Post-rebased, added `Debug` derive for `allocator::Excess` to satisfy
`missing_debug_implementations`.
2017-05-23 14:47:09 +02:00
|
|
|
// Allocator trait and helper struct definitions
|
|
|
|
|
|
|
|
pub mod allocator;
|
|
|
|
|
2014-05-13 16:10:05 -07:00
|
|
|
// Heaps provided for low-level allocation strategies
|
|
|
|
|
|
|
|
pub mod heap;
|
|
|
|
|
|
|
|
// Primitive types using the heaps above
|
|
|
|
|
2015-02-17 21:41:32 +01:00
|
|
|
// Need to conditionally define the mod from `boxed.rs` to avoid
|
|
|
|
// duplicating the lang-items when building in test cfg; but also need
|
|
|
|
// to allow code to have `use boxed::HEAP;`
|
|
|
|
// and `use boxed::Box;` declarations.
|
2014-05-13 16:10:05 -07:00
|
|
|
#[cfg(not(test))]
|
2014-07-10 14:19:17 -07:00
|
|
|
pub mod boxed;
|
2015-01-20 23:57:56 +03:00
|
|
|
#[cfg(test)]
|
2015-09-24 10:00:54 +12:00
|
|
|
mod boxed {
|
2017-06-13 15:52:59 -07:00
|
|
|
pub use std::boxed::{Box, IntermediateBox, HEAP};
|
2015-09-24 10:00:54 +12:00
|
|
|
}
|
2015-02-17 21:41:32 +01:00
|
|
|
#[cfg(test)]
|
2015-01-20 23:57:56 +03:00
|
|
|
mod boxed_test;
|
2016-10-30 22:14:05 -05:00
|
|
|
#[cfg(target_has_atomic = "ptr")]
|
2014-05-13 16:10:05 -07:00
|
|
|
pub mod arc;
|
|
|
|
pub mod rc;
|
2015-07-09 21:57:21 -07:00
|
|
|
pub mod raw_vec;
|
2014-05-13 16:10:05 -07:00
|
|
|
|
2017-06-13 15:52:59 -07:00
|
|
|
// collections modules
|
|
|
|
pub mod binary_heap;
|
|
|
|
mod btree;
|
|
|
|
pub mod borrow;
|
|
|
|
pub mod fmt;
|
|
|
|
pub mod linked_list;
|
|
|
|
pub mod range;
|
|
|
|
pub mod slice;
|
|
|
|
pub mod str;
|
|
|
|
pub mod string;
|
|
|
|
pub mod vec;
|
|
|
|
pub mod vec_deque;
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
pub mod btree_map {
|
|
|
|
//! A map based on a B-Tree.
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
pub use btree::map::*;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
pub mod btree_set {
|
|
|
|
//! A set based on a B-Tree.
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
pub use btree::set::*;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(test))]
|
|
|
|
mod std {
|
|
|
|
pub use core::ops; // RangeFull
|
|
|
|
}
|
|
|
|
|
2017-06-30 08:34:53 -10:00
|
|
|
/// An endpoint of a range of keys.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// `Bound`s are range endpoints:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// #![feature(collections_range)]
|
|
|
|
///
|
|
|
|
/// use std::collections::range::RangeArgument;
|
|
|
|
/// use std::collections::Bound::*;
|
|
|
|
///
|
|
|
|
/// assert_eq!((..100).start(), Unbounded);
|
|
|
|
/// assert_eq!((1..12).start(), Included(&1));
|
|
|
|
/// assert_eq!((1..12).end(), Excluded(&12));
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Using a tuple of `Bound`s as an argument to [`BTreeMap::range`].
|
|
|
|
/// Note that in most cases, it's better to use range syntax (`1..5`) instead.
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::collections::BTreeMap;
|
|
|
|
/// use std::collections::Bound::{Excluded, Included, Unbounded};
|
|
|
|
///
|
|
|
|
/// let mut map = BTreeMap::new();
|
|
|
|
/// map.insert(3, "a");
|
|
|
|
/// map.insert(5, "b");
|
|
|
|
/// map.insert(8, "c");
|
|
|
|
///
|
|
|
|
/// for (key, value) in map.range((Excluded(3), Included(8))) {
|
|
|
|
/// println!("{}: {}", key, value);
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// assert_eq!(Some((&3, &"a")), map.range((Unbounded, Included(5))).next());
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// [`BTreeMap::range`]: btree_map/struct.BTreeMap.html#method.range
|
|
|
|
#[stable(feature = "collections_bound", since = "1.17.0")]
|
|
|
|
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
|
|
|
|
pub enum Bound<T> {
|
|
|
|
/// An inclusive bound.
|
|
|
|
#[stable(feature = "collections_bound", since = "1.17.0")]
|
2017-07-16 22:11:46 +03:00
|
|
|
Included(#[stable(feature = "collections_bound", since = "1.17.0")] T),
|
2017-06-30 08:34:53 -10:00
|
|
|
/// An exclusive bound.
|
|
|
|
#[stable(feature = "collections_bound", since = "1.17.0")]
|
2017-07-16 22:11:46 +03:00
|
|
|
Excluded(#[stable(feature = "collections_bound", since = "1.17.0")] T),
|
2017-06-30 08:34:53 -10:00
|
|
|
/// An infinite endpoint. Indicates that there is no bound in this direction.
|
|
|
|
#[stable(feature = "collections_bound", since = "1.17.0")]
|
|
|
|
Unbounded,
|
|
|
|
}
|
2017-06-13 15:52:59 -07:00
|
|
|
|
|
|
|
/// An intermediate trait for specialization of `Extend`.
|
|
|
|
#[doc(hidden)]
|
|
|
|
trait SpecExtend<I: IntoIterator> {
|
|
|
|
/// Extends `self` with the contents of the given iterator.
|
|
|
|
fn spec_extend(&mut self, iter: I);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[doc(no_inline)]
|
|
|
|
pub use binary_heap::BinaryHeap;
|
|
|
|
#[doc(no_inline)]
|
|
|
|
pub use btree_map::BTreeMap;
|
|
|
|
#[doc(no_inline)]
|
|
|
|
pub use btree_set::BTreeSet;
|
|
|
|
#[doc(no_inline)]
|
|
|
|
pub use linked_list::LinkedList;
|
|
|
|
#[doc(no_inline)]
|
|
|
|
pub use vec_deque::VecDeque;
|
|
|
|
#[doc(no_inline)]
|
|
|
|
pub use string::String;
|
|
|
|
#[doc(no_inline)]
|
|
|
|
pub use vec::Vec;
|