Skip to content

Instantly share code, notes, and snippets.

@zargony
Last active August 29, 2015 14:00
Show Gist options
  • Save zargony/11232547 to your computer and use it in GitHub Desktop.
Save zargony/11232547 to your computer and use it in GitHub Desktop.
fn decode<'a, T: Decodable<MyDecoder<BufReader<'a>>, IoError>> (&self, key: u64) -> Result<Option<T>, ~str> {
match self.get(key) {
Ok(Some(data)) => {
let reader = BufReader::new(data);
// ^~~~ error: `data` does not live long enough
// because 'a is bound to the function context because of the returned T
// but 'a actually only needs to be temporary (the reader var in this context)
let mut decoder = MyDecoder::new(reader);
match Decodable::decode(&mut decoder) {
Ok(value) => Ok(Some(value)),
Err(err) => Err(format!("deserialize: {}", err)),
}
},
Ok(None) => Ok(None),
Err(err) => Err(err),
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment