use std::collections::HashMap;
use std::io::{Result, Read, Write, Error, ErrorKind};
use backend::Backend;
use hash::ContentHasher;
impl<H> Backend<H> for HashMap<H::Digest, Vec<u8>>
where H: ContentHasher,
{
fn store(
&mut self,
source: &Fn(&mut Write, &mut Backend<H>) -> Result<H::Digest>,
) -> Result<H::Digest> {
let mut vec = vec![];
let hash = try!(source(&mut vec, self));
self.insert(hash.clone(), vec);
Ok(hash)
}
fn request(
&self,
hash: &H::Digest,
read: &Fn(&mut Read) -> Result<()>,
) -> Result<()> {
let vec = try!(self.get(hash)
.ok_or(Error::new(ErrorKind::NotFound, "")));
read(&mut &vec[..])
}
}