Created
January 14, 2014 14:48
-
-
Save mneumann/8419440 to your computer and use it in GitHub Desktop.
decoder.rs:176:5: 176:22 error: internal compiler error: Cannot relate bound region: ReInfer(7) <= ReLateBound(949, BrNamed(syntax::ast::DefId{crate: 0u32, node: 963u32}, a))
This message reflects a bug in the Rust compiler. We would appreciate a bug report: https://github.com/mozilla/rust/wiki/HOWTO-submit-a-Rust-bug-report
decoder.rs:176 Decod…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern mod extra; | |
use std::hashmap::HashMap; | |
use extra::serialize; | |
use extra::serialize::Decodable; | |
pub enum Value { | |
NoValue, | |
Boolean(bool), | |
Unsigned(u64), | |
Signed(u64), | |
Float(f64), | |
String(~str), | |
Datetime(u16,u8,u8,u8,u8,u8), | |
Array(~[Value]), | |
TableArray(~[Value]), | |
Table(bool, ~HashMap<~str, Value>) // bool=true iff section already defiend | |
} | |
pub struct Decoder<'a> { | |
priv value: &'a Value | |
} | |
impl<'a> serialize::Decoder for Decoder<'a> { | |
fn read_nil(&mut self) -> () { fail!() } | |
fn read_u64(&mut self) -> u64 { | |
match *self.value { | |
Unsigned(v) => v, | |
_ => fail!() | |
} | |
} | |
fn read_uint(&mut self) -> uint { self.read_u64().to_uint().unwrap() } | |
fn read_u32(&mut self) -> u32 { self.read_u64().to_u32().unwrap() } | |
fn read_u16(&mut self) -> u16 { self.read_u64().to_u16().unwrap() } | |
fn read_u8(&mut self) -> u8 { self.read_u64().to_u8().unwrap() } | |
fn read_i64(&mut self) -> i64 { | |
match *self.value { | |
Unsigned(v) => v.to_i64().unwrap(), | |
Signed(v) => v.to_i64().unwrap(), | |
_ => fail!() | |
} | |
} | |
fn read_int(&mut self) -> int { self.read_i64().to_int().unwrap() } | |
fn read_i32(&mut self) -> i32 { self.read_i64().to_i32().unwrap() } | |
fn read_i16(&mut self) -> i16 { self.read_i64().to_i16().unwrap() } | |
fn read_i8(&mut self) -> i8 { self.read_i64().to_i8().unwrap() } | |
fn read_bool(&mut self) -> bool { | |
match *self.value { | |
Boolean(b) => b, | |
_ => fail!() | |
} | |
} | |
fn read_f64(&mut self) -> f64 { | |
match *self.value { | |
Float(f) => f, | |
_ => fail!() | |
} | |
} | |
fn read_f32(&mut self) -> f32 { | |
self.read_f64().to_f32().unwrap() | |
} | |
fn read_char(&mut self) -> char { | |
let s = self.read_str(); | |
if s.len() != 1 { fail!("no character") } | |
s[0] as char | |
} | |
fn read_str(&mut self) -> ~str { | |
match *self.value { | |
String(ref s) => s.to_owned(), | |
_ => fail!() | |
} | |
} | |
fn read_enum<T>(&mut self, _name: &str, _f: |&mut Decoder<'a>| -> T) -> T { fail!() } | |
fn read_enum_variant<T>(&mut self, _names: &[&str], _f: |&mut Decoder<'a>, uint| -> T) -> T { fail!() } | |
fn read_enum_variant_arg<T>(&mut self, _idx: uint, _f: |&mut Decoder<'a>| -> T) -> T { fail!() } | |
fn read_seq<T>(&mut self, f: |&mut Decoder<'a>, uint| -> T) -> T { | |
match *self.value { | |
Array(ref a) => f(self, a.len()), | |
TableArray(ref a) => f(self, a.len()), | |
_ => fail!() | |
} | |
} | |
fn read_seq_elt<T>(&mut self, idx: uint, f: |&mut Decoder<'a>| -> T) -> T { | |
match *self.value { | |
Array(ref a) => f(&mut Decoder{value: &a[idx]}), | |
TableArray(ref a) => f(&mut Decoder{value: &a[idx]}), | |
_ => fail!() | |
} | |
} | |
fn read_struct<T>(&mut self, _name: &str, _len: uint, f: |&mut Decoder<'a>| -> T) -> T { | |
match *self.value { | |
Table(_, _) => f(self), | |
_ => fail!() | |
} | |
} | |
fn read_struct_field<T>(&mut self, name: &str, _idx: uint, f: |&mut Decoder<'a>| -> T) -> T { | |
match *self.value { | |
Table(_, ref map) => { | |
match map.find_equiv(&name) { | |
Some(val) => f(&mut Decoder{value: val}), | |
None => f(&mut Decoder{value: &NoValue}), // XXX: NoValue equals "nil" here, not invalid value | |
} | |
} | |
_ => fail!() | |
} | |
} | |
fn read_option<T>(&mut self, f: |&mut Decoder<'a>, bool| -> T) -> T { | |
match *self.value { | |
NoValue => f(self, false), | |
_ => f(self, true) | |
} | |
} | |
fn read_map<T>(&mut self, f: |&mut Decoder<'a>, uint| -> T) -> T { | |
// f(self, 0) | |
fail!("Maps not yet implemented") | |
} | |
fn read_map_elt_key<T>(&mut self, _idx: uint, f: |&mut Decoder<'a>| -> T) -> T { f(self) } | |
fn read_map_elt_val<T>(&mut self, _idx: uint, f: |&mut Decoder<'a>| -> T) -> T { f(self) } | |
fn read_enum_struct_variant<T>(&mut self, | |
names: &[&str], | |
f: |&mut Decoder<'a>, uint| -> T) | |
-> T { | |
self.read_enum_variant(names, f) | |
} | |
fn read_enum_struct_variant_field<T>(&mut self, | |
_name: &str, | |
idx: uint, | |
f: |&mut Decoder<'a>| -> T) | |
-> T { | |
self.read_enum_variant_arg(idx, f) | |
} | |
fn read_tuple<T>(&mut self, f: |&mut Decoder<'a>, uint| -> T) -> T { | |
self.read_seq(f) | |
} | |
fn read_tuple_arg<T>(&mut self, idx: uint, f: |&mut Decoder<'a>| -> T) -> T { | |
self.read_seq_elt(idx, f) | |
} | |
fn read_tuple_struct<T>(&mut self, | |
_name: &str, | |
f: |&mut Decoder<'a>, uint| -> T) | |
-> T { | |
self.read_tuple(f) | |
} | |
fn read_tuple_struct_arg<T>(&mut self, | |
idx: uint, | |
f: |&mut Decoder<'a>| -> T) | |
-> T { | |
self.read_tuple_arg(idx, f) | |
} | |
} | |
fn from_toml<'a, T: Decodable<Decoder<'a>>>(value: &'a Value) -> T { | |
let mut decoder: Decoder<'a> = Decoder {value: value}; | |
Decodable::decode(&mut decoder) | |
} | |
fn main() { | |
let v = Unsigned(123); | |
//THIS WORKS: let mut decoder = Decoder {value: &v}; | |
//THIS WORKS: let num: uint = Decodable::decode(&mut decoder); | |
let num: uint = from_toml(&v); // THIS DOES NOT WORK | |
println!("num: {}", num); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment