Skip to content

Commit

Permalink
Add custom view iteration to guide
Browse files Browse the repository at this point in the history
  • Loading branch information
leudz committed Jun 17, 2024
1 parent df9b003 commit 0196433
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
13 changes: 12 additions & 1 deletion guide/master/src/going-further/custom-views.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ View bundles only contain other views while wild views can contain other types.

Example of a View Bundle:
```rust, noplaypen
#[derive(Borrow, BorrowInfo)]
struct Hierarchy<'v> {
entities: EntitiesViewMut<'v>,
parents: ViewMut<'v, Parent>,
Expand All @@ -19,7 +20,17 @@ Example of a Wild View:
struct RandomNumber(u64);
```

### Concrete example
### Iteration

View bundles can be iterated directly by deriving the `IntoIter` trait.

```rust, noplaypen
{{#include ../../../../tests/book/custom_view.rs:into_iter}}
```

All attributes are optional.

## Concrete example

When creating a frame with any low level api there is always some boilerplate. We'll look at how custom views can help for `wgpu`.

Expand Down
31 changes: 31 additions & 0 deletions tests/book/custom_view.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use shipyard::{Borrow, BorrowInfo, Component, EntitiesViewMut, IntoIter, ViewMut, World};

#[test]
#[rustfmt::skip]
#[allow(unused)]
fn view() {
#[derive(Component)]
struct Parent;
#[derive(Component)]
struct Child;

// ANCHOR: into_iter
#[derive(Borrow, BorrowInfo, IntoIter)]
#[shipyard(item_name = "Node")]
struct Hierarchy<'v> {
#[shipyard(item_field_skip)]
entities: EntitiesViewMut<'v>,
#[shipyard(item_field_name = "parent")]
parents: ViewMut<'v, Parent>,
#[shipyard(item_field_name = "child")]
children: ViewMut<'v, Child>,
}

let world = World::new();

world.run(|mut hierarchy: Hierarchy| {
for Node { parent, child } in hierarchy.iter() {
}
});
// ANCHOR_END: into_iter
}
1 change: 1 addition & 0 deletions tests/book/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use shipyard::{track, Component};

mod add_components;
mod add_entity;
mod custom_view;
mod delete_components;
mod delete_entity;
mod get;
Expand Down

0 comments on commit 0196433

Please sign in to comment.