summaryrefslogtreecommitdiff
path: root/client/src/download.rs
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/download.rs')
-rw-r--r--client/src/download.rs17
1 files changed, 11 insertions, 6 deletions
diff --git a/client/src/download.rs b/client/src/download.rs
index 95f9cc7..e65ef25 100644
--- a/client/src/download.rs
+++ b/client/src/download.rs
@@ -17,9 +17,9 @@
use crate::network::Network;
use anyhow::Result;
use log::debug;
-use std::collections::HashSet;
+use std::{collections::HashSet, marker::PhantomData};
use weareshared::{
- packets::{Packet, Resource},
+ packets::{Packet, ReadWrite, Resource},
store::{ResourceStore, sha256},
};
@@ -39,9 +39,14 @@ impl Downloader {
store,
}
}
- pub fn try_get(&mut self, hash: Resource) -> Result<Option<Vec<u8>>> {
+ pub fn try_get<T: ReadWrite>(&mut self, hash: Resource<T>) -> Result<Option<T>> {
+ self.try_get_raw(Resource(hash.0, PhantomData))?
+ .map(|x| T::read(&mut x.as_slice()))
+ .transpose()
+ }
+ pub fn try_get_raw(&mut self, hash: Resource) -> Result<Option<Vec<u8>>> {
if self.have.contains(&hash) {
- self.store.get(hash)
+ self.store.get_raw(hash)
} else {
self.need.insert(hash);
Ok(None)
@@ -50,8 +55,8 @@ impl Downloader {
pub fn packet(&mut self, p: &Packet) -> Result<()> {
match p {
Packet::RespondResource(d) => {
- let key = Resource(sha256(&d.0));
- self.store.set(&d.0)?;
+ let key = Resource(sha256(&d.0), PhantomData);
+ self.store.set_raw(&d.0)?;
self.need.remove(&key);
self.pending.remove(&key);
if self.have.insert(key) {