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