Skip to content

Commit 5b821ad

Browse files
committed
---
yaml --- r: 167277 b: refs/heads/try c: 2d10021 h: refs/heads/master i: 167275: 4a017b5 v: v3
1 parent 6178ffd commit 5b821ad

File tree

2 files changed

+133
-1
lines changed

2 files changed

+133
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: 023dfb0c898d851dee6ace2f8339b73b5287136b
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 023dfb0c898d851dee6ace2f8339b73b5287136b
5-
refs/heads/try: 01cdf00c2fbc388f777366a5ee303019c462b99a
5+
refs/heads/try: 2d100212a916107f336e92c8c0ed79a5bedc4f6a
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
88
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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::{Show, 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.find(&direction);
24+
//~^ ERROR cannot infer an appropriate lifetime for autoref due to conflicting requirements
25+
match maybe_room {
26+
Some(entry) => Ok(entry),
27+
_ => Err("Direction does not exist in room.")
28+
}
29+
}
30+
}
31+
32+
33+
#[deriving(Show, 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 Show for Player {
101+
fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error> {
102+
formatter.write_str("Player{ name:");
103+
formatter.write_str(self.name.as_slice());
104+
formatter.write_str(" }");
105+
Ok(())
106+
}
107+
}
108+
109+
fn str_to_direction(to_parse: &str) -> RoomDirection {
110+
match to_parse {
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 //~ ERROR mismatched types
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+
}

0 commit comments

Comments
 (0)