Skip to content

Commit 507e3f2

Browse files
committed
Add allocator type to more collection types
Adds `A: AllocRef = Global` to `BinaryHeap`, `BTreeMap`, `BTreeSet`, `LinkedList`, `VecDeque`, `Rc`, `Arc`, `Vec`, `String`, `HashMap`, and `HashSet`
1 parent e31e87d commit 507e3f2

File tree

11 files changed

+154
-63
lines changed

11 files changed

+154
-63
lines changed

src/liballoc/collections/binary_heap.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ use core::mem::{self, size_of, swap, ManuallyDrop};
150150
use core::ops::{Deref, DerefMut};
151151
use core::ptr;
152152

153+
use crate::alloc::{AllocRef, Global};
153154
use crate::slice;
154155
use crate::vec::{self, Vec};
155156

@@ -245,8 +246,8 @@ use super::SpecExtend;
245246
/// [peek]: #method.peek
246247
/// [peek\_mut]: #method.peek_mut
247248
#[stable(feature = "rust1", since = "1.0.0")]
248-
pub struct BinaryHeap<T> {
249-
data: Vec<T>,
249+
pub struct BinaryHeap<T, A: AllocRef = Global> {
250+
data: Vec<T, A>,
250251
}
251252

252253
/// Structure wrapping a mutable reference to the greatest item on a

src/liballoc/collections/btree/map.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use core::{fmt, ptr};
1212
use super::node::{self, marker, ForceResult::*, Handle, InsertResult::*, NodeRef};
1313
use super::search::{self, SearchResult::*};
1414
use super::unwrap_unchecked;
15+
use crate::alloc::{AllocRef, Global};
1516

1617
use Entry::*;
1718
use UnderflowResult::*;
@@ -122,13 +123,14 @@ use UnderflowResult::*;
122123
/// *stat += random_stat_buff();
123124
/// ```
124125
#[stable(feature = "rust1", since = "1.0.0")]
125-
pub struct BTreeMap<K, V> {
126+
pub struct BTreeMap<K, V, A: AllocRef = Global> {
126127
root: Option<node::Root<K, V>>,
127128
length: usize,
129+
alloc: PhantomData<A>,
128130
}
129131

130132
#[stable(feature = "btree_drop", since = "1.7.0")]
131-
unsafe impl<#[may_dangle] K, #[may_dangle] V> Drop for BTreeMap<K, V> {
133+
unsafe impl<#[may_dangle] K, #[may_dangle] V, A: AllocRef> Drop for BTreeMap<K, V, A> {
132134
fn drop(&mut self) {
133135
unsafe {
134136
drop(ptr::read(self).into_iter());
@@ -148,7 +150,11 @@ impl<K: Clone, V: Clone> Clone for BTreeMap<K, V> {
148150
{
149151
match node.force() {
150152
Leaf(leaf) => {
151-
let mut out_tree = BTreeMap { root: Some(node::Root::new_leaf()), length: 0 };
153+
let mut out_tree = BTreeMap {
154+
root: Some(node::Root::new_leaf()),
155+
length: 0,
156+
alloc: PhantomData,
157+
};
152158

153159
{
154160
let root = out_tree.root.as_mut().unwrap();
@@ -210,7 +216,7 @@ impl<K: Clone, V: Clone> Clone for BTreeMap<K, V> {
210216
if self.is_empty() {
211217
// Ideally we'd call `BTreeMap::new` here, but that has the `K:
212218
// Ord` constraint, which this method lacks.
213-
BTreeMap { root: None, length: 0 }
219+
BTreeMap { root: None, length: 0, alloc: PhantomData }
214220
} else {
215221
clone_subtree(self.root.as_ref().unwrap().as_ref())
216222
}
@@ -558,7 +564,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
558564
#[stable(feature = "rust1", since = "1.0.0")]
559565
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")]
560566
pub const fn new() -> BTreeMap<K, V> {
561-
BTreeMap { root: None, length: 0 }
567+
BTreeMap { root: None, length: 0, alloc: PhantomData }
562568
}
563569

564570
/// Clears the map, removing all elements.
@@ -1535,7 +1541,7 @@ impl<K, V> ExactSizeIterator for IterMut<'_, K, V> {
15351541
impl<K, V> FusedIterator for IterMut<'_, K, V> {}
15361542

15371543
#[stable(feature = "rust1", since = "1.0.0")]
1538-
impl<K, V> IntoIterator for BTreeMap<K, V> {
1544+
impl<K, V, A: AllocRef> IntoIterator for BTreeMap<K, V, A> {
15391545
type Item = (K, V);
15401546
type IntoIter = IntoIter<K, V>;
15411547

src/liballoc/collections/btree/set.rs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
// to TreeMap
33

44
use core::borrow::Borrow;
5-
use core::cmp::Ordering::{Equal, Greater, Less};
5+
use core::cmp::Ordering::{self, Equal, Greater, Less};
66
use core::cmp::{max, min};
77
use core::fmt::{self, Debug};
8+
use core::hash;
89
use core::iter::{FromIterator, FusedIterator, Peekable};
910
use core::ops::{BitAnd, BitOr, BitXor, RangeBounds, Sub};
1011

1112
use super::map::{BTreeMap, Keys};
1213
use super::Recover;
14+
use crate::alloc::{AllocRef, Global};
1315

1416
// FIXME(conventions): implement bounded iterators
1517

@@ -56,10 +58,44 @@ use super::Recover;
5658
/// println!("{}", book);
5759
/// }
5860
/// ```
59-
#[derive(Hash, PartialEq, Eq, Ord, PartialOrd)]
6061
#[stable(feature = "rust1", since = "1.0.0")]
61-
pub struct BTreeSet<T> {
62-
map: BTreeMap<T, ()>,
62+
pub struct BTreeSet<T, A: AllocRef = Global> {
63+
map: BTreeMap<T, (), A>,
64+
}
65+
66+
#[stable(feature = "rust1", since = "1.0.0")]
67+
impl<T: PartialEq> PartialEq for BTreeSet<T> {
68+
#[inline]
69+
fn eq(&self, other: &Self) -> bool {
70+
self.map.eq(&other.map)
71+
}
72+
}
73+
74+
#[stable(feature = "rust1", since = "1.0.0")]
75+
impl<T: Eq> Eq for BTreeSet<T> {}
76+
77+
#[stable(feature = "rust1", since = "1.0.0")]
78+
impl<T: PartialOrd> PartialOrd for BTreeSet<T> {
79+
#[inline]
80+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
81+
self.map.partial_cmp(&other.map)
82+
}
83+
}
84+
85+
#[stable(feature = "rust1", since = "1.0.0")]
86+
impl<T: Ord> Ord for BTreeSet<T> {
87+
#[inline]
88+
fn cmp(&self, other: &Self) -> Ordering {
89+
self.map.cmp(&other.map)
90+
}
91+
}
92+
93+
#[stable(feature = "rust1", since = "1.0.0")]
94+
impl<T: hash::Hash> hash::Hash for BTreeSet<T> {
95+
#[inline]
96+
fn hash<H: hash::Hasher>(&self, state: &mut H) {
97+
self.map.hash(state)
98+
}
6399
}
64100

65101
#[stable(feature = "rust1", since = "1.0.0")]

src/liballoc/collections/linked_list.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use core::mem;
2121
use core::ptr::NonNull;
2222

2323
use super::SpecExtend;
24+
use crate::alloc::{AllocRef, Global};
2425
use crate::boxed::Box;
2526

2627
#[cfg(test)]
@@ -35,11 +36,12 @@ mod tests;
3536
/// array-based containers are generally faster,
3637
/// more memory efficient, and make better use of CPU cache.
3738
#[stable(feature = "rust1", since = "1.0.0")]
38-
pub struct LinkedList<T> {
39+
pub struct LinkedList<T, A: AllocRef = Global> {
3940
head: Option<NonNull<Node<T>>>,
4041
tail: Option<NonNull<Node<T>>>,
4142
len: usize,
4243
marker: PhantomData<Box<Node<T>>>,
44+
alloc: PhantomData<A>,
4345
}
4446

4547
struct Node<T> {
@@ -134,7 +136,7 @@ impl<T> Node<T> {
134136
}
135137

136138
// private methods
137-
impl<T> LinkedList<T> {
139+
impl<T, A: AllocRef> LinkedList<T, A> {
138140
/// Adds the given node to the front of the list.
139141
#[inline]
140142
fn push_front_node(&mut self, mut node: Box<Node<T>>) {
@@ -216,7 +218,10 @@ impl<T> LinkedList<T> {
216218
node
217219
})
218220
}
221+
}
219222

223+
// private methods
224+
impl<T> LinkedList<T> {
220225
/// Unlinks the specified node from the current list.
221226
///
222227
/// Warning: this will not check that the provided node belongs to the current list.
@@ -310,6 +315,7 @@ impl<T> LinkedList<T> {
310315
tail: first_part_tail,
311316
len: at,
312317
marker: PhantomData,
318+
alloc: PhantomData,
313319
};
314320

315321
// Fix the head ptr of the second part
@@ -346,6 +352,7 @@ impl<T> LinkedList<T> {
346352
tail: second_part_tail,
347353
len: self.len - at,
348354
marker: PhantomData,
355+
alloc: PhantomData,
349356
};
350357

351358
// Fix the tail ptr of the first part
@@ -382,7 +389,7 @@ impl<T> LinkedList<T> {
382389
#[rustc_const_stable(feature = "const_linked_list_new", since = "1.32.0")]
383390
#[stable(feature = "rust1", since = "1.0.0")]
384391
pub const fn new() -> Self {
385-
LinkedList { head: None, tail: None, len: 0, marker: PhantomData }
392+
LinkedList { head: None, tail: None, len: 0, marker: PhantomData, alloc: PhantomData }
386393
}
387394

388395
/// Moves all elements from `other` to the end of the list.
@@ -964,11 +971,11 @@ impl<T> LinkedList<T> {
964971
}
965972

966973
#[stable(feature = "rust1", since = "1.0.0")]
967-
unsafe impl<#[may_dangle] T> Drop for LinkedList<T> {
974+
unsafe impl<#[may_dangle] T, A: AllocRef> Drop for LinkedList<T, A> {
968975
fn drop(&mut self) {
969-
struct DropGuard<'a, T>(&'a mut LinkedList<T>);
976+
struct DropGuard<'a, T, A: AllocRef>(&'a mut LinkedList<T, A>);
970977

971-
impl<'a, T> Drop for DropGuard<'a, T> {
978+
impl<'a, T, A: AllocRef> Drop for DropGuard<'a, T, A> {
972979
fn drop(&mut self) {
973980
// Continue the same loop we do below. This only runs when a destructor has
974981
// panicked. If another one panics this will abort.

src/liballoc/collections/vec_deque.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use core::ops::{Index, IndexMut, RangeBounds, Try};
1818
use core::ptr::{self, NonNull};
1919
use core::slice;
2020

21+
use crate::alloc::{AllocRef, Global};
2122
use crate::collections::TryReserveError;
2223
use crate::raw_vec::RawVec;
2324
use crate::vec::Vec;
@@ -52,15 +53,15 @@ const MAXIMUM_ZST_CAPACITY: usize = 1 << (64 - 1); // Largest possible power of
5253
/// [`append`]: #method.append
5354
#[cfg_attr(not(test), rustc_diagnostic_item = "vecdeque_type")]
5455
#[stable(feature = "rust1", since = "1.0.0")]
55-
pub struct VecDeque<T> {
56+
pub struct VecDeque<T, A: AllocRef = Global> {
5657
// tail and head are pointers into the buffer. Tail always points
5758
// to the first element that could be read, Head always points
5859
// to where data should be written.
5960
// If tail == head the buffer is empty. The length of the ringbuffer
6061
// is defined as the distance between the two.
6162
tail: usize,
6263
head: usize,
63-
buf: RawVec<T>,
64+
buf: RawVec<T, A>,
6465
}
6566

6667
/// PairSlices pairs up equal length slice parts of two deques
@@ -147,7 +148,7 @@ impl<T: Clone> Clone for VecDeque<T> {
147148
}
148149

149150
#[stable(feature = "rust1", since = "1.0.0")]
150-
unsafe impl<#[may_dangle] T> Drop for VecDeque<T> {
151+
unsafe impl<#[may_dangle] T, A: AllocRef> Drop for VecDeque<T, A> {
151152
fn drop(&mut self) {
152153
/// Runs the destructor for all items in the slice when it gets dropped (normally or
153154
/// during unwinding).
@@ -180,7 +181,7 @@ impl<T> Default for VecDeque<T> {
180181
}
181182
}
182183

183-
impl<T> VecDeque<T> {
184+
impl<T, A: AllocRef> VecDeque<T, A> {
184185
/// Marginally more convenient
185186
#[inline]
186187
fn ptr(&self) -> *mut T {
@@ -956,7 +957,9 @@ impl<T> VecDeque<T> {
956957
pub fn iter_mut(&mut self) -> IterMut<'_, T> {
957958
IterMut { tail: self.tail, head: self.head, ring: unsafe { self.buffer_as_mut_slice() } }
958959
}
960+
}
959961

962+
impl<T, A: AllocRef> VecDeque<T, A> {
960963
/// Returns a pair of slices which contain, in order, the contents of the
961964
/// `VecDeque`.
962965
///
@@ -1057,7 +1060,9 @@ impl<T> VecDeque<T> {
10571060
pub fn is_empty(&self) -> bool {
10581061
self.tail == self.head
10591062
}
1063+
}
10601064

1065+
impl<T> VecDeque<T> {
10611066
/// Creates a draining iterator that removes the specified range in the
10621067
/// `VecDeque` and yields the removed items.
10631068
///

src/liballoc/rc.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,10 @@ struct RcBox<T: ?Sized> {
281281
/// [get_mut]: #method.get_mut
282282
#[cfg_attr(not(test), rustc_diagnostic_item = "Rc")]
283283
#[stable(feature = "rust1", since = "1.0.0")]
284-
pub struct Rc<T: ?Sized> {
284+
pub struct Rc<T: ?Sized, A: AllocRef = Global> {
285285
ptr: NonNull<RcBox<T>>,
286286
phantom: PhantomData<RcBox<T>>,
287+
alloc: PhantomData<A>,
287288
}
288289

289290
#[stable(feature = "rust1", since = "1.0.0")]
@@ -299,7 +300,7 @@ impl<T: ?Sized + Unsize<U>, U: ?Sized> DispatchFromDyn<Rc<U>> for Rc<T> {}
299300

300301
impl<T: ?Sized> Rc<T> {
301302
fn from_inner(ptr: NonNull<RcBox<T>>) -> Self {
302-
Self { ptr, phantom: PhantomData }
303+
Self { ptr, phantom: PhantomData, alloc: PhantomData }
303304
}
304305

305306
unsafe fn from_ptr(ptr: *mut RcBox<T>) -> Self {
@@ -1114,7 +1115,7 @@ impl<T: ?Sized> Deref for Rc<T> {
11141115
impl<T: ?Sized> Receiver for Rc<T> {}
11151116

11161117
#[stable(feature = "rust1", since = "1.0.0")]
1117-
unsafe impl<#[may_dangle] T: ?Sized> Drop for Rc<T> {
1118+
unsafe impl<#[may_dangle] T: ?Sized, A: AllocRef> Drop for Rc<T, A> {
11181119
/// Drops the `Rc`.
11191120
///
11201121
/// This will decrement the strong reference count. If the strong reference
@@ -2074,7 +2075,7 @@ trait RcBoxPtr<T: ?Sized> {
20742075
}
20752076
}
20762077

2077-
impl<T: ?Sized> RcBoxPtr<T> for Rc<T> {
2078+
impl<T: ?Sized, A: AllocRef> RcBoxPtr<T> for Rc<T, A> {
20782079
#[inline(always)]
20792080
fn inner(&self) -> &RcBox<T> {
20802081
unsafe { self.ptr.as_ref() }

src/liballoc/string.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#![stable(feature = "rust1", since = "1.0.0")]
4848

4949
use core::char::{decode_utf16, REPLACEMENT_CHARACTER};
50+
use core::cmp::Ordering;
5051
use core::fmt;
5152
use core::hash;
5253
use core::iter::{FromIterator, FusedIterator};
@@ -55,6 +56,7 @@ use core::ops::{self, Add, AddAssign, Index, IndexMut, RangeBounds};
5556
use core::ptr;
5657
use core::str::{lossy, pattern::Pattern};
5758

59+
use crate::alloc::{AllocRef, Global};
5860
use crate::borrow::{Cow, ToOwned};
5961
use crate::boxed::Box;
6062
use crate::collections::TryReserveError;
@@ -277,11 +279,10 @@ use crate::vec::Vec;
277279
/// [`&str`]: ../../std/primitive.str.html
278280
/// [`Deref`]: ../../std/ops/trait.Deref.html
279281
/// [`as_str()`]: struct.String.html#method.as_str
280-
#[derive(PartialOrd, Eq, Ord)]
281282
#[cfg_attr(not(test), rustc_diagnostic_item = "string_type")]
282283
#[stable(feature = "rust1", since = "1.0.0")]
283-
pub struct String {
284-
vec: Vec<u8>,
284+
pub struct String<A: AllocRef = Global> {
285+
vec: Vec<u8, A>,
285286
}
286287

287288
/// A possible error value when converting a `String` from a UTF-8 byte vector.
@@ -1886,6 +1887,25 @@ impl PartialEq for String {
18861887
}
18871888
}
18881889

1890+
#[stable(feature = "rust1", since = "1.0.0")]
1891+
impl Eq for String {}
1892+
1893+
#[stable(feature = "rust1", since = "1.0.0")]
1894+
impl PartialOrd for String {
1895+
#[inline]
1896+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
1897+
self.vec.partial_cmp(&other.vec)
1898+
}
1899+
}
1900+
1901+
#[stable(feature = "rust1", since = "1.0.0")]
1902+
impl Ord for String {
1903+
#[inline]
1904+
fn cmp(&self, other: &Self) -> Ordering {
1905+
self.vec.cmp(&other.vec)
1906+
}
1907+
}
1908+
18891909
macro_rules! impl_eq {
18901910
($lhs:ty, $rhs: ty) => {
18911911
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)