Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

maybe_send macros to replace cfg_attr for async stuff #101

Merged
merged 4 commits into from
Oct 10, 2023
Merged
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
18 changes: 13 additions & 5 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
resolver = "2"
members = [
"compiler-core/",
"compiler-macros/",
"compiler-types/",
"compiler-wasm/",
"web-server/",
Expand Down
3 changes: 3 additions & 0 deletions compiler-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2021"
async-recursion = "1.0.4"
async-trait = "0.1.73"
cached = { version = "0.45.1", features = ["async"]}
compiler-macros = { path = "../compiler-macros" }
compiler-types = { path = "../compiler-types" }
derivative = "2.2.0"
futures = "0.3.28"
Expand Down Expand Up @@ -41,7 +42,9 @@ wasm = [
"js-sys",
"web-sys/Window",
"instant/wasm-bindgen",
"no-async-send",
]
no-async-send=[]

[lib]
name = "celerc"
Expand Down
4 changes: 2 additions & 2 deletions compiler-core/src/comp/comp_preset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use serde_json::Value;

use crate::json::{Cast, Coerce};
use crate::lang::PresetInst;
use crate::macros::{async_recursion, maybe_send};
use crate::prop;

use super::{validate_not_array_or_object, Compiler, CompilerError};
Expand All @@ -12,8 +13,7 @@ impl Compiler {
/// Apply the preset to the output.
///
/// Presets are applied recursively, including presets in the movements
#[cfg_attr(not(feature = "wasm"), async_recursion::async_recursion)]
#[cfg_attr(feature = "wasm", async_recursion::async_recursion(?Send))]
#[maybe_send(async_recursion)]
pub async fn apply_preset(
&self,
depth: usize,
Expand Down
4 changes: 2 additions & 2 deletions compiler-core/src/lang/preset/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::collections::BTreeMap;
use serde_json::Value;

use crate::lang::TempStr;
use crate::macros::{async_recursion, maybe_send};

use super::{Preset, PresetBlob};

Expand Down Expand Up @@ -37,8 +38,7 @@ impl PresetBlob {
/// If the blob has any template strings in it, returns a Some variant with
/// the template strings compiled and the input value taken out.
/// Otherwise returns a None variant.
#[cfg_attr(not(feature = "wasm"), async_recursion::async_recursion)]
#[cfg_attr(feature = "wasm", async_recursion::async_recursion(?Send))]
#[maybe_send(async_recursion)]
async fn compile_internal(value: &mut Value) -> Option<Self> {
match value {
Value::String(s) => {
Expand Down
5 changes: 3 additions & 2 deletions compiler-core/src/lang/preset/hydrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::collections::BTreeMap;

use serde_json::{json, Value};

use crate::macros::{async_recursion, maybe_send};

use super::{Preset, PresetBlob};

impl Preset {
Expand All @@ -22,8 +24,7 @@ impl Preset {

impl PresetBlob {
/// Hydrate a preset blob with the given arguments
#[cfg_attr(not(feature = "wasm"), async_recursion::async_recursion)]
#[cfg_attr(feature = "wasm", async_recursion::async_recursion(?Send))]
#[maybe_send(async_recursion)]
pub async fn hydrate<S>(&self, args: &[S]) -> Value
where
S: AsRef<str> + Sync,
Expand Down
7 changes: 7 additions & 0 deletions compiler-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@ pub mod plug;
pub mod metrics;
pub mod prop;
pub mod util;

/// Re-exports of macros
pub mod macros {
pub use async_recursion::async_recursion;
pub use async_trait::async_trait;
pub use celercmacros::maybe_send;
}
4 changes: 2 additions & 2 deletions compiler-core/src/pack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub use resource::*;

use crate::json::Cast;
use crate::lang::parse_poor;
use crate::macros::{async_recursion, maybe_send};

#[derive(Debug, Clone, PartialEq, thiserror::Error, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", tag = "type", content = "data")]
Expand Down Expand Up @@ -230,8 +231,7 @@ impl PackerValue {
}
}

#[cfg_attr(not(feature = "wasm"), async_recursion::async_recursion)]
#[cfg_attr(feature = "wasm", async_recursion::async_recursion(?Send))]
#[maybe_send(async_recursion)]
async fn flatten_internal(self, output_errors: &mut Vec<PackerError>) -> Option<Value> {
match self {
Self::Ok(x) => Some(x),
Expand Down
4 changes: 2 additions & 2 deletions compiler-core/src/pack/pack_preset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use serde_json::Value;

use crate::json::Cast;
use crate::lang::Preset;
use crate::macros::{async_recursion, maybe_send};
use crate::prop;
use crate::util::async_for;

Expand All @@ -20,8 +21,7 @@ pub async fn pack_presets(
Ok(output)
}

#[cfg_attr(not(feature = "wasm"), async_recursion::async_recursion)]
#[cfg_attr(feature = "wasm", async_recursion::async_recursion(?Send))]
#[maybe_send(async_recursion)]
async fn pack_presets_internal(
preset_name: &str,
value: Value,
Expand Down
4 changes: 2 additions & 2 deletions compiler-core/src/pack/pack_route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::collections::BTreeMap;
use serde_json::Value;

use crate::json::Cast;
use crate::macros::{async_recursion, maybe_send};
use crate::util::async_for;

use super::{PackerError, PackerValue, Resource, Use, ValidUse};
Expand All @@ -29,8 +30,7 @@ pub async fn pack_route(
}

/// Pack a portion of the route
#[cfg_attr(not(feature = "wasm"), async_recursion::async_recursion)]
#[cfg_attr(feature = "wasm", async_recursion::async_recursion(?Send))]
#[maybe_send(async_recursion)]
async fn pack_route_internal(
// The resource that contains the route
resource: &Resource,
Expand Down
4 changes: 2 additions & 2 deletions compiler-core/src/pack/resource/loader.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use serde_json::Value;

use crate::macros::{async_trait, maybe_send};
use crate::pack::{PackerError, PackerResult};

/// Loader that loads resources from external place
#[cfg_attr(not(feature = "wasm"), async_trait::async_trait)]
#[cfg_attr(feature = "wasm", async_trait::async_trait(?Send))]
#[maybe_send(async_trait)]
pub trait ResourceLoader {
/// Load a resource as raw bytes
async fn load_raw(&self, path: &str) -> PackerResult<Vec<u8>>;
Expand Down
5 changes: 3 additions & 2 deletions compiler-core/src/pack/resource/loader_cache.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/// currently unused. implementation kept for reference
use cached::proc_macro::cached;
use serde_json::Value;

use crate::macros::{async_recursion, maybe_send};
use crate::pack::PackerResult;

use super::{ArcLoader, ResourceLoader};
Expand All @@ -16,8 +18,7 @@ impl<L> GlobalCacheLoader<L> {
}
}

#[cfg_attr(not(feature = "wasm"), async_trait::async_trait)]
#[cfg_attr(feature = "wasm", async_trait::async_trait(?Send))]
#[maybe_send(async_trait)]
impl<L> ResourceLoader for GlobalCacheLoader<L> where L: ResourceLoader {
async fn load_raw(&self, r: &str) -> PackerResult<Vec<u8>> {
load_raw_internal(&self.delegate, r).await
Expand Down
4 changes: 2 additions & 2 deletions compiler-core/src/pack/resource/loader_empty.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::macros::{async_trait, maybe_send};
use crate::pack::{PackerError, PackerResult};

use super::ResourceLoader;
Expand All @@ -13,8 +14,7 @@ impl EmptyLoader {
}
}

#[cfg_attr(not(feature = "wasm"), async_trait::async_trait)]
#[cfg_attr(feature = "wasm", async_trait::async_trait(?Send))]
#[maybe_send(async_trait)]
impl ResourceLoader for EmptyLoader {
async fn load_raw(&self, _: &str) -> PackerResult<Vec<u8>> {
Err(Self::throw())
Expand Down
6 changes: 3 additions & 3 deletions compiler-core/src/pack/resource/resource_github.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//! GitHub resource resolver and loader impl
use std::sync::Arc;

use crate::macros::{async_trait, maybe_send};
use crate::pack::{PackerError, PackerResult, ValidUse};
use crate::util::Path;

use super::{ArcLoader, EmptyLoader, Resource, ResourcePath, ResourceResolver};
use crate::pack::{PackerError, PackerResult, ValidUse};

pub struct GitHubResourceResolver {
owner: String,
Expand All @@ -24,8 +25,7 @@ impl GitHubResourceResolver {
}
}

#[cfg_attr(not(feature = "wasm"), async_trait::async_trait)]
#[cfg_attr(feature = "wasm", async_trait::async_trait(?Send))]
#[maybe_send(async_trait)]
impl ResourceResolver for GitHubResourceResolver {
async fn resolve(&self, source: &Resource, target: &ValidUse) -> PackerResult<Resource> {
match target {
Expand Down
12 changes: 6 additions & 6 deletions compiler-core/src/pack/resource/resource_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ use std::sync::Arc;

use serde_json::Value;

use crate::macros::{async_trait, maybe_send};
use crate::pack::{PackerResult, ValidUse};
use crate::util::Path;

use super::ResourceLoader;

#[cfg(not(feature = "wasm"))]
#[cfg(not(feature = "no-async-send"))]
pub type ArcLoader = Arc<dyn ResourceLoader + Send + Sync>;
#[cfg(not(feature = "wasm"))]
#[cfg(not(feature = "no-async-send"))]
pub type ArcResolver = Arc<dyn ResourceResolver + Send + Sync>;
#[cfg(feature = "wasm")]
#[cfg(feature = "no-async-send")]
pub type ArcLoader = Arc<dyn ResourceLoader>;
#[cfg(feature = "wasm")]
#[cfg(feature = "no-async-send")]
pub type ArcResolver = Arc<dyn ResourceResolver>;

macro_rules! loader_delegate {
Expand Down Expand Up @@ -81,8 +82,7 @@ impl Resource {
}
}

#[cfg_attr(not(feature = "wasm"), async_trait::async_trait)]
#[cfg_attr(feature = "wasm", async_trait::async_trait(?Send))]
#[maybe_send(async_trait)]
pub trait ResourceResolver {
/// Resolve a resource from the given `Use` and loader
async fn resolve(&self, source: &Resource, target: &ValidUse) -> PackerResult<Resource>;
Expand Down
4 changes: 2 additions & 2 deletions compiler-core/src/pack/resource/resource_local.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::sync::Arc;

use crate::macros::{async_trait, maybe_send};
use crate::pack::{PackerError, PackerResult, ValidUse};
use crate::util::Path;

use super::{create_github_resource_from, Resource, ResourcePath, ResourceResolver};

pub struct LocalResourceResolver(pub Path);

#[cfg_attr(not(feature = "wasm"), async_trait::async_trait)]
#[cfg_attr(feature = "wasm", async_trait::async_trait(?Send))]
#[maybe_send(async_trait)]
impl ResourceResolver for LocalResourceResolver {
async fn resolve(&self, source: &Resource, target: &ValidUse) -> PackerResult<Resource> {
match target {
Expand Down
14 changes: 14 additions & 0 deletions compiler-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "compiler-macros"
version = "0.0.0"
description = "(Proc) Macros for the compiler core"
edition = "2021"

[dependencies]
proc-macro2 = "1.0.69"
quote = "1.0.33"

[lib]
proc-macro = true
name = "celercmacros"
path = "src/lib.rs"
49 changes: 49 additions & 0 deletions compiler-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use proc_macro2::TokenStream;
use quote::quote;

/// A wrapper to add Send trait to `async_trait` and `async_recursion` based on the `no-async-send`
/// feature gate
///
/// # Examples
/// Instead of
/// ```ignore
/// #[async_trait]
/// pub trait XXX {
/// ...
/// }
/// ```
/// Do
/// ```ignore
/// #[maybe_send(async_trait)]
/// pub trait XXX {
/// ...
/// }
/// ```
/// Instead of
/// ```ignore
/// #[async_recursion]
/// pub async fn foo() {
/// ...
/// }
/// ```
/// Do
/// ```ignore
/// #[maybe_send(async_recursion)]
/// pub async fn foo() {
/// ...
/// }
/// ```
#[proc_macro_attribute]
pub fn maybe_send(
attr: proc_macro::TokenStream,
input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let attr = TokenStream::from(attr);
let input = TokenStream::from(input);
let tokens = quote! {
#[cfg_attr(not(feature="no-async-send"), #attr)]
#[cfg_attr(feature="no-async-send", #attr(?Send))]
#input
};
tokens.into()
}
1 change: 0 additions & 1 deletion compiler-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ serde-wasm-bindgen = "0.5.0"
wasm-bindgen = "0.2.87"
wasm-bindgen-futures = "0.4.37"
js-sys = "0.3.64"
async-trait = "0.1.73"
log = { version = "0.4.20", features = ["std"] }
web-sys = { version = "0.3.64", features = ["console", "Location", "Window"] }
base64 = "0.21.4"
Expand Down
Loading