Skip to content

Commit

Permalink
Improve macro hygiene (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulgb authored May 2, 2024
1 parent f7b747d commit 6e865d1
Show file tree
Hide file tree
Showing 16 changed files with 44 additions and 58 deletions.
9 changes: 4 additions & 5 deletions examples/binary-echo/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use stateroom_wasm::prelude::*;
use stateroom_wasm::{
stateroom_wasm, ClientId, MessageRecipient, StateroomContext, StateroomService,
};

#[stateroom_wasm]
#[derive(Default)]
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) {
Expand Down
1 change: 0 additions & 1 deletion examples/clock/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion examples/clock/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ edition = "2018"
crate-type = ["cdylib", "rlib"]

[dependencies]
stateroom = {path="../../stateroom"}
stateroom-wasm = {path="../../stateroom-wasm"}
2 changes: 1 addition & 1 deletion examples/clock/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use stateroom_wasm::prelude::*;
use stateroom_wasm::{stateroom_wasm, MessageRecipient, StateroomContext, StateroomService};

#[stateroom_wasm]
#[derive(Default)]
Expand Down
1 change: 0 additions & 1 deletion examples/counter-service/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion examples/counter-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ edition = "2018"
crate-type = ["cdylib", "rlib"]

[dependencies]
stateroom = {path="../../stateroom"}
stateroom-wasm = {path="../../stateroom-wasm"}
11 changes: 8 additions & 3 deletions examples/counter-service/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use stateroom_wasm::prelude::*;
use stateroom_wasm::{
stateroom_wasm, ClientId, MessageRecipient, StateroomContext, StateroomService,
};

#[stateroom_wasm]
#[derive(Default)]
Expand All @@ -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),
);
}
}
}
1 change: 0 additions & 1 deletion examples/cpu-hog/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion examples/cpu-hog/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ edition = "2018"
crate-type = ["cdylib", "rlib"]

[dependencies]
stateroom = {path="../../stateroom"}
stateroom-wasm = {path="../../stateroom-wasm"}
wasi = "0.10.2"
22 changes: 8 additions & 14 deletions examples/cpu-hog/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -8,29 +10,21 @@ 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();
if init_time + 10 * SECONDS < cur_time {
break;
}
}

ctx.send_message(
MessageRecipient::Broadcast,
&format!("Finished."),
);

ctx.send_message(MessageRecipient::Broadcast, &format!("Finished."));
}
}
4 changes: 3 additions & 1 deletion examples/echo-server/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use stateroom_wasm::prelude::*;
use stateroom_wasm::{
stateroom_wasm, ClientId, MessageRecipient, StateroomContext, StateroomService,
};

#[stateroom_wasm]
#[derive(Default)]
Expand Down
4 changes: 3 additions & 1 deletion examples/randomness/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use bytemuck::cast;
use stateroom_wasm::prelude::*;
use stateroom_wasm::{
stateroom_wasm, ClientId, MessageRecipient, StateroomContext, StateroomService,
};

#[stateroom_wasm]
#[derive(Default)]
Expand Down
6 changes: 2 additions & 4 deletions stateroom-server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion stateroom-wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub use stateroom::{ClientId, MessageRecipient, StateroomContext, StateroomService};
pub use stateroom_wasm_macro::stateroom_wasm;
pub mod prelude;
3 changes: 0 additions & 3 deletions stateroom-wasm/src/prelude.rs

This file was deleted.

33 changes: 14 additions & 19 deletions stateroom-wasm/stateroom-wasm-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<MessageRecipient>, message: &str) {
fn send_message(&self, recipient: impl Into<stateroom_wasm::MessageRecipient>, message: &str) {
unsafe {
ffi::send_message(
recipient.into().encode_i32(),
Expand All @@ -64,7 +59,7 @@ fn stateroom_wasm_impl(item: &proc_macro2::TokenStream) -> proc_macro2::TokenStr
}
}

fn send_binary(&self, recipient: impl Into<MessageRecipient>, message: &[u8]) {
fn send_binary(&self, recipient: impl Into<stateroom_wasm::MessageRecipient>, message: &[u8]) {
unsafe {
ffi::send_binary(
recipient.into().encode_i32(),
Expand Down Expand Up @@ -100,56 +95,56 @@ 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 => ()
}
}

#[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 => ()
}
}

#[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 => ()
}
}
Expand Down

0 comments on commit 6e865d1

Please sign in to comment.