Skip to content

Commit b0fa1a4

Browse files
dmarteaukylebarron
andauthored
feat: Relax 'Send' on [async-trait] (#311)
* feat: Relax 'Send' on async-trait * feat: Check wasm compilation in CI workflow * don't use wasm-pack * add wasm32 target --------- Co-authored-by: Kyle Barron <kyle@developmentseed.org>
1 parent fed0c5f commit b0fa1a4

5 files changed

Lines changed: 54 additions & 11 deletions

File tree

‎.github/workflows/wasm-build.yml‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Wasm
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
9+
permissions: {}
10+
11+
concurrency:
12+
group: ${{ github.workflow }}-${{ github.ref }}
13+
cancel-in-progress: true
14+
15+
jobs:
16+
wasm_build:
17+
name: Wasm build
18+
runs-on: ubuntu-latest
19+
steps:
20+
- uses: actions/checkout@v6
21+
with:
22+
persist-credentials: false
23+
24+
- name: Install Rust
25+
uses: dtolnay/rust-toolchain@stable
26+
with:
27+
targets: wasm32-unknown-unknown
28+
29+
- uses: Swatinem/rust-cache@v2
30+
31+
- name: Wasm target compilation
32+
run: cargo check --target wasm32-unknown-unknown --lib

‎src/metadata/cache.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ impl<F: MetadataFetch> ReadaheadMetadataCache<F> {
143143
}
144144
}
145145

146-
#[async_trait]
146+
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
147+
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
147148
impl<F: MetadataFetch + Send + Sync> MetadataFetch for ReadaheadMetadataCache<F> {
148149
async fn fetch(&self, range: Range<u64>) -> AsyncTiffResult<Bytes> {
149150
let mut cache = self.cache.lock().await;
@@ -190,7 +191,8 @@ mod test {
190191
}
191192
}
192193

193-
#[async_trait]
194+
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
195+
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
194196
impl MetadataFetch for TestFetch {
195197
async fn fetch(&self, range: Range<u64>) -> crate::error::AsyncTiffResult<Bytes> {
196198
if range.start as usize >= self.data.len() {

‎src/metadata/fetch.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ use crate::reader::{AsyncFileReader, EndianAwareReader, Endianness};
1212
/// [`ImageFileDirectory`][crate::ImageFileDirectory]s.
1313
///
1414
/// Note that implementation is provided for [`AsyncFileReader`].
15-
#[async_trait]
15+
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
16+
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
1617
pub trait MetadataFetch: Debug + Send + Sync + 'static {
1718
/// Return a future that fetches the specified range of bytes asynchronously
1819
///
@@ -21,7 +22,8 @@ pub trait MetadataFetch: Debug + Send + Sync + 'static {
2122
async fn fetch(&self, range: Range<u64>) -> AsyncTiffResult<Bytes>;
2223
}
2324

24-
#[async_trait]
25+
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
26+
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
2527
impl<T: AsyncFileReader> MetadataFetch for T {
2628
async fn fetch(&self, range: Range<u64>) -> AsyncTiffResult<Bytes> {
2729
self.get_bytes(range).await

‎src/metadata/reader.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,8 @@ mod test {
631631

632632
use super::*;
633633

634-
#[async_trait]
634+
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
635+
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
635636
impl MetadataFetch for Bytes {
636637
async fn fetch(&self, range: std::ops::Range<u64>) -> crate::error::AsyncTiffResult<Bytes> {
637638
let usize_range = range.start as usize..range.end as usize;

‎src/reader.rs‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ use crate::error::AsyncTiffResult;
2929
/// [`ObjectStore`]: object_store::ObjectStore
3030
///
3131
/// [`tokio::fs::File`]: https://docs.rs/tokio/latest/tokio/fs/struct.File.html
32-
#[async_trait]
32+
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
33+
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
3334
pub trait AsyncFileReader: Debug + Send + Sync + 'static {
3435
/// Retrieve the bytes in `range` as part of a request for image data, not header metadata.
3536
///
@@ -52,7 +53,8 @@ pub trait AsyncFileReader: Debug + Send + Sync + 'static {
5253
}
5354

5455
/// This allows Box<dyn AsyncFileReader + '_> to be used as an AsyncFileReader,
55-
#[async_trait]
56+
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
57+
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
5658
impl AsyncFileReader for Box<dyn AsyncFileReader + '_> {
5759
async fn get_bytes(&self, range: Range<u64>) -> AsyncTiffResult<Bytes> {
5860
self.as_ref().get_bytes(range).await
@@ -64,7 +66,8 @@ impl AsyncFileReader for Box<dyn AsyncFileReader + '_> {
6466
}
6567

6668
/// This allows Arc<dyn AsyncFileReader + '_> to be used as an AsyncFileReader,
67-
#[async_trait]
69+
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
70+
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
6871
impl AsyncFileReader for Arc<dyn AsyncFileReader + '_> {
6972
async fn get_bytes(&self, range: Range<u64>) -> AsyncTiffResult<Bytes> {
7073
self.as_ref().get_bytes(range).await
@@ -121,7 +124,8 @@ impl<T: tokio::io::AsyncRead + tokio::io::AsyncSeek + Unpin + Send + Debug> Toki
121124
}
122125

123126
#[cfg(feature = "tokio")]
124-
#[async_trait]
127+
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
128+
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
125129
impl<T: tokio::io::AsyncRead + tokio::io::AsyncSeek + Unpin + Send + Debug + 'static>
126130
AsyncFileReader for TokioReader<T>
127131
{
@@ -158,7 +162,8 @@ impl ObjectReader {
158162
}
159163

160164
#[cfg(feature = "object_store")]
161-
#[async_trait]
165+
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
166+
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
162167
impl AsyncFileReader for ObjectReader {
163168
async fn get_bytes(&self, range: Range<u64>) -> AsyncTiffResult<Bytes> {
164169
self.make_range_request(range).await
@@ -211,7 +216,8 @@ impl ReqwestReader {
211216
}
212217

213218
#[cfg(feature = "reqwest")]
214-
#[async_trait]
219+
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
220+
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
215221
impl AsyncFileReader for ReqwestReader {
216222
async fn get_bytes(&self, range: Range<u64>) -> AsyncTiffResult<Bytes> {
217223
self.make_range_request(range).await

0 commit comments

Comments
 (0)