Skip to content
Open
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
4 changes: 2 additions & 2 deletions src/convert.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};

/// This wrapper function encapsules our internal serialization format.
/// This wrapper function encapsulates our internal serialization format.
/// It is used internally to prepare values before sending them to a worker
/// or back to the main thread via `postMessage`.
pub fn to_bytes<T: Serialize>(value: &T) -> Box<[u8]> {
Expand All @@ -9,7 +9,7 @@ pub fn to_bytes<T: Serialize>(value: &T) -> Box<[u8]> {
.into()
}

/// This wrapper function encapsules our internal serialization format.
/// This wrapper function encapsulates our internal serialization format.
/// It is used internally to prepare values after receiving them from a worker
/// or the main thread via `postMessage`.
pub fn from_bytes<'de, T: Deserialize<'de>>(bytes: &'de [u8]) -> T {
Expand Down
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ use thiserror::Error;
/// is called.
#[derive(Debug, Error)]
#[error("WebWorker capacity reached")]
// perhaps make this struct forward compatible by including a private member.
// `pub struct Full(());`
pub struct Full;

/// This error is returned during the creation of a new web worker.
/// It covers generic errors in the actual creation and import errors
/// during the initialization.
// perhaps don't expose the variants (for forward compatibility)
#[derive(Debug, Error)]
pub enum InitError {
/// This error covers errors during the `new Worker()` command.
Expand Down
1 change: 1 addition & 0 deletions src/func.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// reviewed
/// This struct describes the function to be called by the worker.
/// It also ensures type safety, when constructed using the [`crate::webworker!`] macro.
pub struct WebWorkerFn<T, R> {
Expand Down
2 changes: 2 additions & 0 deletions src/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ static WORKER_POOL: OnceCell<SendWrapper<WebWorkerPool>> = OnceCell::const_new()
#[wasm_bindgen(js_name = initWorkerPool)]
pub async fn init_worker_pool(options: WorkerPoolOptions) {
WORKER_POOL
// should call `set` instead to correctly error when it has already been initialized.
// otherwise options would get silently ignored.
.get_or_init(|| async move {
SendWrapper::new(
WebWorkerPool::with_options(options)
Expand Down
5 changes: 5 additions & 0 deletions src/iter_ext/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// file can be in src/iter_ext.rs instead of src/iter_ext/mod.rs
use std::borrow::Borrow;

use futures::future::join_all;
Expand Down Expand Up @@ -51,6 +52,10 @@ where
/// vec.iter().try_par_map(webworker!(my_func)).await
/// ```
#[allow(async_fn_in_trait)]
// try sounds like something that could fail.
// maybe: par_map_if_available?
//
// nice idea btw
async fn try_par_map<R>(self, func: WebWorkerFn<T, R>) -> Vec<R>
where
Self::Item: Into<T>,
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// reviewed
#![doc = include_str!("../README.md")]
#![allow(clippy::borrowed_box)]
pub use global::{has_worker_pool, init_worker_pool, worker_pool};
Expand Down
3 changes: 3 additions & 0 deletions src/pool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod scheduler;
/// ```
#[wasm_bindgen(getter_with_clone)]
#[derive(Default, Clone)]
#[non_exhaustive]
pub struct WorkerPoolOptions {
/// The path to the wasm-bindgen glue. By default, this path is inferred.
/// [`crate::WebWorker::with_path`] lists more details on when this path
Expand Down Expand Up @@ -153,6 +154,8 @@ impl WebWorkerPool {
/// ```ignore
/// worker_pool().await.run(webworker!(sort_vec), &my_vec).await
/// ```
// serde feature seems superfluous as it doesn't disable any dependencies.
// suggestion: remove the serde feature, but keep the same functions.
#[cfg(feature = "serde")]
pub async fn run<T, R>(&self, func: WebWorkerFn<T, R>, arg: &T) -> R
where
Expand Down
1 change: 1 addition & 0 deletions src/pool/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use super::WebWorkerPool;
/// ```rust
/// pub use wasmworker::pool::Strategy;
/// ```
#[non_exhaustive] // forward compatibility
#[wasm_bindgen]
#[derive(Default, Clone, Copy, PartialEq, Eq)]
pub enum Strategy {
Expand Down
4 changes: 4 additions & 0 deletions src/webworker/com.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use serde::{Deserialize, Serialize};
/// It also indicates if errors occurred during the import.
#[derive(Deserialize)]
pub(super) struct PostInit {
// could remove the `success` boolean because it's entirely captured by the
// `message` field -- maybe rename it to `error`.
/// `true` if initialization is complete, and `false` if import errors occurred.
pub(crate) success: bool,
/// The `message` is only set if `success` is false.
Expand All @@ -18,6 +20,7 @@ pub(super) struct PostInit {
pub(super) struct Request {
/// This is the internal task id, which is used to match a [`Response`]
/// to the corresponding task.
// should probably be u32 or u64, doesn't depend on the bit width
pub(crate) id: usize,
/// The name of the function to be executed by the worker.
pub(crate) func_name: &'static str,
Expand All @@ -32,6 +35,7 @@ pub(super) struct Request {
#[derive(Serialize, Deserialize)]
pub(super) struct Response {
/// The corresponding task id, matching the original id from the [`Request`] object.
// same here
pub(crate) id: usize,
/// The response, which should only be `None` if the function could not be found.
/// This should never be the case if the [`crate::func::WebWorkerFn`] was constructed
Expand Down
1 change: 1 addition & 0 deletions src/webworker/js.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// reviewed
use js_sys::JsString;
use wasm_bindgen::prelude::wasm_bindgen;

Expand Down
7 changes: 6 additions & 1 deletion src/webworker/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub struct WebWorker {
/// The current task id, which is used to reidentify responses.
current_task: Cell<usize>,
/// A map between task ids and the channel they need to be sent out with.
/// [Response]s will arrive on our callback and we redistribute them to their origin.
/// [`Response`]s will arrive on our callback and we redistribute them to their origin.
open_tasks: Rc<RefCell<HashMap<usize, oneshot::Sender<Response>>>>,
/// The callback handle for the worker.
_callback: Closure<Callback>,
Expand Down Expand Up @@ -296,6 +296,10 @@ impl WebWorker {
func_name,
arg: to_bytes(arg),
};
// could probably extract everything from here into another function,
// to pay less monomorphisation cost, less code that needs to be
// duplicated for each set of argument types that the function is
// called with.

// Create channel and add task.
let (sender, receiver) = oneshot::channel();
Expand All @@ -313,6 +317,7 @@ impl WebWorker {
.expect_throw("WebWorker gone")
.response
.expect_throw("Could not find function");
// until here
from_bytes(&res)
}

Expand Down