Skip to content

Instantly share code, notes, and snippets.

@Freezerburn
Created October 3, 2014 00:14
Show Gist options
  • Save Freezerburn/0284be9402d638cdf7b9 to your computer and use it in GitHub Desktop.
Save Freezerburn/0284be9402d638cdf7b9 to your computer and use it in GitHub Desktop.
use std::fmt::{Show, Formatter, FormatError};
use std::collections::HashMap;
use std::collections::hashmap::{Entry, Vacant, Occupied};
trait HasInventory {
fn getInventory<'s>(&'s self) -> &'s mut Inventory;
fn addToInventory(&self, item: &Item);
fn removeFromInventory(&self, itemName: &str) -> bool;
}
trait TraversesWorld {
fn attemptTraverse(&self, room: &Room, directionStr: &str) -> Result<&Room, &str> {
let direction = str_to_direction(directionStr);
let maybe_room = room.direction_to_room.find(&direction);
match maybe_room {
Some(entry) => Ok(entry),
_ => Err("Direction does not exist in room.")
}
}
}
#[deriving(Show, Eq, PartialEq, Hash)]
enum RoomDirection {
West,
East,
North,
South,
Up,
Down,
In,
Out,
None
}
struct Room {
description: String,
items: Vec<Item>,
direction_to_room: HashMap<RoomDirection, Room>,
}
impl Room {
fn new(description: &'static str) -> Room {
Room {
description: description.to_string(),
items: Vec::new(),
direction_to_room: HashMap::new()
}
}
fn add_direction(&mut self, direction: RoomDirection, room: Room) {
self.direction_to_room.insert(direction, room);
}
}
struct Item {
name: String,
}
struct Inventory {
items: Vec<Item>,
}
impl Inventory {
fn new() -> Inventory {
Inventory {
items: Vec::new()
}
}
}
struct Player {
name: String,
inventory: Inventory,
}
impl Player {
fn new(name: &'static str) -> Player {
Player {
name: name.to_string(),
inventory: Inventory::new()
}
}
}
impl TraversesWorld for Player {
}
impl Show for Player {
fn fmt(&self, formatter: &mut Formatter) -> Result<(), FormatError> {
formatter.write_str("Player{ name:");
formatter.write_str(self.name.as_slice());
formatter.write_str(" }");
Ok(())
}
}
fn str_to_direction(to_parse: &str) -> RoomDirection {
match to_parse {
"w" | "west" => West,
"e" | "east" => East,
"n" | "north" => North,
"s" | "south" => South,
"in" => In,
"out" => Out,
"up" => Up,
"down" => Down,
_ => None
}
}
fn main() {
let mut player = Player::new("Test player");
let mut room = Room::new("A test room");
println!("Made a player: {}", player);
println!("Direction parse: {}", str_to_direction("east"));
match player.attemptTraverse(&room, "west") {
Ok(_) => println!("Was able to move west"),
Err(msg) => println!("Not able to move west: {}", msg)
};
}
// Causes the following problem:
➜ text_adventure git:(master) ✗ RUST_BACKTRACE=1 cargo build --verbose
Compiling text_adventure v0.0.1 (file:///Users/kuyatt/Documents/workspace-rust/text_adventure)
Running `rustc /Users/kuyatt/Documents/workspace-rust/text_adventure/src/main.rs --crate-name text_adventure --crate-type bin -g --out-dir /Users/kuyatt/Documents/workspace-rust/text_adventure/target --dep-info /Users/kuyatt/Documents/workspace-rust/text_adventure/target/.fingerprint/text_adventure-8f381997c0721fe1/dep-bin-text_adventure -L /Users/kuyatt/Documents/workspace-rust/text_adventure/target -L /Users/kuyatt/Documents/workspace-rust/text_adventure/target/deps`
/Users/kuyatt/Documents/workspace-rust/text_adventure/src/main.rs:46:26: 46:65 error: cannot infer an appropriate lifetime for autoref due to conflicting requirements
/Users/kuyatt/Documents/workspace-rust/text_adventure/src/main.rs:46 let maybe_room = room.direction_to_room.find(&direction);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: internal compiler error: unexpected failure
note: the compiler hit an unexpected failure path. this is a bug.
note: we would appreciate a bug report: http://doc.rust-lang.org/complement-bugreport.html
note: run with `RUST_BACKTRACE=1` for a backtrace
task 'rustc' failed at 'expect item fn', /Users/rustbuild/src/rust-buildbot/slave/nightly-mac/build/src/libcore/option.rs:315
stack backtrace:
1: 0x10f461a59 - rt::backtrace::imp::write::h049a27795fff38dbKPq
2: 0x10f464d91 - failure::on_fail::hf299c5c1cb208a9do6q
3: 0x10f6e60f5 - unwind::begin_unwind_inner::h465f1e1474a7adb5VRd
4: 0x10f6e5dc3 - unwind::begin_unwind_fmt::h11c657e8bf2fc1ecnPd
5: 0x10f6e5b32 - rust_begin_unwind
6: 0x10f739f1c - failure::fail_fmt::h14b9753f2e0afd83oqk
7: 0x10c6f9997 - middle::typeck::infer::error_reporting::InferCtxt<'a, 'tcx>.ErrorReporting::give_suggestion::hee8233fa75fbb959wp5
8: 0x10c6ec09d - middle::typeck::infer::error_reporting::InferCtxt<'a, 'tcx>.ErrorReporting::report_processed_errors::h0bc7f272ea7a0a9cAo5
9: 0x10c6e48ca - middle::typeck::infer::error_reporting::InferCtxt<'a, 'tcx>.ErrorReporting::report_region_errors::hacd12f7fc52064ed3A4
10: 0x10c5fe895 - middle::typeck::infer::InferCtxt<'a, 'tcx>::resolve_regions_and_report_errors::h3d98ef626bf1dad3wWd
11: 0x10c5feb2e - middle::typeck::check::regionck::regionck_fn::he42ac947f4e2271btNM
12: 0x10c642d12 - middle::typeck::check::check_bare_fn::h0fc2fbf97b27b821JGS
13: 0x10c64b161 - middle::typeck::check::check_method_body::h6e18cb2204dbad6btjT
14: 0x10c63ee1b - middle::typeck::check::check_item::hd7aefd1e02f344e1M8S
15: 0x10c642b5f - middle::typeck::check::check_item_types::he9ba5b375f4a0798TFS
16: 0x10c0fc4d6 - util::common::time::h11615330315913117644
17: 0x10c8b690e - middle::typeck::check_crate::h148ef8760d1aaff5GAk
18: 0x10c9206ef - driver::driver::phase_3_run_analysis_passes::h695299e920b1a0eaS8w
19: 0x10c91b5c8 - driver::driver::compile_input::h7ca388b7670c6637EPw
20: 0x10c99a354 - driver::run_compiler::he47acda96320b6ebLFA
21: 0x10c9984f6 - driver::main_args::closure.146669
22: 0x10c114c7b - task::TaskBuilder<S>::try_future::closure.101507
23: 0x10c114b73 - task::TaskBuilder<S>::spawn_internal::closure.101478
24: 0x10c09167d - task::spawn_opts::closure.8560
25: 0x10f7492ac - rust_try_inner
26: 0x10f749296 - rust_try
27: 0x10f6e30a7 - unwind::try::h458d14453b79d95aDGd
28: 0x10f6e2f3c - task::Task::run::h302df3fa031b10f3uWc
29: 0x10c0914d2 - task::spawn_opts::closure.8499
30: 0x10f6e504a - thread::thread_start::hed24eb90b871d5ebZgd
31: 0x7fff86438899 - _pthread_body
32: 0x7fff8643872a - _pthread_struct_init
Could not compile `text_adventure`.
Caused by:
Process didn't exit successfully: `rustc /Users/kuyatt/Documents/workspace-rust/text_adventure/src/main.rs --crate-name text_adventure --crate-type bin -g --out-dir /Users/kuyatt/Documents/workspace-rust/text_adventure/target --dep-info /Users/kuyatt/Documents/workspace-rust/text_adventure/target/.fingerprint/text_adventure-8f381997c0721fe1/dep-bin-text_adventure -L /Users/kuyatt/Documents/workspace-rust/text_adventure/target -L /Users/kuyatt/Documents/workspace-rust/text_adventure/target/deps` (status=101)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment