diff --git a/Cargo.toml b/Cargo.toml index bdf81ba..9c6f69c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "message_worker" -version = "0.6.0" +version = "0.7.0" authors = ["Nick Webster "] repository = "https://github.com/NickGeek/message_worker/" description = "Message Worker is a low-ish level library for Rust for the creation of event-listeners using futures and streams. Notably MW supports non-sync and non-send (i.e. non-thread-safe) contexts within listeners." @@ -16,13 +16,13 @@ name = "benchmarks" harness = false [dependencies] -tokio = { version = "^1.6.0", features = ["rt-multi-thread", "sync", "parking_lot"] } -tokio-stream = { version = "~0.1.6", features = ["sync"] } -ees = "1.0.0" +tokio = { version = "^1.53.3", features = ["rt-multi-thread", "sync", "parking_lot"] } +tokio-stream = { version = "~0.1.19", features = ["sync"] } +thiserror = "2.0.21" [dev-dependencies] -tokio = { version = "^1.6.0", features = ["rt-multi-thread", "sync", "parking_lot", "macros", "test-util"] } -deno_core = { version = "0.199.0", features = ["default", "include_js_files_for_snapshotting"] } -criterion = { version = "0.5.1", features = ["async_tokio", "html_reports"] } -futures = "~0.3.15" -anyhow = "^1.0.40" +tokio = { version = "^1.53.3", features = ["rt-multi-thread", "sync", "parking_lot", "macros", "test-util"] } +deno_core = "0.412.0" +criterion = { version = "0.8.2", features = ["async_tokio", "html_reports"] } +futures = "~0.3.34" +anyhow = "^1.0.104" diff --git a/benches/benchmarks.rs b/benches/benchmarks.rs index 350db0e..5484994 100644 --- a/benches/benchmarks.rs +++ b/benches/benchmarks.rs @@ -3,14 +3,12 @@ use tokio_stream::StreamExt; use anyhow::Result; use message_worker::{blocking, non_blocking, empty_ctx}; -use std::rc::Rc; -use std::sync::Arc; pub fn listener_creation(c: &mut Criterion) { c.bench_function("create listener", |b| { let executor = tokio::runtime::Runtime::new().unwrap(); - async fn handle_message(_ctx: Arc<()>, _event: ()) -> Result> { Ok(None) } + async fn handle_message(_ctx: (), _event: ()) -> Result> { Ok(None) } b.to_async(executor).iter(move || async { let source = tokio_stream::iter(vec![()]); @@ -23,7 +21,7 @@ pub fn listener_process_one(c: &mut Criterion) { c.bench_function("listener process 1 message", |b| { let executor = tokio::runtime::Runtime::new().unwrap(); - async fn handle_message(_ctx: Arc<()>, _event: ()) -> Result> { Ok(None) } + async fn handle_message(_ctx: (), _event: ()) -> Result> { Ok(None) } b.to_async(executor).iter(move || async { let source = tokio_stream::iter(vec![()]); @@ -35,7 +33,7 @@ pub fn listener_process_one(c: &mut Criterion) { pub fn listener_process_many(c: &mut Criterion) { c.bench_function("listener process 1000 messages", |b| { let executor = tokio::runtime::Runtime::new().unwrap(); - async fn handle_message(_ctx: Arc<()>, _event: ()) -> Result> { Ok(None) } + async fn handle_message(_ctx: (), _event: ()) -> Result> { Ok(None) } b.to_async(executor).iter(move || async { let source = futures::stream::repeat(()).take(1000); @@ -48,7 +46,7 @@ pub fn blocking_listener_creation(c: &mut Criterion) { c.bench_function("(blocking) create listener", |b| { let executor = tokio::runtime::Runtime::new().unwrap(); - async fn handle_message(_ctx: Rc<()>, _event: ()) -> Result> { Ok(None) } + async fn handle_message(_ctx: (), _event: ()) -> Result> { Ok(None) } b.to_async(executor).iter(move || async { let source = tokio_stream::iter(vec![()]); @@ -61,7 +59,7 @@ pub fn blocking_listener_process_one(c: &mut Criterion) { c.bench_function("(blocking) listener process 1 message", |b| { let executor = tokio::runtime::Runtime::new().unwrap(); - async fn handle_message(_ctx: Rc<()>, _event: ()) -> Result> { Ok(None) } + async fn handle_message(_ctx: (), _event: ()) -> Result> { Ok(None) } b.to_async(executor).iter(move || async { let source = tokio_stream::iter(vec![()]); @@ -73,7 +71,7 @@ pub fn blocking_listener_process_one(c: &mut Criterion) { pub fn blocking_listener_process_many(c: &mut Criterion) { c.bench_function("(blocking) listener process 1000 messages", |b| { let executor = tokio::runtime::Runtime::new().unwrap(); - async fn handle_message(_ctx: Rc<()>, _event: ()) -> Result> { Ok(None) } + async fn handle_message(_ctx: (), _event: ()) -> Result> { Ok(None) } b.to_async(executor).iter(move || async { let source = futures::stream::repeat(()).take(1000); diff --git a/examples/pingpong/Cargo.lock b/examples/pingpong/Cargo.lock index b01c9eb..a398884 100644 --- a/examples/pingpong/Cargo.lock +++ b/examples/pingpong/Cargo.lock @@ -1,24 +1,18 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "anyhow" -version = "1.0.40" +version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28b2cd92db5cbd74e8e5028f7e27dd7aa3090e89e4f2a197cc7c8dfb69c7063b" - -[[package]] -name = "autocfg" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" [[package]] name = "bitflags" -version = "1.2.1" +version = "2.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" [[package]] name = "bitmaps" @@ -31,48 +25,33 @@ dependencies = [ [[package]] name = "bytes" -version = "1.0.1" +version = "1.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b700ce4376041dcd0a327fd0097c41095743c4c8af8887265942faf1100bd040" +checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" [[package]] name = "cfg-if" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" - -[[package]] -name = "ees" -version = "1.0.0" +version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0dcf5d5be147720bc62622edbf64bbaa1864944a019318caace46602e696c1d" +checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" [[package]] name = "futures-core" -version = "0.3.15" +version = "0.3.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0402f765d8a89a26043b889b26ce3c4679d268fa6bb22cd7c6aad98340e179d1" +checksum = "92d699e522242e69e3003b94ecc1f960f3a5e015aa7c5d7486e65ad01dd94f5e" [[package]] name = "futures-sink" -version = "0.3.15" +version = "0.3.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a57bead0ceff0d6dde8f465ecd96c9338121bb7717d3e7b108059531870c4282" - -[[package]] -name = "hermit-abi" -version = "0.1.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c" -dependencies = [ - "libc", -] +checksum = "1944426bf7d03f1d14f708785e4b33efd750b36d48a157b836b3efc15ede8e1d" [[package]] name = "im" -version = "15.0.0" +version = "15.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "111c1983f3c5bb72732df25cddacee9b546d08325fb584b5ebd38148be7b0246" +checksum = "d0acd33ff0285af998aaf9b57342af478078f53492322fafc47450e09397e0e9" dependencies = [ "bitmaps", "rand_core", @@ -82,88 +61,58 @@ dependencies = [ "version_check", ] -[[package]] -name = "instant" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61124eeebbd69b8190558df225adf7e4caafce0d743919e5d6b19652314ec5ec" -dependencies = [ - "cfg-if", -] - [[package]] name = "libc" -version = "0.2.95" +version = "0.2.191" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "789da6d93f1b866ffe175afc5322a4d76c038605a1c3319bb57b06967ca98a36" +checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" [[package]] name = "lock_api" -version = "0.4.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0382880606dff6d15c9476c416d18690b72742aa7b605bb6dd6ec9030fbf07eb" -dependencies = [ - "scopeguard", -] - -[[package]] -name = "log" version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" +checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" dependencies = [ - "cfg-if", + "scopeguard", ] [[package]] name = "message_worker" -version = "0.5.1" +version = "0.7.0" dependencies = [ - "ees", + "thiserror", "tokio", "tokio-stream", ] -[[package]] -name = "num_cpus" -version = "1.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3" -dependencies = [ - "hermit-abi", - "libc", -] - [[package]] name = "parking_lot" -version = "0.11.1" +version = "0.12.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d7744ac029df22dca6284efe4e898991d28e3085c706c972bcd7da4a27a15eb" +checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" dependencies = [ - "instant", "lock_api", "parking_lot_core", ] [[package]] name = "parking_lot_core" -version = "0.8.3" +version = "0.9.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa7a782938e745763fe6907fc6ba86946d72f49fe7e21de074e08128a99fb018" +checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" dependencies = [ "cfg-if", - "instant", "libc", "redox_syscall", "smallvec", - "winapi", + "windows-link", ] [[package]] name = "pin-project-lite" -version = "0.2.6" +version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc0e1f259c92177c30a4c9d177246edd0a3568b25756a977d0632cf8fa37e905" +checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" [[package]] name = "pingpong" @@ -178,51 +127,51 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.27" +version = "1.0.107" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0d8caf72986c1a598726adc988bb5984792ef84f5ee5aa50209145ee8077038" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" dependencies = [ - "unicode-xid", + "unicode-ident", ] [[package]] name = "quote" -version = "1.0.9" +version = "1.0.47" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" dependencies = [ "proc-macro2", ] [[package]] name = "rand_core" -version = "0.5.1" +version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" [[package]] name = "rand_xoshiro" -version = "0.4.0" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a9fcdd2e881d02f1d9390ae47ad8e5696a9e4be7b547a1da2afbc61973217004" +checksum = "6f97cdb2a36ed4183de61b2f824cc45c9f1037f28afe0a322e9fff4c108b5aaa" dependencies = [ "rand_core", ] [[package]] name = "redox_syscall" -version = "0.2.8" +version = "0.5.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "742739e41cd49414de871ea5e549afb7e2a3ac77b589bcbebe8c82fab37147fc" +checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" dependencies = [ "bitflags", ] [[package]] name = "scopeguard" -version = "1.1.0" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "sized-chunks" @@ -236,29 +185,47 @@ dependencies = [ [[package]] name = "smallvec" -version = "1.6.1" +version = "1.16.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" +checksum = "5b3dc8af474f516a851ff4bd12db780f948b9250ad37211e4eec0bccea54e01b" [[package]] name = "syn" -version = "1.0.72" +version = "3.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d78c8dee4c7bf0e14673097256fed6142ce9d3b85a408189d07482442145823b" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thiserror" +version = "2.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09e52cb86a36cede5cb101bf8908837b3e4c6e5e59fe7fd85c23fb56200d189e" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1e8cdbefb79a9a5a65e0db8b47b723ee907b7c7f8496c76a1770b5c310bab82" +checksum = "fe5197923287db20a58125f0bc85c062f7f2c892de97b18c356f9efb14b28524" dependencies = [ "proc-macro2", "quote", - "unicode-xid", + "syn", ] [[package]] name = "tokio" -version = "1.6.1" +version = "1.53.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a38d31d7831c6ed7aad00aa4c12d9375fd225a6dd77da1d25b707346319a975" +checksum = "ce3335fa71841cda333a58d7615b03901380ecf09d59b3296d21f8bbac0dde4e" dependencies = [ - "autocfg", - "num_cpus", "parking_lot", "pin-project-lite", "tokio-macros", @@ -266,9 +233,9 @@ dependencies = [ [[package]] name = "tokio-macros" -version = "1.2.0" +version = "2.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c49e3df43841dafb86046472506755d8501c5615673955f6aa17181125d13c37" +checksum = "78773a2a397f451582ce068015985c33193cf6dea8b74d2a639fe457b2f07b0e" dependencies = [ "proc-macro2", "quote", @@ -277,9 +244,9 @@ dependencies = [ [[package]] name = "tokio-stream" -version = "0.1.6" +version = "0.1.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8864d706fdb3cc0843a49647ac892720dac98a6eeb818b77190592cf4994066" +checksum = "a3d06f0b082ba57c26b79407372e57cf2a1e28124f78e9479fe80322cf53420b" dependencies = [ "futures-core", "pin-project-lite", @@ -289,54 +256,37 @@ dependencies = [ [[package]] name = "tokio-util" -version = "0.6.7" +version = "0.7.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1caa0b0c8d94a049db56b5acf8cba99dc0623aab1b26d5b5f5e2d945846b3592" +checksum = "e464cf451ba96ebfc6f9b6542f17ee8b8956e33f1e40d9690624e59d7a7f8a4b" dependencies = [ "bytes", "futures-core", "futures-sink", - "log", "pin-project-lite", "tokio", ] [[package]] name = "typenum" -version = "1.13.0" +version = "1.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "879f6906492a7cd215bfa4cf595b600146ccfac0c79bcbd1f3000162af5e8b06" +checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" [[package]] -name = "unicode-xid" -version = "0.2.2" +name = "unicode-ident" +version = "1.0.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" +checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" [[package]] name = "version_check" -version = "0.9.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" - -[[package]] -name = "winapi" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" -dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", -] - -[[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" +version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" [[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" +name = "windows-link" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" diff --git a/examples/pingpong/Cargo.toml b/examples/pingpong/Cargo.toml index 6a37c12..f874623 100644 --- a/examples/pingpong/Cargo.toml +++ b/examples/pingpong/Cargo.toml @@ -9,7 +9,7 @@ license = "MPL-2.0" [dependencies] message_worker = { path = "../.." } -tokio = { version = "^1.6.0", features = ["rt-multi-thread", "sync", "macros", "parking_lot"] } -anyhow = "^1.0.40" -tokio-stream = { version = "~0.1.6", features = ["sync"] } -im = "^15.0.0" +tokio = { version = "^1.53.3", features = ["rt-multi-thread", "sync", "macros", "parking_lot"] } +anyhow = "^1.0.104" +tokio-stream = { version = "~0.1.19", features = ["sync"] } +im = "^15.1.0" diff --git a/examples/pingpong/src/main.rs b/examples/pingpong/src/main.rs index 54e24eb..96ab866 100644 --- a/examples/pingpong/src/main.rs +++ b/examples/pingpong/src/main.rs @@ -29,7 +29,7 @@ async fn pong_actor(ctx: ActorCtx, event: Message) -> Result> { Ok(None) } -async fn error_handler(_ctx: ActorCtx, error: Box) -> bool { +async fn error_handler(_ctx: ActorCtx, error: message_worker::Error) -> bool { eprintln!("There was an error sending an item: {:?}", error); true } diff --git a/examples/pingpong_threaded/Cargo.lock b/examples/pingpong_threaded/Cargo.lock index 3f8855b..0b654a5 100644 --- a/examples/pingpong_threaded/Cargo.lock +++ b/examples/pingpong_threaded/Cargo.lock @@ -1,146 +1,95 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "anyhow" -version = "1.0.40" +version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "28b2cd92db5cbd74e8e5028f7e27dd7aa3090e89e4f2a197cc7c8dfb69c7063b" - -[[package]] -name = "autocfg" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" [[package]] name = "bitflags" -version = "1.2.1" +version = "2.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" [[package]] name = "bytes" -version = "1.0.1" +version = "1.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b700ce4376041dcd0a327fd0097c41095743c4c8af8887265942faf1100bd040" +checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" [[package]] name = "cfg-if" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" - -[[package]] -name = "ees" -version = "1.0.0" +version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0dcf5d5be147720bc62622edbf64bbaa1864944a019318caace46602e696c1d" +checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" [[package]] name = "futures-core" -version = "0.3.15" +version = "0.3.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0402f765d8a89a26043b889b26ce3c4679d268fa6bb22cd7c6aad98340e179d1" +checksum = "92d699e522242e69e3003b94ecc1f960f3a5e015aa7c5d7486e65ad01dd94f5e" [[package]] name = "futures-sink" -version = "0.3.15" +version = "0.3.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a57bead0ceff0d6dde8f465ecd96c9338121bb7717d3e7b108059531870c4282" - -[[package]] -name = "hermit-abi" -version = "0.1.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c" -dependencies = [ - "libc", -] - -[[package]] -name = "instant" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "61124eeebbd69b8190558df225adf7e4caafce0d743919e5d6b19652314ec5ec" -dependencies = [ - "cfg-if", -] +checksum = "1944426bf7d03f1d14f708785e4b33efd750b36d48a157b836b3efc15ede8e1d" [[package]] name = "libc" -version = "0.2.95" +version = "0.2.191" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "789da6d93f1b866ffe175afc5322a4d76c038605a1c3319bb57b06967ca98a36" +checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" [[package]] name = "lock_api" -version = "0.4.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0382880606dff6d15c9476c416d18690b72742aa7b605bb6dd6ec9030fbf07eb" -dependencies = [ - "scopeguard", -] - -[[package]] -name = "log" version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" +checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" dependencies = [ - "cfg-if", + "scopeguard", ] [[package]] name = "message_worker" -version = "0.5.1" +version = "0.7.0" dependencies = [ - "ees", + "thiserror", "tokio", "tokio-stream", ] -[[package]] -name = "num_cpus" -version = "1.13.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3" -dependencies = [ - "hermit-abi", - "libc", -] - [[package]] name = "parking_lot" -version = "0.11.1" +version = "0.12.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d7744ac029df22dca6284efe4e898991d28e3085c706c972bcd7da4a27a15eb" +checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" dependencies = [ - "instant", "lock_api", "parking_lot_core", ] [[package]] name = "parking_lot_core" -version = "0.8.3" +version = "0.9.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa7a782938e745763fe6907fc6ba86946d72f49fe7e21de074e08128a99fb018" +checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" dependencies = [ "cfg-if", - "instant", "libc", "redox_syscall", "smallvec", - "winapi", + "windows-link", ] [[package]] name = "pin-project-lite" -version = "0.2.6" +version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc0e1f259c92177c30a4c9d177246edd0a3568b25756a977d0632cf8fa37e905" +checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" [[package]] name = "pingpong_threaded" @@ -154,62 +103,80 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.27" +version = "1.0.107" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0d8caf72986c1a598726adc988bb5984792ef84f5ee5aa50209145ee8077038" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" dependencies = [ - "unicode-xid", + "unicode-ident", ] [[package]] name = "quote" -version = "1.0.9" +version = "1.0.47" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" dependencies = [ "proc-macro2", ] [[package]] name = "redox_syscall" -version = "0.2.8" +version = "0.5.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "742739e41cd49414de871ea5e549afb7e2a3ac77b589bcbebe8c82fab37147fc" +checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" dependencies = [ "bitflags", ] [[package]] name = "scopeguard" -version = "1.1.0" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "smallvec" -version = "1.6.1" +version = "1.16.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" +checksum = "5b3dc8af474f516a851ff4bd12db780f948b9250ad37211e4eec0bccea54e01b" [[package]] name = "syn" -version = "1.0.72" +version = "3.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d78c8dee4c7bf0e14673097256fed6142ce9d3b85a408189d07482442145823b" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thiserror" +version = "2.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09e52cb86a36cede5cb101bf8908837b3e4c6e5e59fe7fd85c23fb56200d189e" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1e8cdbefb79a9a5a65e0db8b47b723ee907b7c7f8496c76a1770b5c310bab82" +checksum = "fe5197923287db20a58125f0bc85c062f7f2c892de97b18c356f9efb14b28524" dependencies = [ "proc-macro2", "quote", - "unicode-xid", + "syn", ] [[package]] name = "tokio" -version = "1.6.1" +version = "1.53.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a38d31d7831c6ed7aad00aa4c12d9375fd225a6dd77da1d25b707346319a975" +checksum = "ce3335fa71841cda333a58d7615b03901380ecf09d59b3296d21f8bbac0dde4e" dependencies = [ - "autocfg", - "num_cpus", "parking_lot", "pin-project-lite", "tokio-macros", @@ -217,9 +184,9 @@ dependencies = [ [[package]] name = "tokio-macros" -version = "1.2.0" +version = "2.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c49e3df43841dafb86046472506755d8501c5615673955f6aa17181125d13c37" +checksum = "78773a2a397f451582ce068015985c33193cf6dea8b74d2a639fe457b2f07b0e" dependencies = [ "proc-macro2", "quote", @@ -228,9 +195,9 @@ dependencies = [ [[package]] name = "tokio-stream" -version = "0.1.6" +version = "0.1.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8864d706fdb3cc0843a49647ac892720dac98a6eeb818b77190592cf4994066" +checksum = "a3d06f0b082ba57c26b79407372e57cf2a1e28124f78e9479fe80322cf53420b" dependencies = [ "futures-core", "pin-project-lite", @@ -240,42 +207,25 @@ dependencies = [ [[package]] name = "tokio-util" -version = "0.6.7" +version = "0.7.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1caa0b0c8d94a049db56b5acf8cba99dc0623aab1b26d5b5f5e2d945846b3592" +checksum = "e464cf451ba96ebfc6f9b6542f17ee8b8956e33f1e40d9690624e59d7a7f8a4b" dependencies = [ "bytes", "futures-core", "futures-sink", - "log", "pin-project-lite", "tokio", ] [[package]] -name = "unicode-xid" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" - -[[package]] -name = "winapi" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" -dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", -] - -[[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" +name = "unicode-ident" +version = "1.0.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" [[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" +name = "windows-link" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" diff --git a/examples/pingpong_threaded/Cargo.toml b/examples/pingpong_threaded/Cargo.toml index 3dc7d96..a72ace5 100644 --- a/examples/pingpong_threaded/Cargo.toml +++ b/examples/pingpong_threaded/Cargo.toml @@ -9,6 +9,6 @@ license = "MPL-2.0" [dependencies] message_worker = { path = "../.." } -tokio = { version = "^1.6.0", features = ["rt-multi-thread", "sync", "macros", "parking_lot"] } -anyhow = "^1.0.40" -tokio-stream = { version = "~0.1.6", features = ["sync"] } +tokio = { version = "^1.53.3", features = ["rt-multi-thread", "sync", "macros", "parking_lot"] } +anyhow = "^1.0.104" +tokio-stream = { version = "~0.1.19", features = ["sync"] } diff --git a/examples/pingpong_threaded/src/main.rs b/examples/pingpong_threaded/src/main.rs index 5b40c60..6c5e0f6 100644 --- a/examples/pingpong_threaded/src/main.rs +++ b/examples/pingpong_threaded/src/main.rs @@ -31,7 +31,7 @@ async fn pong_actor(ctx: ActorCtx, event: Message) -> Result> { Ok(None) } -async fn error_handler(_ctx: ActorCtx, error: Box) -> bool { +async fn error_handler(_ctx: ActorCtx, error: message_worker::Error) -> bool { eprintln!("There was an error sending an item: {:?}", error); true } diff --git a/src/blocking.rs b/src/blocking.rs index 0cc23d0..57de71b 100644 --- a/src/blocking.rs +++ b/src/blocking.rs @@ -1,9 +1,10 @@ use std::future::Future; -use ees::Error; use tokio::task::JoinHandle; use tokio_stream::{Stream, StreamExt}; +use crate::{Error, HandlerError}; + /// Creates a listener with the default error handler on its own system thread. It is safe to work /// with non-sync and non-send data in this listener. The callback (`handle_event`) will be invoked /// whenever a new item from the `source` stream is emitted. The `context_factory` is a closure you @@ -14,7 +15,6 @@ use tokio_stream::{Stream, StreamExt}; /// use message_worker::blocking::listen; /// use std::rc::Rc; /// use tokio_stream::StreamExt; -/// use ees::Error; /// use tokio_stream::wrappers::ReceiverStream; /// /// # let mut rt = tokio::runtime::Runtime::new().unwrap(); @@ -37,7 +37,7 @@ use tokio_stream::{Stream, StreamExt}; /// (tx, ReceiverStream::new(rx)) /// }; /// -/// async fn handle(ctx: Ctx, _event: ()) -> Result, Error> { +/// async fn handle(ctx: Ctx, _event: ()) -> message_worker::Result> { /// // Accessing from an `Rc` /// let str = (&*ctx.internal_state).clone(); /// @@ -66,7 +66,7 @@ pub fn listen + Unpin + Send + 'static, Message: 'static, HandleEventFuture: Future, HandleMessageErrorT>> + 'static, - HandleMessageErrorT: Into + Send, + HandleMessageErrorT: Into + Send, { listen_with_error_handler(source, context_factory, handle_message, default_error_handler) } @@ -81,7 +81,7 @@ pub fn listen + Unpin + Send + 'static, Message: 'static, HandleEventFuture: Future, HandleMessageErrorT>> + 'static, - HandleMessageErrorT: Into + Send, + HandleMessageErrorT: Into + Send, HandleErrorFuture: Future + 'static { tokio::task::spawn_blocking(move || { @@ -156,7 +156,7 @@ pub fn listen_with_error_handler { - if !handle_error(context.clone(), err.into()).await { + if !handle_error(context.clone(), Error::Handler(err.into())).await { break; } } @@ -409,7 +409,7 @@ mod tests { use std::cell::RefCell; use std::rc::Rc; - use deno_core::{ascii_str, JsRuntime, JsRuntimeForSnapshot, RuntimeOptions, RuntimeSnapshotOptions}; + use deno_core::{JsRuntime, JsRuntimeForSnapshot, PollEventLoopOptions, RuntimeOptions}; use super::*; @@ -443,18 +443,16 @@ mod tests { let mut runtime = JsRuntimeForSnapshot::new(RuntimeOptions { module_loader: Some(Rc::new(deno_core::FsModuleLoader)), ..RuntimeOptions::default() - }, RuntimeSnapshotOptions { - snapshot_module_load_cb: None }); - if let Err(e) = runtime.execute_script_static("", r#"const a = 1 + 1;"#) { + if let Err(e) = runtime.execute_script("", r#"const a = 1 + 1;"#) { failure_msg = Some(match failure_msg { Some(msg) => format!("{}\n{}", msg, e), None => format!("{}", e) }); } - if let Err(e) = runtime.run_event_loop(false).await { + if let Err(e) = runtime.run_event_loop(PollEventLoopOptions::default()).await { failure_msg = Some(match failure_msg { Some(msg) => format!("{}\n{}", msg, e), None => format!("{}", e) @@ -505,9 +503,9 @@ mod tests { runtime.execute_script( "", - ascii_str!(r#"Deno.core.print(`[should_be_able_to_store_the_runtime_on_the_ctx] Got event: ${++a}\n`);"#) + r#"Deno.core.print(`[should_be_able_to_store_the_runtime_on_the_ctx] Got event: ${++a}\n`);"# )?; - runtime.run_event_loop(false).await?; + runtime.run_event_loop(PollEventLoopOptions::default()).await?; ctx.test_res.send(()).await?; Ok(None) @@ -525,14 +523,14 @@ mod tests { ..RuntimeOptions::default() }); - runtime.execute_script_static( + runtime.execute_script( "", r#" Deno.core.print(`[should_be_able_to_store_the_runtime_on_the_ctx] Creating runtime\n`); let a = 0; "# ).unwrap(); - runtime.run_event_loop(false).await.unwrap(); + runtime.run_event_loop(PollEventLoopOptions::default()).await.unwrap(); runtime }).await diff --git a/src/lib.rs b/src/lib.rs index a30925b..39ad9e3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -172,7 +172,7 @@ //! ## The Wild Example (calling V8's C++ via Deno within an event listener to run JS) //! ``` //! use message_worker::blocking::listen; -//! use deno_core::{JsRuntime, RuntimeOptions}; +//! use deno_core::{JsRuntime, PollEventLoopOptions, RuntimeOptions}; //! use std::rc::Rc; //! use std::cell::RefCell; //! use anyhow::Result; @@ -198,11 +198,11 @@ //! let mut ctx = (*ctx).borrow_mut(); //! let runtime = &mut ctx.runtime; //! -//! runtime.execute_script_static( +//! runtime.execute_script( //! "", //! r#"Deno.core.print(`Got a message!\n`);"# //! )?; -//! runtime.run_event_loop(false).await?; +//! runtime.run_event_loop(PollEventLoopOptions::default()).await?; //! //! ctx.test_res.send(()).await?; //! Ok(None) @@ -219,11 +219,11 @@ //! ..RuntimeOptions::default() //! }); //! -//! runtime.execute_script_static( +//! runtime.execute_script( //! "", //! r#"Deno.core.print(`Starting up the JS runtime via C++ FFI and Deno 🤯\n`);"# //! ).unwrap(); -//! runtime.run_event_loop(false).await.unwrap(); +//! runtime.run_event_loop(PollEventLoopOptions::default()).await.unwrap(); //! //! runtime //! }).await @@ -252,6 +252,21 @@ pub mod blocking; /// Listeners that don't block and work with threadsafe (`Sync` + `Send`) data. pub mod non_blocking; +/// The error type that a message handler can return. All error types that implement +/// `std::error::Error + Send + Sync` convert into this type, thus `?` works on them in a handler. +pub type HandlerError = Box; + +/// The result type that a message handler can return. The default error type is [`HandlerError`]. +pub type Result = std::result::Result; + +/// The error that a listener gives to its error handler. +#[derive(Debug, thiserror::Error)] +pub enum Error { + /// An error that the message handler returned. + #[error(transparent)] + Handler(#[from] HandlerError), +} + /// The type of the empty context (unit) pub type EmptyCtx = (); diff --git a/src/non_blocking.rs b/src/non_blocking.rs index fda6830..54fec5f 100644 --- a/src/non_blocking.rs +++ b/src/non_blocking.rs @@ -1,8 +1,9 @@ use std::future::Future; -use ees::Error; use tokio::task::JoinHandle; use tokio_stream::{Stream, StreamExt}; +use crate::{Error, HandlerError}; + /// Creates a listener with the default error handler on its own system thread. It is safe to work /// with non-sync and non-send data in this listener. The callback (`handle_message`) will be invoked /// whenever a new item from the `source` stream is emitted. The `context_factory` is a closure you @@ -17,7 +18,7 @@ pub fn listen + Unpin + Send + 'static, Message: Send + 'static, HandleMessageFuture: Future, HandleMessageErrorT>> + Send + 'static, - HandleMessageErrorT: Into + Send, + HandleMessageErrorT: Into + Send, { listen_with_error_handler(source, context_factory, handle_message, default_error_handler) } @@ -36,7 +37,7 @@ pub fn listen_with_error_handler + Unpin + Send + 'static, Message: Send + 'static, HandleMessageFuture: Future, HandleMessageErrorT>> + Send + 'static, - HandleMessageErrorT: Into + Send, + HandleMessageErrorT: Into + Send, HandleErrorFuture: Future + Send + 'static, { tokio::spawn(async move { @@ -50,7 +51,7 @@ pub fn listen_with_error_handler { - if !handle_error(context.clone(), err.into()).await { + if !handle_error(context.clone(), Error::Handler(err.into())).await { break; } }