Borrow a vec to fix an error
Getty Ritter
7 years ago
9 | 9 | fn parse_pixel<Rd>(r: &mut ByteReader<Rd>) -> Result<(u16,u16,u16,u16), String> |
10 | 10 | where Rd: Iterator<Item=u8> |
11 | 11 | { |
12 | Ok((try!(r.read_u16be()), | |
13 | try!(r.read_u16be()), | |
14 | try!(r.read_u16be()), | |
15 | try!(r.read_u16be()))) | |
12 | Ok((r.read_u16be()?, | |
13 | r.read_u16be()?, | |
14 | r.read_u16be()?, | |
15 | r.read_u16be()?)) | |
16 | 16 | } |
17 | 17 | |
18 | 18 | impl FFImage { |
36 | 36 | pub fn from_reader<Rd>(r: &mut ByteReader<Rd>) -> Result<FFImage, String> |
37 | 37 | where Rd: Iterator<Item=u8> |
38 | 38 | { |
39 | assert!(try!(r.next()) == 'f' as u8); | |
40 | assert!(try!(r.next()) == 'a' as u8); | |
41 | assert!(try!(r.next()) == 'r' as u8); | |
42 | assert!(try!(r.next()) == 'b' as u8); | |
43 | assert!(try!(r.next()) == 'f' as u8); | |
44 | assert!(try!(r.next()) == 'e' as u8); | |
45 | assert!(try!(r.next()) == 'l' as u8); | |
46 | assert!(try!(r.next()) == 'd' as u8); | |
39 | assert!(r.next()? == 'f' as u8); | |
40 | assert!(r.next()? == 'a' as u8); | |
41 | assert!(r.next()? == 'r' as u8); | |
42 | assert!(r.next()? == 'b' as u8); | |
43 | assert!(r.next()? == 'f' as u8); | |
44 | assert!(r.next()? == 'e' as u8); | |
45 | assert!(r.next()? == 'l' as u8); | |
46 | assert!(r.next()? == 'd' as u8); | |
47 | 47 | |
48 | 48 | let w = try!(r.read_u32be()); |
49 | 49 | let h = try!(r.read_u32be()); |
71 | 71 | let img = image::load(Cursor::new(tex.clone()), |
72 | 72 | image::PNG).unwrap().to_rgba(); |
73 | 73 | let dims = img.dimensions(); |
74 |
let img = RawImage2d::from_raw_rgba_reversed( |
|
74 | let img = RawImage2d::from_raw_rgba_reversed(&img.into_raw(), | |
75 | 75 | dims); |
76 | 76 | Some(Texture2d::new(display, img).unwrap()) |
77 | 77 | } else { |
33 | 33 | '"' => parse_string(s), |
34 | 34 | _ if c.is_digit(10) || c == '-' => parse_num(c, s), |
35 | 35 | _ if c.is_alphabetic() => |
36 | Ok(Adnot::Sym(try!(parse_sym(c, s)))), | |
37 | _ => fail(&format!("Invalid character: {:?}", c)), | |
36 | Ok(Adnot::Sym(parse_sym(c, s)?)), | |
37 | _ => Err(AdnotError { message: format!("Invalid character: {:?}", c) }), | |
38 | 38 | } |
39 | 39 | } else { |
40 | 40 | fail("Unexpected end of input") |
49 | 49 | s.next(); |
50 | 50 | return Ok(Adnot::List(vec)); |
51 | 51 | } else { |
52 |
vec.push( |
|
52 | vec.push(parse_val(s)?); | |
53 | 53 | } |
54 | 54 | } |
55 | 55 | } |
62 | 62 | Some(_) => { return fail("Expected a tagname character") } |
63 | 63 | None => { return fail("Unexpected end of input") } |
64 | 64 | }; |
65 |
let name = |
|
65 | let name = parse_sym(first, s)?; | |
66 | 66 | loop { |
67 | 67 | skip_space(s); |
68 | 68 | if let Some(&')') = s.peek() { |
69 | 69 | s.next(); |
70 | 70 | return Ok(Adnot::Sum(name, vec)); |
71 | 71 | } else { |
72 |
vec.push( |
|
72 | vec.push(parse_val(s)?); | |
73 | 73 | } |
74 | 74 | } |
75 | 75 | } |
88 | 88 | Some(_) => { return fail("Expected a tagname character") } |
89 | 89 | None => { return fail("Unexpected end of input") } |
90 | 90 | }; |
91 |
let key = |
|
91 | let key = parse_sym(first, s)?; | |
92 | 92 | skip_space(s); |
93 |
let val = |
|
93 | let val = parse_val(s)?; | |
94 | 94 | map.insert(key, val); |
95 | 95 | } |
96 | 96 | } |
178 | 178 | Ok(ret) |
179 | 179 | } |
180 | 180 | |
181 | /// This reads a PrefixInteger to find out how many other things to read, | |
182 | /// and then reads that number of things. | |
181 | /// This reads a PrefixInteger to find out how many other things | |
182 | /// to read, and then reads that number of things. | |
183 | 183 | pub fn read_several<F, R>(&mut self, reader: F) -> Result<Vec<R>, String> |
184 | 184 | where F: Fn(&mut ByteReader<T>) -> Result<R, String> |
185 | 185 | { |
191 | 191 | Ok(ret) |
192 | 192 | } |
193 | 193 | |
194 | /// This reads a PrefixInteger number of bytes, and then parses those | |
195 | /// bytes as a UTF-8 string. This means, importantly, that we cannot | |
196 | /// naïvely produce values intended to be parsed with this using a | |
197 | /// basic string length. | |
194 | /// This reads a PrefixInteger number of bytes, and then parses | |
195 | /// those bytes as a UTF-8 string. This means, importantly, that | |
196 | /// we cannot naïvely produce values intended to be parsed with | |
197 | /// this using a basic string length. | |
198 | 198 | pub fn read_string(&mut self) -> Result<String, String> { |
199 | 199 | let raw_bytes = try!(self.read_several(|r| r.next())); |
200 | 200 | match String::from_utf8(raw_bytes) { |