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
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
[target.wasm32-unknown-unknown]
rustflags = ["--cfg", "getrandom_backend=\"wasm_js\""]

[url."https://github.com/"]
insteadOf = ["git@github.com:", "ssh://git@github.com/"]
11 changes: 8 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
[submodule "wasm-bindgen"]
path = wasm-bindgen
url = https://github.com/wasm-bindgen/wasm-bindgen
[submodule "wasm-streams"]
path = wasm-streams
url = https://github.com/guybedford/wasm-streams
[submodule "ts-gen"]
path = ts-gen
url = https://github.com/wasm-bindgen/ts-gen
[submodule "wasm-streams"]
path = wasm-streams
url = https://github.com/guybedford/wasm-streams
branch = fix-unwind-safety-impls
[submodule "gloo"]
path = gloo
url = https://github.com/guybedford/gloo
branch = fix-gloo-timers-unwind-safety
2 changes: 0 additions & 2 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ exclude = [
"examples/axum",
"templates/*",
"wasm-bindgen",
"wasm-streams",
"gloo",
"generated",
]
resolver = "2"
Expand Down Expand Up @@ -113,3 +115,4 @@ wasm-bindgen-macro-support = { version = "0.2.121", path = "./wasm-bindgen/crate
wasm-bindgen-shared = { version = "0.2.121", path = "./wasm-bindgen/crates/shared" }
wasm-bindgen-test = { version = "0.3.71", path = "./wasm-bindgen/crates/test" }
web-sys = { version = "0.3.98", path = './wasm-bindgen/crates/web-sys' }
wasm-streams = { version = "0.5.0", path = './wasm-streams' }
1 change: 1 addition & 0 deletions gloo
Submodule gloo added at d73276
32 changes: 17 additions & 15 deletions test/src/counter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::cell::RefCell;
use std::cell::Cell;
use std::panic::AssertUnwindSafe;
use tokio_stream::{StreamExt, StreamMap};
use worker::{
durable_object, wasm_bindgen, wasm_bindgen_futures, DurableObject, Env, Error, Method, Request,
Expand All @@ -10,28 +11,29 @@ use crate::SomeSharedData;

#[durable_object]
pub struct Counter {
count: RefCell<usize>,
unstored_count: RefCell<usize>,
count: AssertUnwindSafe<Cell<usize>>,
unstored_count: AssertUnwindSafe<Cell<usize>>,
state: State,
initialized: RefCell<bool>,
initialized: AssertUnwindSafe<Cell<bool>>,
env: Env,
}

impl DurableObject for Counter {
fn new(state: State, env: Env) -> Self {
Self {
count: RefCell::new(0),
unstored_count: RefCell::new(0),
initialized: RefCell::new(false),
count: AssertUnwindSafe(Cell::new(0)),
unstored_count: AssertUnwindSafe(Cell::new(0)),
initialized: AssertUnwindSafe(Cell::new(false)),
state,
env,
}
}

async fn fetch(&self, req: Request) -> Result<Response> {
if !*self.initialized.borrow() {
*self.initialized.borrow_mut() = true;
*self.count.borrow_mut() = self.state.storage().get("count").await?.unwrap_or(0);
if !self.initialized.get() {
self.initialized.set(true);
self.count
.set(self.state.storage().get("count").await?.unwrap_or(0));
}

if req.path().eq("/ws") {
Expand All @@ -49,15 +51,15 @@ impl DurableObject for Counter {
.empty());
}

*self.unstored_count.borrow_mut() += 1;
*self.count.borrow_mut() += 10;
let count = *self.count.borrow();
self.unstored_count.set(self.unstored_count.get() + 1);
self.count.set(self.count.get() + 10);
let count = self.count.get();
self.state.storage().put("count", count).await?;

Response::ok(format!(
"[durable_object]: self.count: {}, self.unstored_count: {}, secret value: {}",
self.count.borrow(),
self.unstored_count.borrow(),
self.count.get(),
self.unstored_count.get(),
self.env.secret("SOME_SECRET")?
))
}
Expand Down
23 changes: 13 additions & 10 deletions test/src/durable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use serde::Serialize;
use std::cell::Cell;
use std::collections::HashMap;
use std::convert::TryFrom;
use std::{cell::RefCell, collections::HashMap};
use std::panic::AssertUnwindSafe;
use worker::DurableObject;

use worker::{
Expand All @@ -13,14 +15,14 @@ use worker::{
#[durable_object]
pub struct MyClass {
state: State,
number: RefCell<usize>,
number: AssertUnwindSafe<Cell<usize>>,
}

impl DurableObject for MyClass {
fn new(state: State, _env: Env) -> Self {
Self {
state,
number: RefCell::new(0),
number: AssertUnwindSafe(Cell::new(0)),
}
}

Expand Down Expand Up @@ -153,13 +155,14 @@ impl DurableObject for MyClass {
);
}

*self.number.borrow_mut() = storage.get("count").await?.unwrap_or(0) + 1;
self.number
.set(storage.get("count").await?.unwrap_or(0) + 1);

storage.delete_all().await?;

let count = *self.number.borrow();
let count = self.number.get();
storage.put("count", count).await?;
Response::ok(self.number.borrow().to_string())
Response::ok(self.number.get().to_string())
}
"/transaction" => {
Response::error("transactional storage API is still unstable", 501)
Expand All @@ -178,20 +181,20 @@ impl DurableObject for MyClass {
pub struct AnotherClass {
#[allow(unused)]
state: State,
counter: RefCell<i32>,
counter: AssertUnwindSafe<Cell<i32>>,
}

impl DurableObject for AnotherClass {
fn new(state: State, _env: Env) -> Self {
Self {
state,
counter: RefCell::new(0),
counter: AssertUnwindSafe(Cell::new(0)),
}
}

async fn fetch(&self, _req: Request) -> Result<Response> {
*self.counter.borrow_mut() += 1;
Response::ok(format!("Counter: {}", self.counter.borrow()))
self.counter.set(self.counter.get() + 1);
Response::ok(format!("Counter: {}", self.counter.get()))
}
}

Expand Down
1 change: 1 addition & 0 deletions wasm-streams
Submodule wasm-streams added at c40355
Loading