Skip to content

Commit 2a14e08

Browse files
committed
Move std::{trie, hashmap} to libcollections
These two containers are indeed collections, so their place is in libcollections, not in libstd. There will always be a hash map as part of the standard distribution of Rust, but by moving it out of the standard library it makes libstd that much more portable to more platforms and environments. This conveniently also removes the stuttering of 'std::hashmap::HashMap', although 'collections::HashMap' is only one character shorter.
1 parent edf351e commit 2a14e08

File tree

109 files changed

+448
-436
lines changed

Some content is hidden

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

109 files changed

+448
-436
lines changed

mk/crates.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ DEPS_flate := std native:miniz
6969
DEPS_arena := std collections
7070
DEPS_glob := std
7171
DEPS_serialize := std
72-
DEPS_term := std
72+
DEPS_term := std collections
7373
DEPS_semver := std
7474
DEPS_uuid := std serialize
7575
DEPS_sync := std

src/doc/rust.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ expression context, the final namespace qualifier is omitted.
467467
Two examples of paths with type arguments:
468468

469469
~~~~
470-
# use std::hashmap::HashMap;
470+
# struct HashMap<K, V>;
471471
# fn f() {
472472
# fn id<T>(t: T) -> T { t }
473473
type T = HashMap<int,~str>; // Type arguments used in a type expression

