]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
1 | // Copyright 2013 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 | ||
54a0048b SL |
11 | use std::collections::BTreeMap; |
12 | use std::borrow::Cow; | |
c34b1796 | 13 | |
54a0048b | 14 | use std::borrow::Cow::{Owned as O, Borrowed as B}; |
1a4d82fc | 15 | |
85aaf69f | 16 | type SendStr = Cow<'static, str>; |
1a4d82fc | 17 | |
54a0048b | 18 | fn main() { |
c34b1796 | 19 | let mut map: BTreeMap<SendStr, usize> = BTreeMap::new(); |
54a0048b SL |
20 | assert!(map.insert(B("foo"), 42).is_none()); |
21 | assert!(map.insert(O("foo".to_string()), 42).is_some()); | |
22 | assert!(map.insert(B("foo"), 42).is_some()); | |
23 | assert!(map.insert(O("foo".to_string()), 42).is_some()); | |
1a4d82fc | 24 | |
54a0048b SL |
25 | assert!(map.insert(B("foo"), 43).is_some()); |
26 | assert!(map.insert(O("foo".to_string()), 44).is_some()); | |
27 | assert!(map.insert(B("foo"), 45).is_some()); | |
28 | assert!(map.insert(O("foo".to_string()), 46).is_some()); | |
1a4d82fc JJ |
29 | |
30 | let v = 46; | |
31 | ||
54a0048b SL |
32 | assert_eq!(map.get(&O("foo".to_string())), Some(&v)); |
33 | assert_eq!(map.get(&B("foo")), Some(&v)); | |
1a4d82fc JJ |
34 | |
35 | let (a, b, c, d) = (50, 51, 52, 53); | |
36 | ||
54a0048b SL |
37 | assert!(map.insert(B("abc"), a).is_none()); |
38 | assert!(map.insert(O("bcd".to_string()), b).is_none()); | |
39 | assert!(map.insert(B("cde"), c).is_none()); | |
40 | assert!(map.insert(O("def".to_string()), d).is_none()); | |
1a4d82fc | 41 | |
54a0048b SL |
42 | assert!(map.insert(B("abc"), a).is_some()); |
43 | assert!(map.insert(O("bcd".to_string()), b).is_some()); | |
44 | assert!(map.insert(B("cde"), c).is_some()); | |
45 | assert!(map.insert(O("def".to_string()), d).is_some()); | |
1a4d82fc | 46 | |
54a0048b SL |
47 | assert!(map.insert(O("abc".to_string()), a).is_some()); |
48 | assert!(map.insert(B("bcd"), b).is_some()); | |
49 | assert!(map.insert(O("cde".to_string()), c).is_some()); | |
50 | assert!(map.insert(B("def"), d).is_some()); | |
1a4d82fc | 51 | |
54a0048b SL |
52 | assert_eq!(map.get(&B("abc")), Some(&a)); |
53 | assert_eq!(map.get(&B("bcd")), Some(&b)); | |
54 | assert_eq!(map.get(&B("cde")), Some(&c)); | |
55 | assert_eq!(map.get(&B("def")), Some(&d)); | |
1a4d82fc | 56 | |
54a0048b SL |
57 | assert_eq!(map.get(&O("abc".to_string())), Some(&a)); |
58 | assert_eq!(map.get(&O("bcd".to_string())), Some(&b)); | |
59 | assert_eq!(map.get(&O("cde".to_string())), Some(&c)); | |
60 | assert_eq!(map.get(&O("def".to_string())), Some(&d)); | |
1a4d82fc | 61 | |
54a0048b | 62 | assert!(map.remove(&B("foo")).is_some()); |
1a4d82fc JJ |
63 | assert_eq!(map.into_iter().map(|(k, v)| format!("{}{}", k, v)) |
64 | .collect::<Vec<String>>() | |
65 | .concat(), | |
66 | "abc50bcd51cde52def53".to_string()); | |
67 | } |