Skip to content

Commit

Permalink
Try to fix R2 Send/Sync bound issue
Browse files Browse the repository at this point in the history
  • Loading branch information
avsaase committed Mar 20, 2024
1 parent fca35f4 commit 63cb306
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 11 deletions.
26 changes: 23 additions & 3 deletions examples/axum/Cargo.lock

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

3 changes: 2 additions & 1 deletion examples/axum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ crate-type = ["cdylib"]
[dependencies]
worker = { path="../../worker", features=['http'] }
worker-macros = { path="../../worker-macros", features=['http'] }
axum = { version = "0.7", default-features = false }
axum = { version = "0.7", default-features = false, features = ["macros"]}
axum-macros = "0.4.1"
tower-service = "0.3.2"
console_error_panic_hook = { version = "0.1.1" }
wasm-bindgen-futures = "0.4"
31 changes: 24 additions & 7 deletions examples/axum/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
use axum::{routing::get, Router};
use std::sync::Arc;

use axum::{extract::State, response::IntoResponse, routing::get, Router};
// use axum_macros::debug_handler;
use tower_service::Service;
use worker::*;

fn router() -> Router {
Router::new().route("/", get(root))
#[derive(Clone)]
pub struct AppState {
env: Arc<Env>,
}

fn router(env: Env) -> Router {
let state = AppState { env: Arc::new(env) };
Router::new().route("/", get(root)).with_state(state)
}

#[event(fetch)]
async fn fetch(
req: HttpRequest,
_env: Env,
env: Env,
_ctx: Context,
) -> Result<axum::http::Response<axum::body::Body>> {
console_error_panic_hook::set_once();

Ok(router().call(req).await?)
Ok(router(env).call(req).await?)
}

pub async fn root() -> &'static str {
#[axum_macros::debug_handler]
// #[worker::send]
pub async fn root(State(AppState { env }): State<AppState>) -> &'static str {
let objects = env
.bucket("BUCKET")
.unwrap()
.list()
.execute()
.await
.unwrap();
"Hello Axum!"
}
3 changes: 3 additions & 0 deletions worker/src/r2/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,9 @@ pub struct ListOptionsBuilder<'bucket> {
pub(crate) include: Option<Vec<Include>>,
}

unsafe impl Send for ListOptionsBuilder<'_> {}
unsafe impl Sync for ListOptionsBuilder<'_> {}

impl<'bucket> ListOptionsBuilder<'bucket> {
/// The number of results to return. Defaults to 1000, with a maximum of 1000.
pub fn limit(mut self, limit: u32) -> Self {
Expand Down
9 changes: 9 additions & 0 deletions worker/src/r2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pub struct Bucket {
inner: EdgeR2Bucket,
}

unsafe impl Send for Bucket {}
unsafe impl Sync for Bucket {}

impl Bucket {
/// Retrieves the [Object] for the given key containing only object metadata, if the key exists.
pub async fn head(&self, key: impl Into<String>) -> Result<Option<Object>> {
Expand Down Expand Up @@ -165,6 +168,9 @@ pub struct Object {
inner: ObjectInner,
}

unsafe impl Send for Object {}
unsafe impl Sync for Object {}

impl Object {
pub fn key(&self) -> String {
match &self.inner {
Expand Down Expand Up @@ -399,6 +405,9 @@ pub struct Objects {
inner: EdgeR2Objects,
}

unsafe impl Send for Objects {}
unsafe impl Sync for Objects {}

impl Objects {
/// An [Vec] of [Object] matching the [list](Bucket::list) request.
pub fn objects(&self) -> Vec<Object> {
Expand Down

0 comments on commit 63cb306

Please sign in to comment.