src/doc/tutorial.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -1977,8 +1977,8 @@ illegal to copy and pass by value.
19771977
Generic `type`, `struct`, and `enum` declarations follow the same pattern:
19781978
19791979
~~~~
1980-
use std::hashmap::HashMap;
1981-
type Set<T> = HashMap<T, ()>;
1980+
extern crate collections;
1981+
type Set<T> = collections::HashMap<T, ()>;
19821982
19831983
struct Stack<T> {
19841984
elements: ~[T]
@@ -1988,6 +1988,7 @@ enum Option<T> {
19881988
Some(T),
19891989
None
19901990
}
1991+
# fn main() {}
19911992
~~~~
19921993
19931994
These declarations can be instantiated to valid types like `Set<int>`,

src/etc/combine-tests.py

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ def scrub(b):
5555
#[crate_id=\"run_pass_stage2#0.1\"];
5656
#[feature(globs, macro_rules, struct_variant, managed_boxes)];
5757
#[allow(warnings)];
58+
extern crate collections;
59+
extern crate extra;
5860
"""
5961
)
6062
for t in stage2_tests:

src/libstd/hashmap.rs src/libcollections/hashmap.rs

+129-27
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
//! # Example
1717
//!
1818
//! ```rust
19-
//! use std::hashmap::HashMap;
19+
//! use collections::HashMap;
2020
//!
2121
//! // type inference lets us omit an explicit type signature (which
2222
//! // would be `HashMap<&str, &str>` in this example).
@@ -52,24 +52,20 @@
5252
//! }
5353
//! ```
5454
55-
use container::{Container, Mutable, Map, MutableMap, Set, MutableSet};
56-
use clone::Clone;
57-
use cmp::{Eq, Equiv, max};
58-
use default::Default;
59-
use fmt;
60-
use hash_old::Hash;
61-
use iter;
62-
use iter::{Iterator, FromIterator, Extendable};
63-
use iter::{FilterMap, Chain, Repeat, Zip};
64-
use mem::replace;
65-
use num;
66-
use option::{None, Option, Some};
67-
use rand::Rng;
68-
use rand;
69-
use result::{Ok, Err};
70-
use vec::{ImmutableVector, MutableVector, OwnedVector, Items, MutItems};
71-
use vec_ng;
72-
use vec_ng::Vec;
55+
use std::cmp::max;
56+
use std::fmt;
57+
use std::hash_old::Hash;
58+
use std::iter::{FilterMap, Chain, Repeat, Zip};
59+
use std::iter;
60+
use std::mem::replace;
61+
use std::num;
62+
use std::rand::Rng;
63+
use std::rand;
64+
use std::vec::{Items, MutItems};
65+
use std::vec_ng::Vec;
66+
use std::vec_ng;
67+
68+
use serialize::{Encodable, Decodable, Encoder, Decoder};
7369

7470
static INITIAL_CAPACITY: uint = 32u; // 2^5
7571

@@ -404,7 +400,7 @@ impl<K: Hash + Eq, V> HashMap<K, V> {
404400
/// # Example
405401
///
406402
/// ```rust
407-
/// use std::hashmap::HashMap;
403+
/// use collections::HashMap;
408404
///
409405
/// // map some strings to vectors of strings
410406
/// let mut map = HashMap::<~str, ~[~str]>::new();
@@ -613,6 +609,10 @@ impl<A: fmt::Show + Hash + Eq, B: fmt::Show> fmt::Show for HashMap<A, B> {
613609
}
614610
}
615611

612+
impl<K: fmt::Show + Hash + Eq, V: fmt::Show> ToStr for HashMap<K, V> {
613+
fn to_str(&self) -> ~str { format!("{}", *self) }
614+
}
615+
616616
/// HashMap iterator
617617
#[deriving(Clone)]
618618
pub struct Entries<'a, K, V> {
@@ -891,6 +891,10 @@ impl<A: fmt::Show + Hash + Eq> fmt::Show for HashSet<A> {
891891
}
892892
}
893893

894+
impl<A: fmt::Show + Hash + Eq> ToStr for HashSet<A> {
895+
fn to_str(&self) -> ~str { format!("{}", *self) }
896+
}
897+
894898
impl<K: Eq + Hash> FromIterator<K> for HashSet<K> {
895899
fn from_iterator<T: Iterator<K>>(iter: &mut T) -> HashSet<K> {
896900
let (lower, _) = iter.size_hint();
@@ -919,12 +923,75 @@ pub type SetAlgebraItems<'a, T> =
919923
FilterMap<'static,(&'a HashSet<T>, &'a T), &'a T,
920924
Zip<Repeat<&'a HashSet<T>>,SetItems<'a,T>>>;
921925

926+
impl<
927+
E: Encoder,
928+
K: Encodable<E> + Hash + IterBytes + Eq,
929+
V: Encodable<E>
930+
> Encodable<E> for HashMap<K, V> {
931+
fn encode(&self, e: &mut E) {
932+
e.emit_map(self.len(), |e| {
933+
let mut i = 0;
934+
for (key, val) in self.iter() {
935+
e.emit_map_elt_key(i, |e| key.encode(e));
936+
e.emit_map_elt_val(i, |e| val.encode(e));
937+
i += 1;
938+
}
939+
})
940+
}
941+
}
942+
943+
impl<
944+
D: Decoder,
945+
K: Decodable<D> + Hash + IterBytes + Eq,
946+
V: Decodable<D>
947+
> Decodable<D> for HashMap<K, V> {
948+
fn decode(d: &mut D) -> HashMap<K, V> {
949+
d.read_map(|d, len| {
950+
let mut map = HashMap::with_capacity(len);
951+
for i in range(0u, len) {
952+
let key = d.read_map_elt_key(i, |d| Decodable::decode(d));
953+
let val = d.read_map_elt_val(i, |d| Decodable::decode(d));
954+
map.insert(key, val);
955+
}
956+
map
957+
})
958+
}
959+
}
960+
961+
impl<
962+
S: Encoder,
963+
T: Encodable<S> + Hash + IterBytes + Eq
964+
> Encodable<S> for HashSet<T> {
965+
fn encode(&self, s: &mut S) {
966+
s.emit_seq(self.len(), |s| {
967+
let mut i = 0;
968+
for e in self.iter() {
969+
s.emit_seq_elt(i, |s| e.encode(s));
970+
i += 1;
971+
}
972+
})
973+
}
974+
}
975+
976+
impl<
977+
D: Decoder,
978+
T: Decodable<D> + Hash + IterBytes + Eq
979+
> Decodable<D> for HashSet<T> {
980+
fn decode(d: &mut D) -> HashSet<T> {
981+
d.read_seq(|d, len| {
982+
let mut set = HashSet::with_capacity(len);
983+
for i in range(0u, len) {
984+
set.insert(d.read_seq_elt(i, |d| Decodable::decode(d)));
985+
}
986+
set
987+
})
988+
}
989+
}
922990

923991
#[cfg(test)]
924992
mod test_map {
925-
use prelude::*;
926-
use super::*;
927-
use fmt;
993+
use super::{HashMap, HashSet};
994+
use std::fmt;
928995

929996
#[test]
930997
fn test_create_capacity_zero() {
@@ -1180,14 +1247,49 @@ mod test_map {
11801247
assert!(table_str == ~"{1: s2, 3: s4}" || table_str == ~"{3: s4, 1: s2}");
11811248
assert_eq!(format!("{}", empty), ~"{}");
11821249
}
1250+
1251+
struct StructWithToStrWithoutEqOrHash {
1252+
value: int
1253+
}
1254+
1255+
impl fmt::Show for StructWithToStrWithoutEqOrHash {
1256+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1257+
write!(f.buf, "s{}", self.value)
1258+
}
1259+
}
1260+
1261+
#[test]
1262+
fn test_hashset() {
1263+
let mut set: HashSet<int> = HashSet::new();
1264+
let empty_set: HashSet<int> = HashSet::new();
1265+
1266+
set.insert(1);
1267+
set.insert(2);
1268+
1269+
let set_str = set.to_str();
1270+
1271+
assert!(set_str == ~"{1, 2}" || set_str == ~"{2, 1}");
1272+
assert_eq!(empty_set.to_str(), ~"{}");
1273+
}
1274+
1275+
#[test]
1276+
fn test_hashmap() {
1277+
let mut table: HashMap<int, StructWithToStrWithoutEqOrHash> = HashMap::new();
1278+
let empty: HashMap<int, StructWithToStrWithoutEqOrHash> = HashMap::new();
1279+
1280+
table.insert(3, StructWithToStrWithoutEqOrHash { value: 4 });
1281+
table.insert(1, StructWithToStrWithoutEqOrHash { value: 2 });
1282+
1283+
let table_str = table.to_str();
1284+
1285+
assert!(table_str == ~"{1: s2, 3: s4}" || table_str == ~"{3: s4, 1: s2}");
1286+
assert_eq!(empty.to_str(), ~"{}");
1287+
}
11831288
}
11841289
11851290
#[cfg(test)]
11861291
mod test_set {
1187-
use super::*;
1188-
use prelude::*;
1189-
use container::Container;
1190-
use vec::ImmutableEqVector;
1292+
use super::HashSet;
11911293
11921294
#[test]
11931295
fn test_disjoint() {

src/libcollections/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,25 @@ pub use btree::BTree;
2727
pub use deque::Deque;
2828
pub use dlist::DList;
2929
pub use enum_set::EnumSet;
30+
pub use hashmap::{HashMap, HashSet};
3031
pub use list::List;
3132
pub use lru_cache::LruCache;
3233
pub use priority_queue::PriorityQueue;
3334
pub use ringbuf::RingBuf;
3435
pub use smallintmap::SmallIntMap;
3536
pub use treemap::{TreeMap, TreeSet};
37+
pub use trie::{TrieMap, TrieSet};
3638

3739
pub mod bitv;
3840
pub mod btree;
3941
pub mod deque;
4042
pub mod dlist;
4143
pub mod enum_set;
44+
pub mod hashmap;
4245
pub mod list;
4346
pub mod lru_cache;
4447
pub mod priority_queue;
4548
pub mod ringbuf;
4649
pub mod smallintmap;
4750
pub mod treemap;
51+
pub mod trie;

src/libcollections/lru_cache.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@
3838
//! ```
3939
4040
use std::container::Container;
41-
use std::hashmap::HashMap;
4241
use std::to_bytes::Cb;
4342
use std::ptr;
4443
use std::cast;
4544

45+
use HashMap;
46+
4647
struct KeyRef<K> { k: *K }
4748

4849
struct LruEntry<K, V> {

0 commit comments

Comments
 (0)