Skip to content

Commit ba3ba00

Browse files
committed
auto merge of #14538 : alexcrichton/rust/libcollections, r=brson
As with the previous commit with `librand`, this commit shuffles around some `collections` code. The new state of the world is similar to that of librand: * The libcollections crate now only depends on libcore and liballoc. * The standard library has a new module, `std::collections`. All functionality of libcollections is reexported through this module. I would like to stress that this change is purely cosmetic. There are very few alterations to these primitives. There are a number of notable points about the new organization: * std::{str, slice, string, vec} all moved to libcollections. There is no reason that these primitives shouldn't be necessarily usable in a freestanding context that has allocation. These are all reexported in their usual places in the standard library. * The `hashmap`, and transitively the `lru_cache`, modules no longer reside in `libcollections`, but rather in libstd. The reason for this is because the `HashMap::new` contructor requires access to the OSRng for initially seeding the hash map. Beyond this requirement, there is no reason that the hashmap could not move to libcollections. I do, however, have a plan to move the hash map to the collections module. The `HashMap::new` function could be altered to require that the `H` hasher parameter ascribe to the `Default` trait, allowing the entire `hashmap` module to live in libcollections. The key idea would be that the default hasher would be different in libstd. Something along the lines of: // src/libstd/collections/mod.rs pub type HashMap<K, V, H = RandomizedSipHasher> = core_collections::HashMap<K, V, H>; This is not possible today because you cannot invoke static methods through type aliases. If we modified the compiler, however, to allow invocation of static methods through type aliases, then this type definition would essentially be switching the default hasher from `SipHasher` in libcollections to a libstd-defined `RandomizedSipHasher` type. This type's `Default` implementation would randomly seed the `SipHasher` instance, and otherwise perform the same as `SipHasher`. This future state doesn't seem incredibly far off, but until that time comes, the hashmap module will live in libstd to not compromise on functionality. * In preparation for the hashmap moving to libcollections, the `hash` module has moved from libstd to libcollections. A previously snapshotted commit enables a distinct `Writer` trait to live in the `hash` module which `Hash` implementations are now parameterized over. Due to using a custom trait, the `SipHasher` implementation has lost its specialized methods for writing integers. These can be re-added backwards-compatibly in the future via default methods if necessary, but the FNV hashing should satisfy much of the need for speedier hashing. A list of breaking changes: * HashMap::{get, get_mut} no longer fails with the key formatted into the error message with `{:?}`, instead, a generic message is printed. With backtraces, it should still be not-too-hard to track down errors. * The HashMap, HashSet, and LruCache types are now available through std::collections instead of the collections crate. * Manual implementations of hash should be parameterized over `hash::Writer` instead of just `Writer`. [breaking-change]
2 parents 3ec321f + 760b93a commit ba3ba00

File tree

141 files changed

+715
-698
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

141 files changed

+715
-698
lines changed

mk/crates.mk

+12-11
Original file line numberDiff line numberDiff line change
@@ -60,36 +60,36 @@ DEPS_core :=
6060
DEPS_rlibc :=
6161
DEPS_alloc := core libc native:jemalloc
6262
DEPS_debug := std
63-
DEPS_std := core rand libc alloc native:rustrt native:backtrace
63+
DEPS_std := core rand libc alloc collections native:rustrt native:backtrace
6464
DEPS_graphviz := std
6565
DEPS_green := std native:context_switch
6666
DEPS_rustuv := std native:uv native:uv_support
6767
DEPS_native := std
68-
DEPS_syntax := std term serialize collections log fmt_macros debug
68+
DEPS_syntax := std term serialize log fmt_macros debug
6969
DEPS_rustc := syntax native:rustllvm flate arena serialize sync getopts \
70-
collections time log graphviz debug
71-
DEPS_rustdoc := rustc native:hoedown serialize sync getopts collections \
70+
time log graphviz debug
71+
DEPS_rustdoc := rustc native:hoedown serialize sync getopts \
7272
test time debug
7373
DEPS_flate := std native:miniz
74-
DEPS_arena := std collections
74+
DEPS_arena := std
7575
DEPS_graphviz := std
7676
DEPS_glob := std
77-
DEPS_serialize := std collections log
78-
DEPS_term := std collections log
77+
DEPS_serialize := std log
78+
DEPS_term := std log
7979
DEPS_semver := std
8080
DEPS_uuid := std serialize
8181
DEPS_sync := std alloc
8282
DEPS_getopts := std
83-
DEPS_collections := std debug
83+
DEPS_collections := core alloc
8484
DEPS_fourcc := syntax std
8585
DEPS_hexfloat := syntax std
8686
DEPS_num := std
87-
DEPS_test := std collections getopts serialize term time regex
87+
DEPS_test := std getopts serialize term time regex
8888
DEPS_time := std serialize sync
8989
DEPS_rand := core
90-
DEPS_url := std collections
90+
DEPS_url := std
9191
DEPS_log := std sync
92-
DEPS_regex := std collections
92+
DEPS_regex := std
9393
DEPS_regex_macros = syntax std regex
9494
DEPS_fmt_macros = std
9595

