Skip to content

Commit

Permalink
Merge pull request #774 from ClaasJG/master
Browse files Browse the repository at this point in the history
Fix #773
  • Loading branch information
Imberflur authored Apr 24, 2024
2 parents 708c3ac + 0fc0552 commit b0c961d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
17 changes: 16 additions & 1 deletion docs/tutorials/src/02_hello_world.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,25 @@ struct Velocity {
If the `#[storage(...)]` attribute is omitted, the given component will be
stored in a `DenseVecStorage` by default. But for this example, we are
explicitly asking for these components to be kept in a `VecStorage` instead (see
the later [storages chapter][sc] for more details). But before we move on, we
the later [storages chapter][sc] for more details).

`#[storage(VecStorage)]` assumes `<Self>` as the default type parameter for the storage.
More complex type parameters can be specified explicitly:

```rust,ignore
#[derive(Component, Debug)]
#[storage(FlaggedStorage<Self, DenseVecStorage<Self>>)]
pub struct Data {
[..]
}
```
(see the later [`FlaggedStorage` and modification events chapter][tc] for more details on `FlaggedStorage`)

But before we move on, we
need to create a world in which to store all of our components.

[sc]: ./05_storages.html
[tc]: ./12_tracked.html

## The `World`

Expand Down
21 changes: 18 additions & 3 deletions specs-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extern crate syn;
use proc_macro::TokenStream;
use syn::{
parse::{Parse, ParseStream, Result},
DeriveInput, Path,
DeriveInput, Path, PathArguments,
};

mod impl_saveload;
Expand All @@ -28,7 +28,17 @@ mod impl_saveload;
/// use specs::storage::VecStorage;
///
/// #[derive(Component, Debug)]
/// #[storage(VecStorage)] // This line is optional, defaults to `DenseVecStorage`
/// #[storage(VecStorage<Self>)] // This line is optional, defaults to `DenseVecStorage<Self>`
/// struct Pos(f32, f32, f32);
/// ```
///
/// When the type parameter is `<Self>` it can be omitted i.e.:
///
///```rust,ignore
/// use specs::storage::VecStorage;
///
/// #[derive(Component, Debug)]
/// #[storage(VecStorage)] // Equals to #[storage(VecStorage<Self>)]
/// struct Pos(f32, f32, f32);
/// ```
#[proc_macro_derive(Component, attributes(storage))]
Expand Down Expand Up @@ -68,9 +78,14 @@ fn impl_component(ast: &DeriveInput) -> proc_macro2::TokenStream {
})
.unwrap_or_else(|| parse_quote!(DenseVecStorage));

let additional_generics = match storage.segments.last().unwrap().arguments {
PathArguments::AngleBracketed(_) => quote!(),
_ => quote!(<Self>),
};

quote! {
impl #impl_generics Component for #name #ty_generics #where_clause {
type Storage = #storage<Self>;
type Storage = #storage #additional_generics;
}
}
}
Expand Down

0 comments on commit b0c961d

Please sign in to comment.