diff --git a/examples/binary-echo/src/lib.rs b/examples/binary-echo/src/lib.rs index baf68b0..0e62ce8 100644 --- a/examples/binary-echo/src/lib.rs +++ b/examples/binary-echo/src/lib.rs @@ -1,4 +1,6 @@ -use stateroom_wasm::prelude::*; +use stateroom_wasm::{ + stateroom_wasm, ClientId, MessageRecipient, StateroomContext, StateroomService, +}; #[stateroom_wasm] #[derive(Default)] @@ -6,10 +8,7 @@ struct BinaryEcho; impl StateroomService for BinaryEcho { fn message(&mut self, _: ClientId, message: &str, ctx: &impl StateroomContext) { - ctx.send_binary( - MessageRecipient::Broadcast, - message.as_bytes(), - ); + ctx.send_binary(MessageRecipient::Broadcast, message.as_bytes()); } fn binary(&mut self, _: ClientId, message: &[u8], ctx: &impl StateroomContext) { diff --git a/examples/clock/Cargo.lock b/examples/clock/Cargo.lock index 675f87d..c69cd5f 100644 --- a/examples/clock/Cargo.lock +++ b/examples/clock/Cargo.lock @@ -6,7 +6,6 @@ version = 3 name = "clock" version = "0.1.0" dependencies = [ - "stateroom", "stateroom-wasm", ] diff --git a/examples/clock/Cargo.toml b/examples/clock/Cargo.toml index e5ecb15..ca7bd20 100644 --- a/examples/clock/Cargo.toml +++ b/examples/clock/Cargo.toml @@ -8,5 +8,4 @@ edition = "2018" crate-type = ["cdylib", "rlib"] [dependencies] -stateroom = {path="../../stateroom"} stateroom-wasm = {path="../../stateroom-wasm"} diff --git a/examples/clock/src/lib.rs b/examples/clock/src/lib.rs index c13db66..8af0019 100644 --- a/examples/clock/src/lib.rs +++ b/examples/clock/src/lib.rs @@ -1,4 +1,4 @@ -use stateroom_wasm::prelude::*; +use stateroom_wasm::{stateroom_wasm, MessageRecipient, StateroomContext, StateroomService}; #[stateroom_wasm] #[derive(Default)] diff --git a/examples/counter-service/Cargo.lock b/examples/counter-service/Cargo.lock index 0aff33b..ee787a8 100644 --- a/examples/counter-service/Cargo.lock +++ b/examples/counter-service/Cargo.lock @@ -6,7 +6,6 @@ version = 3 name = "counter" version = "0.1.0" dependencies = [ - "stateroom", "stateroom-wasm", ] diff --git a/examples/counter-service/Cargo.toml b/examples/counter-service/Cargo.toml index 20029c4..4c71d8c 100644 --- a/examples/counter-service/Cargo.toml +++ b/examples/counter-service/Cargo.toml @@ -8,5 +8,4 @@ edition = "2018" crate-type = ["cdylib", "rlib"] [dependencies] -stateroom = {path="../../stateroom"} stateroom-wasm = {path="../../stateroom-wasm"} diff --git a/examples/counter-service/src/lib.rs b/examples/counter-service/src/lib.rs index c13684d..ca5e932 100644 --- a/examples/counter-service/src/lib.rs +++ b/examples/counter-service/src/lib.rs @@ -1,4 +1,6 @@ -use stateroom_wasm::prelude::*; +use stateroom_wasm::{ + stateroom_wasm, ClientId, MessageRecipient, StateroomContext, StateroomService, +}; #[stateroom_wasm] #[derive(Default)] @@ -12,6 +14,9 @@ impl StateroomService for SharedCounterServer { _ => (), } - ctx.send_message(MessageRecipient::Broadcast, &format!("new value: {}", self.0)); + ctx.send_message( + MessageRecipient::Broadcast, + &format!("new value: {}", self.0), + ); } -} \ No newline at end of file +} diff --git a/examples/cpu-hog/Cargo.lock b/examples/cpu-hog/Cargo.lock index 869d34d..fbd304d 100644 --- a/examples/cpu-hog/Cargo.lock +++ b/examples/cpu-hog/Cargo.lock @@ -6,7 +6,6 @@ version = 3 name = "cpu-hog" version = "0.1.0" dependencies = [ - "stateroom", "stateroom-wasm", "wasi", ] diff --git a/examples/cpu-hog/Cargo.toml b/examples/cpu-hog/Cargo.toml index b6393e4..51613b0 100644 --- a/examples/cpu-hog/Cargo.toml +++ b/examples/cpu-hog/Cargo.toml @@ -8,6 +8,5 @@ edition = "2018" crate-type = ["cdylib", "rlib"] [dependencies] -stateroom = {path="../../stateroom"} stateroom-wasm = {path="../../stateroom-wasm"} wasi = "0.10.2" diff --git a/examples/cpu-hog/src/lib.rs b/examples/cpu-hog/src/lib.rs index b8fbb3f..721491d 100644 --- a/examples/cpu-hog/src/lib.rs +++ b/examples/cpu-hog/src/lib.rs @@ -1,4 +1,6 @@ -use stateroom_wasm::prelude::*; +use stateroom_wasm::{ + stateroom_wasm, ClientId, MessageRecipient, StateroomContext, StateroomService, +}; // Seconds per nanosecond. (`wasi::clock_time_get` uses nanos.) const SECONDS: u64 = 1_000_000_000; @@ -8,18 +10,13 @@ const SECONDS: u64 = 1_000_000_000; struct CpuHog; fn get_time() -> u64 { - unsafe { - wasi::clock_time_get(wasi::CLOCKID_REALTIME, 0).unwrap() - } + unsafe { wasi::clock_time_get(wasi::CLOCKID_REALTIME, 0).unwrap() } } impl StateroomService for CpuHog { fn connect(&mut self, _: ClientId, ctx: &impl StateroomContext) { - ctx.send_message( - MessageRecipient::Broadcast, - &format!("Connected."), - ); - + ctx.send_message(MessageRecipient::Broadcast, &format!("Connected.")); + let init_time = get_time(); loop { let cur_time = get_time(); @@ -27,10 +24,7 @@ impl StateroomService for CpuHog { break; } } - - ctx.send_message( - MessageRecipient::Broadcast, - &format!("Finished."), - ); + + ctx.send_message(MessageRecipient::Broadcast, &format!("Finished.")); } } diff --git a/examples/echo-server/src/lib.rs b/examples/echo-server/src/lib.rs index c931595..c82299a 100644 --- a/examples/echo-server/src/lib.rs +++ b/examples/echo-server/src/lib.rs @@ -1,4 +1,6 @@ -use stateroom_wasm::prelude::*; +use stateroom_wasm::{ + stateroom_wasm, ClientId, MessageRecipient, StateroomContext, StateroomService, +}; #[stateroom_wasm] #[derive(Default)] diff --git a/examples/randomness/src/lib.rs b/examples/randomness/src/lib.rs index 55a625a..e1a0bdc 100644 --- a/examples/randomness/src/lib.rs +++ b/examples/randomness/src/lib.rs @@ -1,5 +1,7 @@ use bytemuck::cast; -use stateroom_wasm::prelude::*; +use stateroom_wasm::{ + stateroom_wasm, ClientId, MessageRecipient, StateroomContext, StateroomService, +}; #[stateroom_wasm] #[derive(Default)] diff --git a/stateroom-server/src/server.rs b/stateroom-server/src/server.rs index e1e0bc1..503a3c7 100644 --- a/stateroom-server/src/server.rs +++ b/stateroom-server/src/server.rs @@ -96,7 +96,7 @@ impl ServerState { let mut service = factory.build("", context.clone()).unwrap(); service.init(context.as_ref()); - + loop { let msg = rx.recv().await; match msg { @@ -126,9 +126,7 @@ impl ServerState { pub fn remove(&self, client: &ClientId) { self.inbound_sender - .try_send(Event::Leave { - client: *client, - }) + .try_send(Event::Leave { client: *client }) .unwrap(); self.senders.remove(client); } diff --git a/stateroom-wasm/src/lib.rs b/stateroom-wasm/src/lib.rs index 4d2feff..fb1a55f 100644 --- a/stateroom-wasm/src/lib.rs +++ b/stateroom-wasm/src/lib.rs @@ -1,2 +1,2 @@ +pub use stateroom::{ClientId, MessageRecipient, StateroomContext, StateroomService}; pub use stateroom_wasm_macro::stateroom_wasm; -pub mod prelude; diff --git a/stateroom-wasm/src/prelude.rs b/stateroom-wasm/src/prelude.rs deleted file mode 100644 index 541518d..0000000 --- a/stateroom-wasm/src/prelude.rs +++ /dev/null @@ -1,3 +0,0 @@ -/// Re-exports useful items from `stateroom` and `stateroom_wasm_macro`. -pub use stateroom::{ClientId, MessageRecipient, StateroomContext, StateroomService}; -pub use stateroom_wasm_macro::stateroom_wasm; diff --git a/stateroom-wasm/stateroom-wasm-macro/src/lib.rs b/stateroom-wasm/stateroom-wasm-macro/src/lib.rs index f73c887..7ac39fc 100644 --- a/stateroom-wasm/stateroom-wasm-macro/src/lib.rs +++ b/stateroom-wasm/stateroom-wasm-macro/src/lib.rs @@ -29,12 +29,7 @@ fn stateroom_wasm_impl(item: &proc_macro2::TokenStream) -> proc_macro2::TokenStr mod _stateroom_wasm_macro_autogenerated { extern crate alloc; - use super::*; - use stateroom_wasm::prelude::{ - MessageRecipient, - StateroomContext, - ClientId, - }; + use super::#name; // Instance-global stateroom service. static mut SERVER_STATE: Option<#name> = None; @@ -47,14 +42,14 @@ fn stateroom_wasm_impl(item: &proc_macro2::TokenStream) -> proc_macro2::TokenStr struct GlobalStateroomContext; - impl StateroomContext for GlobalStateroomContext { + impl stateroom_wasm::StateroomContext for GlobalStateroomContext { fn set_timer(&self, ms_delay: u32) { unsafe { ffi::set_timer(ms_delay); } } - fn send_message(&self, recipient: impl Into, message: &str) { + fn send_message(&self, recipient: impl Into, message: &str) { unsafe { ffi::send_message( recipient.into().encode_i32(), @@ -64,7 +59,7 @@ fn stateroom_wasm_impl(item: &proc_macro2::TokenStream) -> proc_macro2::TokenStr } } - fn send_binary(&self, recipient: impl Into, message: &[u8]) { + fn send_binary(&self, recipient: impl Into, message: &[u8]) { unsafe { ffi::send_binary( recipient.into().encode_i32(), @@ -100,17 +95,17 @@ fn stateroom_wasm_impl(item: &proc_macro2::TokenStream) -> proc_macro2::TokenStr } #[no_mangle] - extern "C" fn connect(client_id: ClientId) { + extern "C" fn connect(client_id: stateroom_wasm::ClientId) { match unsafe { SERVER_STATE.as_mut() } { - Some(st) => StateroomService::connect(st, client_id.into(), &GlobalStateroomContext), + Some(st) => stateroom_wasm::StateroomService::connect(st, client_id.into(), &GlobalStateroomContext), None => () } } #[no_mangle] - extern "C" fn disconnect(client_id: ClientId) { + extern "C" fn disconnect(client_id: stateroom_wasm::ClientId) { match unsafe { SERVER_STATE.as_mut() } { - Some(st) => StateroomService::disconnect(st, client_id.into(), &GlobalStateroomContext), + Some(st) => stateroom_wasm::StateroomService::disconnect(st, client_id.into(), &GlobalStateroomContext), None => () } } @@ -118,7 +113,7 @@ fn stateroom_wasm_impl(item: &proc_macro2::TokenStream) -> proc_macro2::TokenStr #[no_mangle] extern "C" fn timer() { match unsafe { SERVER_STATE.as_mut() } { - Some(st) => StateroomService::timer(st, &GlobalStateroomContext), + Some(st) => stateroom_wasm::StateroomService::timer(st, &GlobalStateroomContext), None => () } } @@ -126,30 +121,30 @@ fn stateroom_wasm_impl(item: &proc_macro2::TokenStream) -> proc_macro2::TokenStr #[no_mangle] extern "C" fn init() { match unsafe { SERVER_STATE.as_mut() } { - Some(st) => StateroomService::init(st, &GlobalStateroomContext), + Some(st) => stateroom_wasm::StateroomService::init(st, &GlobalStateroomContext), None => () } } #[no_mangle] - extern "C" fn message(client_id: ClientId, ptr: *const u8, len: usize) { + extern "C" fn message(client_id: stateroom_wasm::ClientId, ptr: *const u8, len: usize) { unsafe { let string = String::from_utf8(std::slice::from_raw_parts(ptr, len).to_vec()).expect("Error parsing UTF-8 from host {:?}"); match SERVER_STATE.as_mut() { - Some(st) => StateroomService::message(st, client_id.into(), &string, &GlobalStateroomContext), + Some(st) => stateroom_wasm::StateroomService::message(st, client_id.into(), &string, &GlobalStateroomContext), None => () } } } #[no_mangle] - extern "C" fn binary(client_id: ClientId, ptr: *const u8, len: usize) { + extern "C" fn binary(client_id: stateroom_wasm::ClientId, ptr: *const u8, len: usize) { unsafe { let data = std::slice::from_raw_parts(ptr, len); match SERVER_STATE.as_mut() { - Some(st) => StateroomService::binary(st, client_id.into(), data, &GlobalStateroomContext), + Some(st) => stateroom_wasm::StateroomService::binary(st, client_id.into(), data, &GlobalStateroomContext), None => () } }