@@ -105,6 +105,7 @@ ONLY_RLIB_libc := 1
105105
ONLY_RLIB_rlibc := 1
106106
ONLY_RLIB_alloc := 1
107107
ONLY_RLIB_rand := 1
108+
ONLY_RLIB_collections := 1
108109

109110
################################################################################
110111
# You should not need to edit below this line

src/doc/tutorial.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -2058,8 +2058,7 @@ illegal to copy and pass by value.
20582058
Generic `type`, `struct`, and `enum` declarations follow the same pattern:
20592059

20602060
~~~~
2061-
extern crate collections;
2062-
type Set<T> = collections::HashMap<T, ()>;
2061+
type Set<T> = std::collections::HashMap<T, ()>;
20632062
20642063
struct Stack<T> {
20652064
elements: Vec<T>

src/libarena/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
html_root_url = "http://doc.rust-lang.org/")]
2929
#![allow(missing_doc)]
3030

31-
extern crate collections;
32-
3331
use std::cell::{Cell, RefCell};
3432
use std::cmp;
3533
use std::intrinsics::{TyDesc, get_tydesc};

src/libcollections/bitv.rs

+21-19
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@
1010

1111
#![allow(missing_doc)]
1212

13+
use core::prelude::*;
1314

14-
use std::cmp;
15-
use std::fmt;
16-
use std::iter::RandomAccessIterator;
17-
use std::iter::{Enumerate, Repeat, Map, Zip};
18-
use std::ops;
19-
use std::slice;
20-
use std::uint;
15+
use core::cmp;
16+
use core::fmt;
17+
use core::iter::{Enumerate, Repeat, Map, Zip};
18+
use core::ops;
19+
use core::slice;
20+
use core::uint;
21+
22+
use vec::Vec;
2123

