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

Fix #773 #774

Merged
merged 3 commits into from
Apr 24, 2024
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
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;
ClaasJG marked this conversation as resolved.
Show resolved Hide resolved
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
Loading