Skip to content

Commit 2c1a715

Browse files
committed
Auto merge of #51569 - SimonSapin:liballoc, r=sfackler
Make the public API of the alloc crate a subset of std This only affects **unstable** APIs. I plan to submit an RFC proposing to stabilize the crate. The reason it isn’t stable yet (#27783) is in case we end up merging the standard library crates into one. However the `core` crate is already stable, so if that happens we’ll need to keep it working somehow (likely by making replacing its contents by `pub use` items). We can do the same for `alloc`. This PR will hopefully make this easier, but even if that doesn’t happen consistency with `std` seems good.
2 parents 5fdcd3a + 15bb6c4 commit 2c1a715

File tree

23 files changed

+121
-105
lines changed

23 files changed

+121
-105
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/liballoc/btree/set.rs renamed to src/liballoc/collections/btree/set.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use core::iter::{Peekable, FromIterator, FusedIterator};
1919
use core::ops::{BitOr, BitAnd, BitXor, Sub, RangeBounds};
2020

2121
use borrow::Borrow;
22-
use btree_map::{BTreeMap, Keys};
22+
use collections::btree_map::{self, BTreeMap, Keys};
2323
use super::Recover;
2424

2525
// FIXME(conventions): implement bounded iterators
@@ -104,7 +104,7 @@ impl<'a, T: 'a + fmt::Debug> fmt::Debug for Iter<'a, T> {
104104
#[stable(feature = "rust1", since = "1.0.0")]
105105
#[derive(Debug)]
106106
pub struct IntoIter<T> {
107-
iter: ::btree_map::IntoIter<T, ()>,
107+
iter: btree_map::IntoIter<T, ()>,
108108
}
109109

110110
/// An iterator over a sub-range of items in a `BTreeSet`.
@@ -117,7 +117,7 @@ pub struct IntoIter<T> {
117117
#[derive(Debug)]
118118
#[stable(feature = "btree_range", since = "1.17.0")]
119119
pub struct Range<'a, T: 'a> {
120-
iter: ::btree_map::Range<'a, T, ()>,
120+
iter: btree_map::Range<'a, T, ()>,
121121
}
122122

123123
/// A lazy iterator producing elements in the difference of `BTreeSet`s.
File renamed without changes.

src/liballoc/collections/mod.rs

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright 2018 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+
//! Collection types.
12+
13+
#![stable(feature = "rust1", since = "1.0.0")]
14+
15+
pub mod binary_heap;
16+
mod btree;
17+
pub mod linked_list;
18+
pub mod vec_deque;
19+
20+
#[stable(feature = "rust1", since = "1.0.0")]
21+
pub mod btree_map {
22+
//! A map based on a B-Tree.
23+
#[stable(feature = "rust1", since = "1.0.0")]
24+
pub use super::btree::map::*;
25+
}
26+
27+
#[stable(feature = "rust1", since = "1.0.0")]
28+
pub mod btree_set {
29+
//! A set based on a B-Tree.
30+
#[stable(feature = "rust1", since = "1.0.0")]
31+
pub use super::btree::set::*;
32+
}
33+
34+
#[stable(feature = "rust1", since = "1.0.0")]
35+
#[doc(no_inline)]
36+
pub use self::binary_heap::BinaryHeap;
37+
38+
#[stable(feature = "rust1", since = "1.0.0")]
39+
#[doc(no_inline)]
40+
pub use self::btree_map::BTreeMap;
41+
42+
#[stable(feature = "rust1", since = "1.0.0")]
43+
#[doc(no_inline)]
44+
pub use self::btree_set::BTreeSet;
45+
46+
#[stable(feature = "rust1", since = "1.0.0")]
47+
#[doc(no_inline)]
48+
pub use self::linked_list::LinkedList;
49+
50+
#[stable(feature = "rust1", since = "1.0.0")]
51+
#[doc(no_inline)]
52+
pub use self::vec_deque::VecDeque;
53+
54+
use alloc::{AllocErr, LayoutErr};
55+
56+
/// Augments `AllocErr` with a CapacityOverflow variant.
57+
#[derive(Clone, PartialEq, Eq, Debug)]
58+
#[unstable(feature = "try_reserve", reason = "new API", issue="48043")]
59+
pub enum CollectionAllocErr {
60+
/// Error due to the computed capacity exceeding the collection's maximum
61+
/// (usually `isize::MAX` bytes).
62+
CapacityOverflow,
63+
/// Error due to the allocator (see the `AllocErr` type's docs).
64+
AllocErr,
65+
}
66+
67+
#[unstable(feature = "try_reserve", reason = "new API", issue="48043")]
68+
impl From<AllocErr> for CollectionAllocErr {
69+
#[inline]
70+
fn from(AllocErr: AllocErr) -> Self {
71+
CollectionAllocErr::AllocErr
72+
}
73+
}
74+
75+
#[unstable(feature = "try_reserve", reason = "new API", issue="48043")]
76+
impl From<LayoutErr> for CollectionAllocErr {
77+
#[inline]
78+
fn from(_: LayoutErr) -> Self {
79+
CollectionAllocErr::CapacityOverflow
80+
}
81+
}
82+
83+
/// An intermediate trait for specialization of `Extend`.
84+
#[doc(hidden)]
85+
trait SpecExtend<I: IntoIterator> {
86+
/// Extends `self` with the contents of the given iterator.
87+
fn spec_extend(&mut self, iter: I);
88+
}

src/liballoc/vec_deque.rs renamed to src/liballoc/collections/vec_deque.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use core::slice;
3030
use core::hash::{Hash, Hasher};
3131
use core::cmp;
3232

33-
use alloc::CollectionAllocErr;
33+
use collections::CollectionAllocErr;
3434
use raw_vec::RawVec;
3535
use vec::Vec;
3636

@@ -2891,7 +2891,7 @@ mod tests {
28912891

28922892
#[test]
28932893
fn test_from_vec() {
2894-
use super::super::vec::Vec;
2894+
use vec::Vec;
28952895
for cap in 0..35 {
28962896
for len in 0..cap + 1 {
28972897
let mut vec = Vec::with_capacity(cap);
@@ -2907,7 +2907,7 @@ mod tests {
29072907

29082908
#[test]
29092909
fn test_vec_from_vecdeque() {
2910-
use super::super::vec::Vec;
2910+
use vec::Vec;
29112911

29122912
fn create_vec_and_test_convert(cap: usize, offset: usize, len: usize) {
29132913
let mut vd = VecDeque::with_capacity(cap);

src/liballoc/lib.rs

+7-54
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
//! This library provides smart pointers and collections for managing
1414
//! heap-allocated values.
1515
//!
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.
16+
//! This library, like libcore, normally doesn’t need to be used directly
17+
//! since its contents are re-exported in the [`std` crate](../std/index.html).
18+
//! Crates that use the `#![no_std]` attribute however will typically
19+
//! not depend on `std`, so they’d use this crate instead.
2020
//!
2121
//! ## Boxed values
2222
//!
@@ -40,7 +40,7 @@
4040
//!
4141
//! ## Atomically reference counted pointers
4242
//!
43-
//! The [`Arc`](arc/index.html) type is the threadsafe equivalent of the `Rc`
43+
//! The [`Arc`](sync/index.html) type is the threadsafe equivalent of the `Rc`
4444
//! type. It provides all the same functionality of `Rc`, except it requires
4545
//! that the contained type `T` is shareable. Additionally, `Arc<T>` is itself
4646
//! sendable while `Rc<T>` is not.
@@ -141,13 +141,6 @@ extern crate rand;
141141
#[macro_use]
142142
mod macros;
143143

144-
#[rustc_deprecated(since = "1.27.0", reason = "use the heap module in core, alloc, or std instead")]
145-
#[unstable(feature = "allocator_api", issue = "32838")]
146-
/// Use the `alloc` module instead.
147-
pub mod allocator {
148-
pub use alloc::*;
149-
}
150-
151144
// Heaps provided for low-level allocation strategies
152145

153146
pub mod alloc;
@@ -169,60 +162,20 @@ mod boxed {
169162
}
170163
#[cfg(test)]
171164
mod boxed_test;
165+
pub mod collections;
172166
#[cfg(target_has_atomic = "ptr")]
173-
pub mod arc;
167+
pub mod sync;
174168
pub mod rc;
175169
pub mod raw_vec;
176170

177-
// collections modules
178-
pub mod binary_heap;
179-
mod btree;
180171
pub mod borrow;
181172
pub mod fmt;
182-
pub mod linked_list;
183173
pub mod slice;
184174
pub mod str;
185175
pub mod string;
186176
pub mod vec;
187-
pub mod vec_deque;
188-
189-
#[stable(feature = "rust1", since = "1.0.0")]
190-
pub mod btree_map {
191-
//! A map based on a B-Tree.
192-
#[stable(feature = "rust1", since = "1.0.0")]
193-
pub use btree::map::*;
194-
}
195-
196-
#[stable(feature = "rust1", since = "1.0.0")]
197-
pub mod btree_set {
198-
//! A set based on a B-Tree.
199-
#[stable(feature = "rust1", since = "1.0.0")]
200-
pub use btree::set::*;
201-
}
202177

203178
#[cfg(not(test))]
204179
mod std {
205180
pub use core::ops; // RangeFull
206181
}
207-
208-
/// An intermediate trait for specialization of `Extend`.
209-
#[doc(hidden)]
210-
trait SpecExtend<I: IntoIterator> {
211-
/// Extends `self` with the contents of the given iterator.
212-
fn spec_extend(&mut self, iter: I);
213-
}
214-
215-
#[doc(no_inline)]
216-
pub use binary_heap::BinaryHeap;
217-
#[doc(no_inline)]
218-
pub use btree_map::BTreeMap;
219-
#[doc(no_inline)]
220-
pub use btree_set::BTreeSet;
221-
#[doc(no_inline)]
222-
pub use linked_list::LinkedList;
223-
#[doc(no_inline)]
224-
pub use vec_deque::VecDeque;
225-
#[doc(no_inline)]
226-
pub use string::String;
227-
#[doc(no_inline)]
228-
pub use vec::Vec;

src/liballoc/raw_vec.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
#![unstable(feature = "raw_vec_internals", reason = "implemention detail", issue = "0")]
12+
#![doc(hidden)]
13+
1114
use core::cmp;
1215
use core::mem;
1316
use core::ops::Drop;
1417
use core::ptr::{self, NonNull, Unique};
1518
use core::slice;
1619

1720
use alloc::{Alloc, Layout, Global, handle_alloc_error};
18-
use alloc::CollectionAllocErr;
19-
use alloc::CollectionAllocErr::*;
21+
use collections::CollectionAllocErr;
22+
use collections::CollectionAllocErr::*;
2023
use boxed::Box;
2124

2225
/// A low-level utility for more ergonomically allocating, reallocating, and deallocating
@@ -264,7 +267,7 @@ impl<T, A: Alloc> RawVec<T, A> {
264267
/// # Examples
265268
///
266269
/// ```
267-
/// # #![feature(alloc)]
270+
/// # #![feature(alloc, raw_vec_internals)]
268271
/// # extern crate alloc;
269272
/// # use std::ptr;
270273
/// # use alloc::raw_vec::RawVec;
@@ -468,7 +471,7 @@ impl<T, A: Alloc> RawVec<T, A> {
468471
/// # Examples
469472
///
470473
/// ```
471-
/// # #![feature(alloc)]
474+
/// # #![feature(alloc, raw_vec_internals)]
472475
/// # extern crate alloc;
473476
/// # use std::ptr;
474477
/// # use alloc::raw_vec::RawVec;

src/liballoc/str.rs

-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ use boxed::Box;
5151
use slice::{SliceConcatExt, SliceIndex};
5252
use string::String;
5353
use vec::Vec;
54-
use vec_deque::VecDeque;
5554

5655
#[stable(feature = "rust1", since = "1.0.0")]
5756
pub use core::str::{FromStr, Utf8Error};

src/liballoc/string.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use core::ptr;
6666
use core::str::pattern::Pattern;
6767
use core::str::lossy;
6868

69-
use alloc::CollectionAllocErr;
69+
use collections::CollectionAllocErr;
7070
use borrow::{Cow, ToOwned};
7171
use boxed::Box;
7272
use str::{self, from_boxed_utf8_unchecked, FromStr, Utf8Error, Chars};
File renamed without changes.

src/liballoc/task.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ pub use self::if_arc::*;
1818
#[cfg(target_has_atomic = "ptr")]
1919
mod if_arc {
2020
use super::*;
21-
use arc::Arc;
2221
use core::marker::PhantomData;
2322
use core::mem;
2423
use core::ptr::{self, NonNull};
24+
use sync::Arc;
2525

2626
/// A way of waking up a specific task.
2727
///

src/liballoc/vec.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ use core::ptr;
8080
use core::ptr::NonNull;
8181
use core::slice;
8282

83-
use alloc::CollectionAllocErr;
83+
use collections::CollectionAllocErr;
8484
use borrow::ToOwned;
8585
use borrow::Cow;
8686
use boxed::Box;

src/libarena/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#![feature(alloc)]
2727
#![feature(core_intrinsics)]
2828
#![feature(dropck_eyepatch)]
29+
#![feature(raw_vec_internals)]
2930
#![cfg_attr(test, feature(test))]
3031

3132
#![allow(deprecated)]

src/libcore/alloc.rs

-28
Original file line numberDiff line numberDiff line change
@@ -385,34 +385,6 @@ impl fmt::Display for CannotReallocInPlace {
385385
}
386386
}
387387

388-
/// Augments `AllocErr` with a CapacityOverflow variant.
389-
// FIXME: should this be in libcore or liballoc?
390-
#[derive(Clone, PartialEq, Eq, Debug)]
391-
#[unstable(feature = "try_reserve", reason = "new API", issue="48043")]
392-
pub enum CollectionAllocErr {
393-
/// Error due to the computed capacity exceeding the collection's maximum
394-
/// (usually `isize::MAX` bytes).
395-
CapacityOverflow,
396-
/// Error due to the allocator (see the `AllocErr` type's docs).
397-
AllocErr,
398-
}
399-
400-
#[unstable(feature = "try_reserve", reason = "new API", issue="48043")]
401-
impl From<AllocErr> for CollectionAllocErr {
402-
#[inline]
403-
fn from(AllocErr: AllocErr) -> Self {
404-
CollectionAllocErr::AllocErr
405-
}
406-
}
407-
408-
#[unstable(feature = "try_reserve", reason = "new API", issue="48043")]
409-
impl From<LayoutErr> for CollectionAllocErr {
410-
#[inline]
411-
fn from(_: LayoutErr) -> Self {
412-
CollectionAllocErr::CapacityOverflow
413-
}
414-
}
415-
416388
/// A memory allocator that can be registered as the standard library’s default
417389
/// though the `#[global_allocator]` attributes.
418390
///

src/libstd/collections/hash/map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use self::Entry::*;
1212
use self::VacantEntryState::*;
1313

14-
use alloc::CollectionAllocErr;
14+
use collections::CollectionAllocErr;
1515
use cell::Cell;
1616
use borrow::Borrow;
1717
use cmp::max;

src/libstd/collections/hash/table.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use alloc::{Global, Alloc, Layout, LayoutErr, CollectionAllocErr, handle_alloc_error};
11+
use alloc::{Global, Alloc, Layout, LayoutErr, handle_alloc_error};
12+
use collections::CollectionAllocErr;
1213
use hash::{BuildHasher, Hash, Hasher};
1314
use marker;
1415
use mem::{size_of, needs_drop};

0 commit comments

Comments
 (0)