Skip to content

Commit

Permalink
fix features
Browse files Browse the repository at this point in the history
  • Loading branch information
Nertsal committed Nov 6, 2024
1 parent 78ad411 commit e611e44
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 136 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: test-all-features
args: --all-targets
args: --workspace --all-targets
- name: Test documentation
uses: actions-rs/cargo@v1
with:
command: test-all-features
args: --doc
command: test
args: --doc --all-features
32 changes: 31 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ default = ["arena", "query_mut", "dynamic"]
query_mut = ["stecs-derive/query_mut"]
hashstorage = []
arena = ["dep:slotmap"]
dynamic = ["dep:anymap3"]
dynamic = ["stecs-derive/dynamic", "dep:anymap3"]

[workspace]
members = ["crates/*"]
Expand All @@ -28,9 +28,39 @@ anymap3 = { version = "1.0", optional = true }
[dev-dependencies]
criterion = { version = "0.5", features = ["html_reports"] }

[package.metadata."docs.rs"]
all-features = true

[[bench]]
name = "pos_vel"
harness = false
required-features = ["query_mut"]
[[bench]]
name = "simple"
harness = false
required-features = ["query_mut"]

[[example]]
name = "full"
doc-scrape-examples = true
required-features = ["query_mut"]

[[example]]
name = "blogpost"
doc-scrape-examples = true
required-features = ["arena", "query_mut"]

[[example]]
name = "mutations"
doc-scrape-examples = true
required-features = ["query_mut"]

[[example]]
name = "generics"
doc-scrape-examples = true
required-features = []

[[example]]
name = "dynamic"
doc-scrape-examples = true
required-features = ["query_mut", "dynamic"]
1 change: 1 addition & 0 deletions crates/stecs-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ proc-macro = true
[features]
default = []
query_mut = []
dynamic = []

[dependencies]
darling = "0.20"
Expand Down
9 changes: 8 additions & 1 deletion crates/stecs-derive/src/get/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ impl StorageGetOpts {
};

get_fields = match optic {
Optic::Dynamic { .. } | Optic::GetId => quote! {
#[cfg(feature = "dynamic")]
Optic::Dynamic { .. } => quote! {
{
let #name = #access;
#get_fields
}
},
Optic::GetId => quote! {
{
let #name = #access;
#get_fields
Expand Down
42 changes: 30 additions & 12 deletions crates/stecs-derive/src/optic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use quote::quote;

#[derive(Debug, Clone)]
pub enum Optic {
#[cfg(feature = "dynamic")]
Dynamic {
ty: syn::Type,
component: OpticComponent,
Expand Down Expand Up @@ -36,6 +37,7 @@ pub enum OpticComponent {

#[derive(Debug, Clone, Copy)]
enum Access {
#[cfg(feature = "dynamic")]
Owned,
Borrow,
BorrowMut,
Expand Down Expand Up @@ -64,6 +66,7 @@ impl Optic {

fn access_impl(&self, is_mut: bool, id: TokenStream, archetype: TokenStream) -> TokenStream {
match self {
#[cfg(feature = "dynamic")]
Optic::Dynamic { ty, component } => {
let value_name = quote! { __value };
let storage = if is_mut {
Expand Down Expand Up @@ -113,6 +116,7 @@ impl Optic {
#[cfg(feature = "query_mut")]
pub fn access_many_mut(&self, ids: TokenStream, archetype: TokenStream) -> TokenStream {
match self {
#[cfg(feature = "dynamic")]
Optic::Dynamic { ty, component } => {
let value_name = quote! { __value };
let access = if component.is_identity() {
Expand Down Expand Up @@ -190,6 +194,7 @@ impl OpticComponent {
};

let convert = match access {
#[cfg(feature = "dynamic")]
Access::Owned => quote! {},
Access::Borrow => quote! { .as_ref() },
Access::BorrowMut => quote! { .as_mut() },
Expand Down Expand Up @@ -218,19 +223,32 @@ enum OpticPart {

impl Parse for Optic {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
if input.parse::<Option<syn::Token![dyn]>>()?.is_some() {
let ty: syn::Type = input.parse()?;

let component = if input.parse::<Option<syn::Token![.]>>()?.is_some() {
let parts =
Punctuated::<OpticPartToken, syn::Token![.]>::parse_separated_nonempty(input)?;
let parts: Vec<_> = parts.into_iter().collect();
build_component_optic(&parts)?
} else {
OpticComponent::Identity
};
if let Some(_dyn) = input.parse::<Option<syn::Token![dyn]>>()? {
#[cfg(not(feature = "dynamic"))]
{
return Err(syn::Error::new_spanned(
_dyn,
"`dyn` components are not available because the `dynamic` feature is disabled",
));
}

return Ok(Optic::Dynamic { ty, component });
#[cfg(feature = "dynamic")]
{
let ty: syn::Type = input.parse()?;

let component = if input.parse::<Option<syn::Token![.]>>()?.is_some() {
let parts =
Punctuated::<OpticPartToken, syn::Token![.]>::parse_separated_nonempty(
input,
)?;
let parts: Vec<_> = parts.into_iter().collect();
build_component_optic(&parts)?
} else {
OpticComponent::Identity
};

return Ok(Optic::Dynamic { ty, component });
}
}

let parts = Punctuated::<OpticPartToken, syn::Token![.]>::parse_separated_nonempty(input)?;
Expand Down
12 changes: 8 additions & 4 deletions crates/stecs-derive/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ impl Parse for QueryOpts {
ImageOpts::Tuple { fields } => fields.iter().any(|field| field.is_mut),
};
if is_mut {
let span = _span_start
.join(input.span())
.expect("spans are from the same input stream");
// NOTE: sometimes in doc tests the `join` fails
let span = _span_start.join(input.span()).unwrap_or(input.span());
return Err(syn::Error::new(
span,
"enable the `query_mut` feature flag to allow mutable queries",
Expand Down Expand Up @@ -99,7 +98,11 @@ impl QueryOpts {
}
} else {
let component = optic.access(id_expr.clone(), quote! { #storage });
if matches!(optic, Optic::Dynamic { .. }) {
#[cfg(not(feature = "dynamic"))]
let dynamic = false;
#[cfg(feature = "dynamic")]
let dynamic = matches!(optic, Optic::Dynamic { .. });
if dynamic {
quote! {
let #name = #ids_expr.map(|#id_expr| {
#component
Expand Down Expand Up @@ -145,6 +148,7 @@ impl QueryOpts {
.iter()
.map(|(name, _, optic)| {
let optional = match optic {
#[cfg(feature = "dynamic")]
Optic::Dynamic { component, .. } => component.is_prism(),
Optic::GetId => false,
Optic::Access { component, .. } => component.is_prism(),
Expand Down
Loading

0 comments on commit e611e44

Please sign in to comment.