]> git.lizzy.rs Git - rust.git/blob - src/test/ui/issues/issue-17728.rs
Auto merge of #54624 - arielb1:evaluate-outlives, r=nikomatsakis
[rust.git] / src / test / ui / issues / issue-17728.rs
1 // Copyright 2014 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 use std::fmt::{Debug, Formatter, Error};
12 use std::collections::HashMap;
13
14 trait HasInventory {
15     fn getInventory<'s>(&'s self) -> &'s mut Inventory;
16     fn addToInventory(&self, item: &Item);
17     fn removeFromInventory(&self, itemName: &str) -> bool;
18 }
19
20 trait TraversesWorld {
21     fn attemptTraverse(&self, room: &Room, directionStr: &str) -> Result<&Room, &str> {
22         let direction = str_to_direction(directionStr);
23         let maybe_room = room.direction_to_room.get(&direction);
24         match maybe_room {
25             Some(entry) => Ok(entry),
26             //~^ ERROR 25:28: 25:37: lifetime mismatch [E0623]
27             _ => Err("Direction does not exist in room.")
28         }
29     }
30 }
31
32
33 #[derive(Debug, Eq, PartialEq, Hash)]
34 enum RoomDirection {
35     West,
36     East,
37     North,
38     South,
39     Up,
40     Down,
41     In,
42     Out,
43
44     None
45 }
46
47 struct Room {
48     description: String,
49     items: Vec<Item>,
50     direction_to_room: HashMap<RoomDirection, Room>,
51 }
52
53 impl Room {
54     fn new(description: &'static str) -> Room {
55         Room {
56             description: description.to_string(),
57             items: Vec::new(),
58             direction_to_room: HashMap::new()
59         }
60     }
61
62     fn add_direction(&mut self, direction: RoomDirection, room: Room) {
63         self.direction_to_room.insert(direction, room);
64     }
65 }
66
67 struct Item {
68     name: String,
69 }
70
71 struct Inventory {
72     items: Vec<Item>,
73 }
74
75 impl Inventory {
76     fn new() -> Inventory {
77         Inventory {
78             items: Vec::new()
79         }
80     }
81 }
82
83 struct Player {
84     name: String,
85     inventory: Inventory,
86 }
87
88 impl Player {
89     fn new(name: &'static str) -> Player {
90         Player {
91             name: name.to_string(),
92             inventory: Inventory::new()
93         }
94     }
95 }
96
97 impl TraversesWorld for Player {
98 }
99
100 impl Debug for Player {
101     fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error> {
102         formatter.write_str("Player{ name:");
103         formatter.write_str(&self.name);
104         formatter.write_str(" }");
105         Ok(())
106     }
107 }
108
109 fn str_to_direction(to_parse: &str) -> RoomDirection {
110     match to_parse { //~ ERROR match arms have incompatible types
111         "w" | "west" => RoomDirection::West,
112         "e" | "east" => RoomDirection::East,
113         "n" | "north" => RoomDirection::North,
114         "s" | "south" => RoomDirection::South,
115         "in" => RoomDirection::In,
116         "out" => RoomDirection::Out,
117         "up" => RoomDirection::Up,
118         "down" => RoomDirection::Down,
119         _ => None
120     }
121 }
122
123 fn main() {
124     let mut player = Player::new("Test player");
125     let mut room = Room::new("A test room");
126     println!("Made a player: {:?}", player);
127     println!("Direction parse: {:?}", str_to_direction("east"));
128     match player.attemptTraverse(&room, "west") {
129         Ok(_) => println!("Was able to move west"),
130         Err(msg) => println!("Not able to move west: {}", msg)
131     };
132 }