2224
#[deriving(Clone)]
2325
struct SmallBitv {
@@ -977,26 +979,26 @@ impl<'a> Iterator<uint> for BitPositions<'a> {
977979

978980
#[cfg(test)]
979981
mod tests {
980-
extern crate test;
981-
use self::test::Bencher;
982+
use std::prelude::*;
983+
use std::uint;
984+
use std::rand;
985+
use std::rand::Rng;
986+
use test::Bencher;
982987

983988
use bitv::{Bitv, SmallBitv, BigBitv, BitvSet, from_bools, from_fn,
984989
from_bytes};
985990
use bitv;
986-
987-
use std::uint;
988-
use std::rand;
989-
use std::rand::Rng;
991+
use vec::Vec;
990992

991993
static BENCH_BITS : uint = 1 << 14;
992994

993995
#[test]
994996
fn test_to_str() {
995997
let zerolen = Bitv::new(0u, false);
996-
assert_eq!(zerolen.to_str(), "".to_string());
998+
assert_eq!(zerolen.to_str().as_slice(), "");
997999

9981000
let eightbits = Bitv::new(8u, false);
999-
assert_eq!(eightbits.to_str(), "00000000".to_string());
1001+
assert_eq!(eightbits.to_str().as_slice(), "00000000")
10001002
}
10011003

10021004
#[test]
@@ -1019,7 +1021,7 @@ mod tests {
10191021
let mut b = bitv::Bitv::new(2, false);
10201022
b.set(0, true);
10211023
b.set(1, false);
1022-
assert_eq!(b.to_str(), "10".to_string());
1024+
assert_eq!(b.to_str().as_slice(), "10");
10231025
}
10241026

10251027
#[test]
@@ -1330,7 +1332,7 @@ mod tests {
13301332
fn test_from_bytes() {
13311333
let bitv = from_bytes([0b10110110, 0b00000000, 0b11111111]);
13321334
let str = format!("{}{}{}", "10110110", "00000000", "11111111");
1333-
assert_eq!(bitv.to_str(), str);
1335+
assert_eq!(bitv.to_str().as_slice(), str.as_slice());
13341336
}
13351337

13361338
#[test]
@@ -1347,8 +1349,8 @@ mod tests {
13471349

13481350
#[test]
13491351
fn test_from_bools() {
1350-
assert!(from_bools([true, false, true, true]).to_str() ==
1351-
"1011".to_string());
1352+
assert!(from_bools([true, false, true, true]).to_str().as_slice() ==
1353+
"1011");
13521354
}
13531355

13541356
#[test]

src/libcollections/btree.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@
1818
///a length (the height of the tree), and lower and upper bounds on the
1919
///number of elements that a given node can contain.
2020
21-
use std::fmt;
22-
use std::fmt::Show;
21+
use core::prelude::*;
22+
23+
use alloc::owned::Box;
24+
use core::fmt;
25+
use core::fmt::Show;
26+
27+
use vec::Vec;
2328

2429
#[allow(missing_doc)]
2530
pub struct BTree<K, V> {
@@ -772,6 +777,7 @@ impl<K: fmt::Show + Ord, V: fmt::Show> fmt::Show for BranchElt<K, V> {
772777

773778
#[cfg(test)]
774779
mod test_btree {
780+
use std::prelude::*;
775781

776782
use super::{BTree, Node, LeafElt};
777783

src/libcollections/deque.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
//! Container traits for collections
1212
13-
use std::container::Mutable;
13+
use core::prelude::*;
1414

1515
/// A double-ended sequence that allows querying, insertion and deletion at both ends.
1616
pub trait Deque<T> : Mutable {
@@ -41,11 +41,10 @@ pub trait Deque<T> : Mutable {
4141

4242
#[cfg(test)]
4343
pub mod bench {
44-
extern crate test;
45-
use self::test::Bencher;
46-
use std::container::MutableMap;
44+
use std::prelude::*;
4745
use std::rand;
4846
use std::rand::Rng;
47+
use test::Bencher;
4948

5049
pub fn insert_rand_n<M:MutableMap<uint,uint>>(n: uint,
5150
map: &mut M,

src/libcollections/dlist.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@
2121
// Backlinks over DList::prev are raw pointers that form a full chain in
2222
// the reverse direction.
2323

24-
use std::iter;
25-
use std::mem;
26-
use std::ptr;
24+
use core::prelude::*;
25+
26+
use alloc::owned::Box;
27+
use core::iter;
28+
use core::mem;
29+
use core::ptr;
2730

2831
use deque::Deque;
2932

@@ -607,11 +610,14 @@ impl<A: Clone> Clone for DList<A> {
607610

608611
#[cfg(test)]
609612
mod tests {
610-
extern crate test;
611-
use self::test::Bencher;
612-
use deque::Deque;
613+
use std::prelude::*;
613614
use std::rand;
615+
use test::Bencher;
616+
use test;
617+
618+
use deque::Deque;
614619
use super::{DList, Node, ListInsertion};
620+
use vec::Vec;
615621

616622
pub fn check_links<T>(list: &DList<T>) {
617623
let mut len = 0u;

src/libcollections/enum_set.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
//! This module defines a container which uses an efficient bit mask
1414
//! representation to hold C-like enum variants.
1515
16-
use std::num::Bitwise;
16+
use core::prelude::*;
17+
18+
use core::num::Bitwise;
1719

1820
#[deriving(Clone, PartialEq, Eq, Hash, Show)]
1921
/// A specialized Set implementation to use enum types.
@@ -136,7 +138,7 @@ impl<E:CLike> Iterator<E> for Items<E> {
136138

137139
#[cfg(test)]
138140
mod test {
139-
141+
use std::prelude::*;
140142
use std::mem;
141143

142144
use enum_set::{EnumSet, CLike};

src/libstd/hash/mod.rs src/libcollections/hash/mod.rs

+44-33
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,18 @@
6363

6464
#![allow(unused_must_use)]
6565

66-
use container::Container;
67-
use intrinsics::TypeId;
68-
use iter::Iterator;
69-
use option::{Option, Some, None};
70-
use owned::Box;
71-
use rc::Rc;
72-
use result::{Result, Ok, Err};
73-
use slice::{Vector, ImmutableVector};
74-
use str::{Str, StrSlice};
66+
use core::prelude::*;
67+
68+
use alloc::owned::Box;
69+
use alloc::rc::Rc;
70+
use core::intrinsics::TypeId;
71+
use core::mem;
72+
7573
use vec::Vec;
7674

7775
/// Reexport the `sip::hash` function as our default hasher.
7876
pub use hash = self::sip::hash;
7977

80-
pub use Writer = io::Writer;
81-
8278
pub mod sip;
8379

8480
/// A trait that represents a hashable type. The `S` type parameter is an
@@ -96,32 +92,51 @@ pub trait Hasher<S> {
9692
fn hash<T: Hash<S>>(&self, value: &T) -> u64;
9793
}
9894

95+
pub trait Writer {
96+
fn write(&mut self, bytes: &[u8]);
97+
}
98+
9999
//////////////////////////////////////////////////////////////////////////////
100100

101+
fn id<T>(t: T) -> T { t }
102+
101103
macro_rules! impl_hash(
102-
( $( $ty:ty => $method:ident;)* ) => (
104+
( $($ty:ident, $uty:ident, $f:path;)* ) => (
103105
$(
104106
impl<S: Writer> Hash<S> for $ty {
105107
#[inline]
106108
fn hash(&self, state: &mut S) {
107-
state.$method(*self);
109+
let a: [u8, ..::core::$ty::BYTES] = unsafe {
110+
mem::transmute($f(*self as $uty) as $ty)
111+
};
112+
state.write(a.as_slice())
108113
}
109114
}
110115
)*
111116
)
112117
)
113118

114119
impl_hash!(
115-
u8 => write_u8;
116-
u16 => write_le_u16;
117-
u32 => write_le_u32;
118-
u64 => write_le_u64;
119-
uint => write_le_uint;
120-
i8 => write_i8;
121-
i16 => write_le_i16;
122-
i32 => write_le_i32;
123-
i64 => write_le_i64;
124-
int => write_le_int;
120+
u8, u8, id;
121+
u16, u16, mem::to_le16;
122+
u32, u32, mem::to_le32;
123+
u64, u64, mem::to_le64;
124+
i8, u8, id;
125+
i16, u16, mem::to_le16;
126+
i32, u32, mem::to_le32;
127+
i64, u64, mem::to_le64;
128+
)
129+
130+
#[cfg(target_word_size = "32")]
131+
impl_hash!(
132+
uint, u32, mem::to_le32;
133+
int, u32, mem::to_le32;
134+
)
135+
136+
#[cfg(target_word_size = "64")]
137+
impl_hash!(
138+
uint, u64, mem::to_le64;
139+
int, u64, mem::to_le64;
125140
)
126141

127142
impl<S: Writer> Hash<S> for bool {
@@ -142,7 +157,7 @@ impl<'a, S: Writer> Hash<S> for &'a str {
142157
#[inline]
143158
fn hash(&self, state: &mut S) {
144159
state.write(self.as_bytes());
145-
state.write_u8(0xFF);
160+
0xffu8.hash(state)
146161
}
147162
}
148163

@@ -301,14 +316,11 @@ impl<S: Writer, T: Hash<S>, U: Hash<S>> Hash<S> for Result<T, U> {
301316

302317
#[cfg(test)]
303318
mod tests {
304-
use mem;
305-
use io::{IoResult, Writer};
306-
use iter::{Iterator};
307-
use option::{Some, None};
308-
use result::Ok;
309-
use slice::ImmutableVector;
319+
use std::prelude::*;
320+
use std::mem;
310321

311-
use super::{Hash, Hasher};
322+
use slice::ImmutableVector;
323+
use super::{Hash, Hasher, Writer};
312324

313325
struct MyWriterHasher;
314326

@@ -326,11 +338,10 @@ mod tests {
326338

327339
impl Writer for MyWriter {
328340
// Most things we'll just add up the bytes.
329-
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
341+
fn write(&mut self, buf: &[u8]) {
330342
for byte in buf.iter() {
331343
self.hash += *byte as u64;
332344
}
333-
Ok(())
334345
}
335346
}
336347

0 commit comments

Comments
 (0)