imagesize icon indicating copy to clipboard operation
imagesize copied to clipboard

Async support

Open Bauxitedev opened this issue 2 years ago • 2 comments
trafficstars

Does this crate support async? If not, how would I go about using this in an async context? (e.g. for a file upload in an async web server)

Bauxitedev avatar Feb 13 '23 11:02 Bauxitedev

It does not support async internally and probably won't in the near future.

One goal of this library is to have zero dependencies, and from a quick glance all the async io examples I see use other crates. I'm not saying it'll never happen, but it looks like it'll increase the complexity of the library quite a bit.

As for your file upload example I don't have any specific help to give you, but I do use this crate in an async context via Serenity with no issues. I don't do anything special, I just call it from inside an async function and the library handles execution.

Roughsketch avatar Feb 14 '23 02:02 Roughsketch

I see. For anyone else looking for async support, this is the workaround I managed to come up with. file is a tokio::fs::File.


let std_file = file.try_clone().await?.into_std().await;
let imgsize = spawn_blocking(|| {
    imagesize::reader_size(std::io::BufReader::new(std_file))
})
.await
.expect("Panic occured while trying to infer image size")?;

Bauxitedev avatar Feb 20 '23 10:02 Bauxitedev