Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions web-async/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ edition = "2021"
keywords = ["wasm", "async", "futures"]
categories = ["wasm"]

[dependencies]
tracing = { version = "0.1", optional = true }

[features]
tracing = ["dep:tracing"]

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen-futures = "0.4"
[dependencies]
tracing = { version = "0.1", optional = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
parking_lot = "0.12"
tokio = { version = "1", features = ["rt"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen-futures = "0.4"
19 changes: 9 additions & 10 deletions web-async/src/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use std::ops::{Deref, DerefMut};

// It's a cosmetic wrapper around Arc<Mutex<T>> on native platforms.
// On WASM, it uses Rc<RefCell<T>> instead.
// On native, uses parking_lot::Mutex which has better performance and deadlock detection.
pub struct Lock<T> {
#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
inner: std::sync::Arc<std::sync::Mutex<T>>,
inner: std::sync::Arc<parking_lot::Mutex<T>>,

#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
inner: std::rc::Rc<std::cell::RefCell<T>>,
Expand All @@ -15,13 +16,13 @@ pub struct Lock<T> {
impl<T> Lock<T> {
pub fn new(value: T) -> Self {
Self {
inner: std::sync::Arc::new(std::sync::Mutex::new(value)),
inner: std::sync::Arc::new(parking_lot::Mutex::new(value)),
}
}
}

impl<T> Lock<T> {
pub fn lock(&self) -> LockGuard<T> {
pub fn lock(&self) -> LockGuard<'_, T> {
LockGuard::new(&self.inner)
}

Expand Down Expand Up @@ -55,18 +56,16 @@ impl<T> Clone for Lock<T> {

pub struct LockGuard<'a, T> {
#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
inner: std::sync::MutexGuard<'a, T>,
inner: parking_lot::MutexGuard<'a, T>,

#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
inner: std::cell::RefMut<'a, T>,
}

#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
impl<'a, T> LockGuard<'a, T> {
fn new(inner: &'a std::sync::Arc<std::sync::Mutex<T>>) -> Self {
Self {
inner: inner.lock().unwrap(),
}
fn new(inner: &'a std::sync::Arc<parking_lot::Mutex<T>>) -> Self {
Self { inner: inner.lock() }
}
}

Expand Down Expand Up @@ -101,15 +100,15 @@ impl<T: fmt::Debug> fmt::Debug for LockGuard<'_, T> {

pub struct LockWeak<T> {
#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
inner: std::sync::Weak<std::sync::Mutex<T>>,
inner: std::sync::Weak<parking_lot::Mutex<T>>,

#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
inner: std::rc::Weak<std::cell::RefCell<T>>,
}

#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
impl<T> LockWeak<T> {
fn new(inner: &std::sync::Arc<std::sync::Mutex<T>>) -> Self {
fn new(inner: &std::sync::Arc<parking_lot::Mutex<T>>) -> Self {
Self {
inner: std::sync::Arc::downgrade(inner),
}
Expand Down
20 changes: 20 additions & 0 deletions web-async/src/spawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,35 @@ use std::future::Future;
// TODO: use a send feature and make this runtime agnostic?

#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
#[track_caller]
pub fn spawn<F: Future<Output = ()> + Send + 'static>(f: F) {
#[cfg(feature = "tracing")]
let f = tracing::Instrument::in_current_span(f);
tokio::task::spawn(f);
}

#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
#[track_caller]
pub fn spawn_named<F: Future<Output = ()> + Send + 'static>(name: &str, f: F) {
#[cfg(feature = "tracing")]
let f = tracing::Instrument::in_current_span(f);
tokio::task::Builder::new().name(name).spawn(f).ok();
}

#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
#[track_caller]
pub fn spawn<F: Future<Output = ()> + 'static>(f: F) {
#[cfg(feature = "tracing")]
let f = tracing::Instrument::in_current_span(f);
wasm_bindgen_futures::spawn_local(f);
}

#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
#[track_caller]
pub fn spawn_named<F: Future<Output = ()> + 'static>(name: &str, f: F) {
// WASM doesn't support task names, just spawn normally
#[cfg(feature = "tracing")]
let f = tracing::Instrument::in_current_span(f);
let _ = name; // Suppress unused warning
wasm_bindgen_futures::spawn_local(f);
}
74 changes: 37 additions & 37 deletions web-codecs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ edition = "2021"
categories = ["wasm", "multimedia", "web-programming", "api-bindings"]
rust-version = "1.85"

[package.metadata.docs.rs]
rustdoc-args = ["--cfg", "web_sys_unstable_apis"]
rustc-args = ["--cfg", "web_sys_unstable_apis"]

[dependencies]
bytemuck = "1.22"
bytes = "1"
Expand All @@ -21,44 +25,40 @@ tokio = { version = "1", features = ["sync", "macros"] }
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"

[package.metadata.docs.rs]
rustdoc-args = ["--cfg", "web_sys_unstable_apis"]
rustc-args = ["--cfg", "web_sys_unstable_apis"]

[dependencies.web-sys]
version = "0.3.77"
features = [
"VideoDecoder",
"VideoDecoderInit",
"VideoDecoderConfig",
"VideoFrame",
"VideoColorSpace",
"VideoColorSpaceInit",
"EncodedVideoChunk",
"EncodedVideoChunkInit",
"EncodedVideoChunkType",
"HardwareAcceleration",
"VideoMatrixCoefficients",
"VideoColorPrimaries",
"VideoTransferCharacteristics",
"VideoEncoder",
"VideoEncoderInit",
"VideoEncoderConfig",
"VideoEncoderEncodeOptions",
"LatencyMode",
"AlphaOption",
"EncodedAudioChunk",
"EncodedAudioChunkInit",
"EncodedAudioChunkType",
"AudioData",
"AudioDecoder",
"AudioDecoderInit",
"AudioDecoderConfig",
"AudioEncoder",
"AudioEncoderInit",
"AudioEncoderConfig",
"AudioSampleFormat",
"AudioDataCopyToOptions",
"AudioDataInit",
"console",
"VideoDecoder",
"VideoDecoderInit",
"VideoDecoderConfig",
"VideoFrame",
"VideoColorSpace",
"VideoColorSpaceInit",
"EncodedVideoChunk",
"EncodedVideoChunkInit",
"EncodedVideoChunkType",
"HardwareAcceleration",
"VideoMatrixCoefficients",
"VideoColorPrimaries",
"VideoTransferCharacteristics",
"VideoEncoder",
"VideoEncoderInit",
"VideoEncoderConfig",
"VideoEncoderEncodeOptions",
"LatencyMode",
"AlphaOption",
"EncodedAudioChunk",
"EncodedAudioChunkInit",
"EncodedAudioChunkType",
"AudioData",
"AudioDecoder",
"AudioDecoderInit",
"AudioDecoderConfig",
"AudioEncoder",
"AudioEncoderInit",
"AudioEncoderConfig",
"AudioSampleFormat",
"AudioDataCopyToOptions",
"AudioDataInit",
"console",
]
10 changes: 5 additions & 5 deletions web-streams/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ wasm-bindgen-futures = "0.4"
[dependencies.web-sys]
version = "0.3.77"
features = [
"ReadableStream",
"ReadableStreamDefaultReader",
"ReadableStreamReadResult",
"WritableStream",
"WritableStreamDefaultWriter",
"ReadableStream",
"ReadableStreamDefaultReader",
"ReadableStreamReadResult",
"WritableStream",
"WritableStreamDefaultWriter",
]
Loading