Skip to content

Commit

Permalink
Merge further into nonassociated function
Browse files Browse the repository at this point in the history
  • Loading branch information
voidentente committed Nov 15, 2023
1 parent 4959bfe commit 63c0e58
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions src/web_asset_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ impl WebAssetReader {
uri.set_extension(extension);
uri
}
}

#[cfg(target_arch = "wasm")]
async fn get(&self, path: PathBuf) -> Result<Box<Reader>, AssetReaderError> {
let uri_str = uri.to_str().unwrap();
async fn get<'a>(uri: PathBuf) -> Result<Box<Reader<'a>>, AssetReaderError> {
let uri_str = uri.to_str().unwrap();

#[cfg(target_arch = "wasm")]
let bytes = {
use wasm_bindgen::JsCast;
use wasm_bindgen_futures::JsFuture;
let window = web_sys::window().unwrap();
Expand All @@ -54,41 +56,33 @@ impl WebAssetReader {
.await
.unwrap();

let bytes = js_sys::Uint8Array::new(&data).to_vec();

let reader: Box<Reader> = Box::new(VecReader::new(bytes));

Ok(reader)
}
js_sys::Uint8Array::new(&data).to_vec()
};

#[cfg(not(target_arch = "wasm"))]
async fn get(&self, uri: PathBuf) -> Result<Box<Reader>, AssetReaderError> {
let uri_str = uri.to_str().unwrap();

let bytes = surf::get(uri_str)
.recv_bytes()
.await
.map_err(|_| AssetReaderError::NotFound(uri))?;
let bytes = surf::get(uri_str)
.recv_bytes()
.await
.map_err(|_| AssetReaderError::NotFound(uri))?;

let reader: Box<Reader> = Box::new(VecReader::new(bytes));
let reader: Box<Reader> = Box::new(VecReader::new(bytes));

Ok(reader)
}
Ok(reader)
}

impl AssetReader for WebAssetReader {
fn read<'a>(
&'a self,
path: &'a Path,
) -> BoxedFuture<'a, Result<Box<Reader<'a>>, AssetReaderError>> {
Box::pin(self.get(self.make_uri(path)))
Box::pin(get(self.make_uri(path)))
}

fn read_meta<'a>(
&'a self,
path: &'a Path,
) -> BoxedFuture<'a, Result<Box<Reader<'a>>, AssetReaderError>> {
Box::pin(self.get(self.make_meta_uri(path)))
Box::pin(get(self.make_meta_uri(path)))
}

fn is_directory<'a>(
Expand Down

0 comments on commit 63c0e58

Please sign in to comment.