Skip to content

Commit b54697f

Browse files
committed
Provide examples for derive macros
While rustdoc can't test these, they're nonetheless invaluable for illustrating proper use.
1 parent 1b45d9f commit b54697f

2 files changed

Lines changed: 40 additions & 7 deletions

File tree

macros/src/lib.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ use syn::{parse_macro_input, DeriveInput};
2626
/// `World::remove`. Monomorphic `Bundle` implementations are slightly more efficient than the
2727
/// polymorphic implementations for tuples, and can be convenient when combined with other derives
2828
/// like `serde::Deserialize`.
29+
///
30+
/// # Example
31+
/// ```ignore
32+
/// #[derive(Bundle)]
33+
/// struct Foo {
34+
/// x: i32,
35+
/// y: char,
36+
/// }
37+
///
38+
/// let mut world = World::new();
39+
/// let e = world.spawn(Foo { x: 42, y: 'a' });
40+
/// assert_eq!(*world.get::<i32>(e).unwrap(), 42);
41+
/// ```
2942
#[proc_macro_derive(Bundle)]
3043
pub fn derive_bundle(input: TokenStream) -> TokenStream {
3144
let input = parse_macro_input!(input as DeriveInput);
@@ -41,6 +54,25 @@ pub fn derive_bundle(input: TokenStream) -> TokenStream {
4154
/// Queries structs can be passed to the type parameter of `World::query`. They must have exactly
4255
/// one lifetime parameter, and all of their fields must be queries (e.g. references) using that
4356
/// lifetime.
57+
///
58+
/// # Example
59+
/// ```ignore
60+
/// #[derive(Query, Debug, PartialEq)]
61+
/// struct Foo<'a> {
62+
/// x: &'a i32,
63+
/// y: &'a mut bool,
64+
/// }
65+
///
66+
/// let mut world = World::new();
67+
/// let e = world.spawn((42, false));
68+
/// assert_eq!(
69+
/// world.query_one_mut::<Foo>(e).unwrap(),
70+
/// Foo {
71+
/// x: &42,
72+
/// y: &mut false
73+
/// }
74+
/// );
75+
/// ```
4476
#[proc_macro_derive(Query)]
4577
pub fn derive_query(input: TokenStream) -> TokenStream {
4678
let input = parse_macro_input!(input as DeriveInput);

tests/tests.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,21 @@ fn query_all() {
6565
#[test]
6666
#[cfg(feature = "macros")]
6767
fn derived_query() {
68-
#[derive(Query)]
68+
#[derive(Query, Debug, PartialEq)]
6969
struct Foo<'a> {
7070
x: &'a i32,
7171
y: &'a mut bool,
7272
}
7373

7474
let mut world = World::new();
7575
let e = world.spawn((42, false));
76-
let ents = world
77-
.query::<Foo>()
78-
.iter()
79-
.map(|(e, q)| (e, *q.x, *q.y))
80-
.collect::<Vec<_>>();
81-
assert_eq!(ents, &[(e, 42, false)]);
76+
assert_eq!(
77+
world.query_one_mut::<Foo>(e).unwrap(),
78+
Foo {
79+
x: &42,
80+
y: &mut false
81+
}
82+
);
8283
}
8384

8485
#[test]

0 commit comments

Comments
 (0)