]> git.lizzy.rs Git - rust.git/blob - src/liballoc/lib.rs
Auto merge of #29498 - wthrowe:replace-pattern, r=alexcrichton
[rust.git] / src / liballoc / lib.rs
1 // Copyright 2014 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 library
12 //!
13 //! This is the lowest level library through which allocation in Rust can be
14 //! performed.
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 //! Currently, there are four major definitions in this library.
22 //!
23 //! ## Boxed values
24 //!
25 //! The [`Box`](boxed/index.html) type is a smart pointer type. There can
26 //! only be one owner of a `Box`, and the owner can decide to mutate the
27 //! contents, which live on the heap.
28 //!
29 //! This type can be sent among threads efficiently as the size of a `Box` value
30 //! is the same as that of a pointer. Tree-like data structures are often built
31 //! with boxes because each node often has only one owner, the parent.
32 //!
33 //! ## Reference counted pointers
34 //!
35 //! The [`Rc`](rc/index.html) type is a non-threadsafe reference-counted pointer
36 //! type intended for sharing memory within a thread. An `Rc` pointer wraps a
37 //! type, `T`, and only allows access to `&T`, a shared reference.
38 //!
39 //! This type is useful when inherited mutability (such as using `Box`) is too
40 //! constraining for an application, and is often paired with the `Cell` or
41 //! `RefCell` types in order to allow mutation.
42 //!
43 //! ## Atomically reference counted pointers
44 //!
45 //! The [`Arc`](arc/index.html) type is the threadsafe equivalent of the `Rc`
46 //! type. It provides all the same functionality of `Rc`, except it requires
47 //! that the contained type `T` is shareable. Additionally, `Arc<T>` is itself
48 //! sendable while `Rc<T>` is not.
49 //!
50 //! This types allows for shared access to the contained data, and is often
51 //! paired with synchronization primitives such as mutexes to allow mutation of
52 //! shared resources.
53 //!
54 //! ## Heap interfaces
55 //!
56 //! The [`heap`](heap/index.html) module defines the low-level interface to the
57 //! default global allocator. It is not compatible with the libc allocator API.
58
59 #![crate_name = "alloc"]
60 #![crate_type = "rlib"]
61 #![allow(unused_attributes)]
62 #![unstable(feature = "alloc",
63             reason = "this library is unlikely to be stabilized in its current \
64                       form or name",
65             issue = "27783")]
66 #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
67        html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
68        html_root_url = "https://doc.rust-lang.org/nightly/",
69        issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/",
70        test(no_crate_inject, attr(allow(unused_variables), deny(warnings))))]
71 #![no_std]
72 #![needs_allocator]
73
74 #![feature(allocator)]
75 #![feature(box_syntax)]
76 #![feature(coerce_unsized)]
77 #![feature(core_intrinsics)]
78 #![feature(custom_attribute)]
79 #![feature(fundamental)]
80 #![feature(lang_items)]
81 #![feature(num_bits_bytes)]
82 #![feature(optin_builtin_traits)]
83 #![feature(placement_in_syntax)]
84 #![feature(placement_new_protocol)]
85 #![feature(raw)]
86 #![feature(shared)]
87 #![feature(staged_api)]
88 #![feature(unboxed_closures)]
89 #![feature(unique)]
90 #![feature(unsafe_no_drop_flag, filling_drop)]
91 #![feature(dropck_parametricity)]
92 #![feature(unsize)]
93 #![feature(drop_in_place)]
94 #![feature(fn_traits)]
95
96 #![feature(needs_allocator)]
97
98 #![cfg_attr(test, feature(test, rustc_private, box_heap))]
99
100 // Allow testing this library
101
102 #[cfg(test)]
103 #[macro_use]
104 extern crate std;
105 #[cfg(test)]
106 #[macro_use]
107 extern crate log;
108
109 // Heaps provided for low-level allocation strategies
110
111 pub mod heap;
112
113 // Primitive types using the heaps above
114
115 // Need to conditionally define the mod from `boxed.rs` to avoid
116 // duplicating the lang-items when building in test cfg; but also need
117 // to allow code to have `use boxed::HEAP;`
118 // and `use boxed::Box;` declarations.
119 #[cfg(not(test))]
120 pub mod boxed;
121 #[cfg(test)]
122 mod boxed {
123     pub use std::boxed::{Box, HEAP};
124 }
125 #[cfg(test)]
126 mod boxed_test;
127 pub mod arc;
128 pub mod rc;
129 pub mod raw_vec;
130
131 /// Common out-of-memory routine
132 #[cold]
133 #[inline(never)]
134 #[unstable(feature = "oom", reason = "not a scrutinized interface",
135            issue = "27700")]
136 pub fn oom() -> ! {
137     // FIXME(#14674): This really needs to do something other than just abort
138     //                here, but any printing done must be *guaranteed* to not
139     //                allocate.
140     unsafe { core::intrinsics::abort() }
141 }