From 032f126bd756e2f885fc8e3755edcfa787937b03 Mon Sep 17 00:00:00 2001 From: = Date: Sat, 6 Mar 2021 20:53:50 +0100 Subject: [PATCH 01/10] I need help --- quill/api/src/hlist.rs | 175 +++++++++++++++++++ quill/api/src/lib.rs | 3 + quill/api/src/query.rs | 16 +- quill/api/src/send_message.rs | 19 ++ quill/example-plugins/block-place/src/lib.rs | 2 +- 5 files changed, 204 insertions(+), 11 deletions(-) create mode 100644 quill/api/src/hlist.rs create mode 100644 quill/api/src/send_message.rs diff --git a/quill/api/src/hlist.rs b/quill/api/src/hlist.rs new file mode 100644 index 000000000..e1b22689b --- /dev/null +++ b/quill/api/src/hlist.rs @@ -0,0 +1,175 @@ +use std::marker::PhantomData; + +pub struct HCons { + pub head: Head, + pub tail: Tail, +} + +pub trait HList: Sized { + type Tuple: Tuple; + + fn flatten(self) -> Self::Tuple; +} + +pub trait Tuple: Sized { + type HList: HList; + + fn hlist(self) -> Self::HList; +} + +impl HList for () { + type Tuple = (); + + #[inline] + fn flatten(self) -> Self::Tuple {} +} + +impl Tuple for () { + type HList = (); + + #[inline] + fn hlist(self) -> Self::HList {} +} + +macro_rules! Product { + () => { () }; + ($head:ident $(,$tail:ident)* $(,)?) => { + HCons<$head, Product!($($tail),*)> + }; +} + +macro_rules! product_pat { + () => { () }; + ($head:ident $(,$tail:ident)* $(,)?) => { + HCons { + head: $head, + tail: product_pat!($($tail),*) + } + }; +} + +macro_rules! impl_tuple { + ($head:ident $(,$tail:ident)* $(,)?) => { + impl<$head $(,$tail)*> Tuple for ($head, $($tail),*) { + type HList = HCons<$head, <($($tail,)*) as Tuple>::HList>; + + #[inline] + fn hlist(self) -> Self::HList { + #[allow(non_snake_case)] + let ($head, $($tail),*) = self; + HCons { + head: $head, + tail: ($($tail,)*).hlist(), + } + } + } + + impl<$head $(,$tail)*> HList for Product!($head $(,$tail)*) { + type Tuple = ($head, $($tail),*); + + #[inline] + fn flatten(self) -> Self::Tuple { + #[allow(non_snake_case)] + let product_pat!($head, $($tail),*) = self; + ($head, $($tail),*) + } + } + }; +} + +macro_rules! smaller_tuples_too { + ($macro:ident, $head:ident $(,)?) => { + $macro!($head); + }; + ($macro:ident, $head:ident, $($tail:ident),* $(,)?) => { + $macro!($head, $($tail),*); + smaller_tuples_too!($macro, $($tail),*); + }; +} + +smaller_tuples_too!( + impl_tuple, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, + T19, T20 +); + + +pub trait Plucker { + type Remainder; + + fn pluck(self) -> (Target, Self::Remainder); +} + +pub trait PluckerRef { + fn pluck(&self) -> &Target; +} + +pub trait PluckerRefMut { + fn pluck(&mut self) -> &mut Target; +} + +struct Here {} + +impl Plucker for HCons { + type Remainder = Tail; + + #[inline] + fn pluck(self) -> (Target, Self::Remainder) { + (self.head, self.tail) + } +} + +impl PluckerRef for HCons { + #[inline] + fn pluck(&self) -> &Target { + &self.head + } +} + +impl PluckerRefMut for HCons { + #[inline] + fn pluck(&mut self) -> &mut Target { + &mut self.head + } +} + +struct There(PhantomData); + +impl Plucker> for HCons +where + Tail: Plucker, +{ + type Remainder = HCons>::Remainder>; + + #[inline] + fn pluck(self) -> (Target, Self::Remainder) { + let (target, tail_remainder): (Target, >::Remainder) = + >::pluck(self.tail); + ( + target, + HCons { + head: self.head, + tail: tail_remainder, + }, + ) + } +} + +impl PluckerRef> for HCons +where + Tail: PluckerRef, +{ + #[inline] + fn pluck(&self) -> &Target { + >::pluck(&self.tail) + } +} + +impl PluckerRefMut> for HCons +where + Tail: PluckerRefMut, +{ + #[inline] + fn pluck(&mut self) -> &mut Target { + >::pluck(&mut self.tail) + } +} \ No newline at end of file diff --git a/quill/api/src/lib.rs b/quill/api/src/lib.rs index 658995958..597790dd6 100644 --- a/quill/api/src/lib.rs +++ b/quill/api/src/lib.rs @@ -5,11 +5,14 @@ mod entity; mod entity_builder; mod game; pub mod query; +pub mod hlist; mod setup; +mod send_message; pub use entity::{Entity, EntityId}; pub use entity_builder::EntityBuilder; pub use game::Game; +pub use hlist::*; pub use setup::Setup; #[doc(inline)] diff --git a/quill/api/src/query.rs b/quill/api/src/query.rs index 64da4919e..7c8170ed7 100644 --- a/quill/api/src/query.rs +++ b/quill/api/src/query.rs @@ -4,7 +4,7 @@ use std::{marker::PhantomData, mem::MaybeUninit}; use quill_common::{entity::QueryData, Component, HostComponent, PointerMut}; -use crate::{Entity, EntityId}; +use crate::{Entity, EntityId, Tuple}; /// A type that can be used for a query. /// @@ -65,24 +65,19 @@ where macro_rules! impl_query_tuple { ($($query:ident),* $(,)?) => { impl <$($query: Query),*> Query for ($($query,)*) { - type Item = ($($query::Item),*); + type Item = ($($query::Item,)*); fn add_component_types(types: &mut Vec) { - $( - $query::add_component_types(types); - )* + $($query::add_component_types(types);)* } unsafe fn get_unchecked(data: &QueryData, component_index: &mut usize, component_offsets: &mut [usize]) -> Self::Item { - ( - $( - $query::get_unchecked(data, component_index, component_offsets) - ),* - ) + ($($query::get_unchecked(data, component_index, component_offsets),)*) } } } } +impl_query_tuple!(A); impl_query_tuple!(A, B); impl_query_tuple!(A, B, C); impl_query_tuple!(A, B, C, D); @@ -137,6 +132,7 @@ where impl Iterator for QueryIter where Q: Query, + ::Item: Tuple, { type Item = (Entity, Q::Item); diff --git a/quill/api/src/send_message.rs b/quill/api/src/send_message.rs new file mode 100644 index 000000000..5b09bc7db --- /dev/null +++ b/quill/api/src/send_message.rs @@ -0,0 +1,19 @@ +use quill_common::entities::Player; + +use crate::{Entity, Plucker, PluckerRef, Tuple}; + +pub trait SendMessage { + fn send_message(&self, message: &str); +} + +impl SendMessage for (Entity, T) +where + T: Tuple, + ::HList: PluckerRef +{ + fn send_message(&self, message: &str) { + let components = &self.1; + let foo = components.hlist(); + let player = PluckerRef::::pluck(components.hlist()); + } +} \ No newline at end of file diff --git a/quill/example-plugins/block-place/src/lib.rs b/quill/example-plugins/block-place/src/lib.rs index fa1be6692..651d890bf 100644 --- a/quill/example-plugins/block-place/src/lib.rs +++ b/quill/example-plugins/block-place/src/lib.rs @@ -16,7 +16,7 @@ impl Plugin for BlockPlace { } fn system(_plugin: &mut BlockPlace, game: &mut Game) { - for (_entity, _event) in game.query::<&BlockPlacementEvent>() { + for (_entity, _event) in game.query::<(&BlockPlacementEvent,)>() { println!("A client has placed a block!"); } } From b5dbaaa5f18ca0b9f824d95758cabae6fb917713 Mon Sep 17 00:00:00 2001 From: = Date: Sat, 6 Mar 2021 22:04:44 +0100 Subject: [PATCH 02/10] ... --- quill/api/src/hlist.rs | 42 ----------------------------------- quill/api/src/send_message.rs | 7 +++--- 2 files changed, 3 insertions(+), 46 deletions(-) diff --git a/quill/api/src/hlist.rs b/quill/api/src/hlist.rs index e1b22689b..e86eee226 100644 --- a/quill/api/src/hlist.rs +++ b/quill/api/src/hlist.rs @@ -99,14 +99,6 @@ pub trait Plucker { fn pluck(self) -> (Target, Self::Remainder); } -pub trait PluckerRef { - fn pluck(&self) -> &Target; -} - -pub trait PluckerRefMut { - fn pluck(&mut self) -> &mut Target; -} - struct Here {} impl Plucker for HCons { @@ -118,20 +110,6 @@ impl Plucker for HCons { } } -impl PluckerRef for HCons { - #[inline] - fn pluck(&self) -> &Target { - &self.head - } -} - -impl PluckerRefMut for HCons { - #[inline] - fn pluck(&mut self) -> &mut Target { - &mut self.head - } -} - struct There(PhantomData); impl Plucker> for HCons @@ -152,24 +130,4 @@ where }, ) } -} - -impl PluckerRef> for HCons -where - Tail: PluckerRef, -{ - #[inline] - fn pluck(&self) -> &Target { - >::pluck(&self.tail) - } -} - -impl PluckerRefMut> for HCons -where - Tail: PluckerRefMut, -{ - #[inline] - fn pluck(&mut self) -> &mut Target { - >::pluck(&mut self.tail) - } } \ No newline at end of file diff --git a/quill/api/src/send_message.rs b/quill/api/src/send_message.rs index 5b09bc7db..b3269050a 100644 --- a/quill/api/src/send_message.rs +++ b/quill/api/src/send_message.rs @@ -1,6 +1,6 @@ use quill_common::entities::Player; -use crate::{Entity, Plucker, PluckerRef, Tuple}; +use crate::{Entity, Plucker, Tuple}; pub trait SendMessage { fn send_message(&self, message: &str); @@ -9,11 +9,10 @@ pub trait SendMessage { impl SendMessage for (Entity, T) where T: Tuple, - ::HList: PluckerRef + ::HList: Plucker, Index> { fn send_message(&self, message: &str) { let components = &self.1; - let foo = components.hlist(); - let player = PluckerRef::::pluck(components.hlist()); + let player = Plucker::::pluck(&components.hlist()); } } \ No newline at end of file From 8f65247466c58c3c6e3e4281756517c285b22768 Mon Sep 17 00:00:00 2001 From: = Date: Sat, 6 Mar 2021 22:05:13 +0100 Subject: [PATCH 03/10] ... --- quill/api/src/send_message.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quill/api/src/send_message.rs b/quill/api/src/send_message.rs index b3269050a..c045732b2 100644 --- a/quill/api/src/send_message.rs +++ b/quill/api/src/send_message.rs @@ -9,7 +9,7 @@ pub trait SendMessage { impl SendMessage for (Entity, T) where T: Tuple, - ::HList: Plucker, Index> + ::HList: Plucker { fn send_message(&self, message: &str) { let components = &self.1; From e3f08d4c5aeb7bda5dc79955ccafccc286a1ba10 Mon Sep 17 00:00:00 2001 From: = Date: Sun, 7 Mar 2021 11:07:44 +0100 Subject: [PATCH 04/10] maybe? --- quill/api/src/hlist.rs | 79 ++++++++++++++++++- quill/api/src/lib.rs | 2 +- quill/api/src/send_message.rs | 18 ++--- .../example-plugins/query-entities/src/lib.rs | 8 +- 4 files changed, 93 insertions(+), 14 deletions(-) diff --git a/quill/api/src/hlist.rs b/quill/api/src/hlist.rs index e86eee226..3ec7ac994 100644 --- a/quill/api/src/hlist.rs +++ b/quill/api/src/hlist.rs @@ -11,12 +11,24 @@ pub trait HList: Sized { fn flatten(self) -> Self::Tuple; } +pub trait HListRef<'a>: Sized { + type Tuple: TupleRef<'a, HList = Self>; + + fn flatten(&'a self) -> Self::Tuple; +} + pub trait Tuple: Sized { type HList: HList; fn hlist(self) -> Self::HList; } +pub trait TupleRef<'a>: Sized { + type HList: HListRef<'a, Tuple = Self>; + + fn hlist(&'a self) -> Self::HList; +} + impl HList for () { type Tuple = (); @@ -24,6 +36,24 @@ impl HList for () { fn flatten(self) -> Self::Tuple {} } +impl<'a> HListRef<'a> for &'a () { + type Tuple = &'a (); + + #[inline] + fn flatten(&'a self) -> Self::Tuple { + &() + } +} + +impl<'a, T1> HListRef<'a> for HCons<&'a T1, &'a ()> { + type Tuple = (&'a T1,); + + #[inline] + fn flatten(&'a self) -> Self::Tuple { + (&self.head,) + } +} + impl Tuple for () { type HList = (); @@ -31,6 +61,27 @@ impl Tuple for () { fn hlist(self) -> Self::HList {} } +impl<'a> TupleRef<'a> for &'a () { + type HList = &'a (); + + #[inline] + fn hlist(&'a self) -> Self::HList { + &() + } +} + +impl<'a, T1> TupleRef<'a> for (&'a T1,) { + type HList = HCons<&'a T1, &'a ()>; + + #[inline] + fn hlist(&'a self) -> Self::HList { + HCons { + head: self.0, + tail: &(), + } + } +} + macro_rules! Product { () => { () }; ($head:ident $(,$tail:ident)* $(,)?) => { @@ -99,7 +150,11 @@ pub trait Plucker { fn pluck(self) -> (Target, Self::Remainder); } -struct Here {} +pub trait PluckerRef { + fn pluck(&self) -> &Target; +} + +pub struct Here(); impl Plucker for HCons { type Remainder = Tail; @@ -110,7 +165,14 @@ impl Plucker for HCons { } } -struct There(PhantomData); +impl<'a, Target, Tail> PluckerRef for &'a HCons<&'a Target, &'a Tail> { + #[inline] + fn pluck(&self) -> &Target { + self.head + } +} + +pub struct There(PhantomData); impl Plucker> for HCons where @@ -130,4 +192,15 @@ where }, ) } -} \ No newline at end of file +} + +impl<'a, Target, Head, Tail, TailIndex> PluckerRef> for &'a HCons<&'a Head, &'a Tail> +where + &'a Tail: PluckerRef, +{ + #[inline] + fn pluck(&self) -> &Target { + <&'a Tail as PluckerRef>::pluck(&self.tail) + } +} + diff --git a/quill/api/src/lib.rs b/quill/api/src/lib.rs index 597790dd6..4c293ceb3 100644 --- a/quill/api/src/lib.rs +++ b/quill/api/src/lib.rs @@ -7,7 +7,7 @@ mod game; pub mod query; pub mod hlist; mod setup; -mod send_message; +pub mod send_message; pub use entity::{Entity, EntityId}; pub use entity_builder::EntityBuilder; diff --git a/quill/api/src/send_message.rs b/quill/api/src/send_message.rs index c045732b2..8eae7dc2c 100644 --- a/quill/api/src/send_message.rs +++ b/quill/api/src/send_message.rs @@ -1,18 +1,18 @@ use quill_common::entities::Player; -use crate::{Entity, Plucker, Tuple}; +use crate::{Entity, PluckerRef, TupleRef}; -pub trait SendMessage { - fn send_message(&self, message: &str); +pub trait SendMessage<'a, Index> { + fn send_message(&'a self, message: &str); } -impl SendMessage for (Entity, T) +impl<'a, T, Index> SendMessage<'a, Index> for (&'a Entity, &'a T) where - T: Tuple, - ::HList: Plucker + &'a T: TupleRef<'a>, + <&'a T as TupleRef<'a>>::HList: PluckerRef { - fn send_message(&self, message: &str) { - let components = &self.1; - let player = Plucker::::pluck(&components.hlist()); + fn send_message(&'a self, message: &str) { + let foo = self.1.hlist(); + let player = PluckerRef::::pluck(&foo); } } \ No newline at end of file diff --git a/quill/example-plugins/query-entities/src/lib.rs b/quill/example-plugins/query-entities/src/lib.rs index c5851b0af..f6554a655 100644 --- a/quill/example-plugins/query-entities/src/lib.rs +++ b/quill/example-plugins/query-entities/src/lib.rs @@ -1,7 +1,7 @@ //! An example plugin that spawns 10,000 entities //! on startup, then moves them each tick using a query. -use quill::{entities::PiglinBrute, EntityInit, Game, Plugin, Position}; +use quill::{EntityInit, Game, Plugin, Position, Tuple, TupleRef, entities::{PiglinBrute, Player}, send_message::SendMessage}; use rand::Rng; quill::plugin!(QueryEntities); @@ -47,4 +47,10 @@ fn query_system(plugin: &mut QueryEntities, game: &mut Game) { ..position }); } + + for (entity, (player,)) in game.query::<(&Player,)>() { + TupleRef::hlist(&(&player,)); + (&entity, &(&player,)).send_message("Hello world!"); + // (&player).send_message("Hello world!"); + } } From 16e7fd1565f7ced19ce2c33bd5dbae12c123d73f Mon Sep 17 00:00:00 2001 From: = Date: Sun, 7 Mar 2021 12:01:58 +0100 Subject: [PATCH 05/10] it works --- quill/api/src/hlist.rs | 88 ++++++++++--------- quill/api/src/send_message.rs | 6 +- .../example-plugins/query-entities/src/lib.rs | 7 +- 3 files changed, 52 insertions(+), 49 deletions(-) diff --git a/quill/api/src/hlist.rs b/quill/api/src/hlist.rs index 3ec7ac994..68406ec2b 100644 --- a/quill/api/src/hlist.rs +++ b/quill/api/src/hlist.rs @@ -36,21 +36,12 @@ impl HList for () { fn flatten(self) -> Self::Tuple {} } -impl<'a> HListRef<'a> for &'a () { - type Tuple = &'a (); - - #[inline] - fn flatten(&'a self) -> Self::Tuple { - &() - } -} - -impl<'a, T1> HListRef<'a> for HCons<&'a T1, &'a ()> { - type Tuple = (&'a T1,); +impl<'a> HListRef<'a> for () { + type Tuple = (); #[inline] fn flatten(&'a self) -> Self::Tuple { - (&self.head,) + () } } @@ -61,46 +52,56 @@ impl Tuple for () { fn hlist(self) -> Self::HList {} } -impl<'a> TupleRef<'a> for &'a () { - type HList = &'a (); - - #[inline] - fn hlist(&'a self) -> Self::HList { - &() - } -} - -impl<'a, T1> TupleRef<'a> for (&'a T1,) { - type HList = HCons<&'a T1, &'a ()>; +impl<'a> TupleRef<'a> for () { + type HList = (); #[inline] fn hlist(&'a self) -> Self::HList { - HCons { - head: self.0, - tail: &(), - } + () } } -macro_rules! Product { +macro_rules! HList { () => { () }; - ($head:ident $(,$tail:ident)* $(,)?) => { - HCons<$head, Product!($($tail),*)> + ($head:ty $(,$tail:ty)* $(,)?) => { + HCons<$head, HList!($($tail),*)> }; } -macro_rules! product_pat { +macro_rules! hlist_pat { () => { () }; ($head:ident $(,$tail:ident)* $(,)?) => { HCons { head: $head, - tail: product_pat!($($tail),*) + tail: hlist_pat!($($tail),*) } }; } macro_rules! impl_tuple { ($head:ident $(,$tail:ident)* $(,)?) => { + impl<$head $(,$tail)*> HList for HList!($head $(,$tail)*) { + type Tuple = ($head, $($tail),*); + + #[inline] + fn flatten(self) -> Self::Tuple { + #[allow(non_snake_case)] + let hlist_pat!($head, $($tail),*) = self; + ($head, $($tail),*) + } + } + + impl<'a, $head $(,$tail)*> HListRef<'a> for HList!(&'a $head $(,&'a $tail)*) { + type Tuple = (&'a $head, $(&'a $tail),*); + + #[inline] + fn flatten(&'a self) -> Self::Tuple { + #[allow(non_snake_case)] + let hlist_pat!($head, $($tail),*) = self; + ($head, $($tail),*) + } + } + impl<$head $(,$tail)*> Tuple for ($head, $($tail),*) { type HList = HCons<$head, <($($tail,)*) as Tuple>::HList>; @@ -115,14 +116,17 @@ macro_rules! impl_tuple { } } - impl<$head $(,$tail)*> HList for Product!($head $(,$tail)*) { - type Tuple = ($head, $($tail),*); - + impl<'a, $head $(,$tail)*> TupleRef<'a> for (&'a $head, $(&'a $tail),*) { + type HList = HCons<&'a $head, <($(&'a $tail,)*) as Tuple>::HList>; + #[inline] - fn flatten(self) -> Self::Tuple { + fn hlist(&'a self) -> Self::HList { #[allow(non_snake_case)] - let product_pat!($head, $($tail),*) = self; - ($head, $($tail),*) + let ($head, $($tail),*) = self; + HCons { + head: $head, + tail: ($(*$tail,)*).hlist(), + } } } }; @@ -165,7 +169,7 @@ impl Plucker for HCons { } } -impl<'a, Target, Tail> PluckerRef for &'a HCons<&'a Target, &'a Tail> { +impl<'a, Target, Tail> PluckerRef for HCons<&'a Target, Tail> { #[inline] fn pluck(&self) -> &Target { self.head @@ -194,13 +198,13 @@ where } } -impl<'a, Target, Head, Tail, TailIndex> PluckerRef> for &'a HCons<&'a Head, &'a Tail> +impl<'a, Target, Head, Tail, TailIndex> PluckerRef> for HCons<&'a Head, Tail> where - &'a Tail: PluckerRef, + Tail: PluckerRef, { #[inline] fn pluck(&self) -> &Target { - <&'a Tail as PluckerRef>::pluck(&self.tail) + >::pluck(&self.tail) } } diff --git a/quill/api/src/send_message.rs b/quill/api/src/send_message.rs index 8eae7dc2c..7772670fa 100644 --- a/quill/api/src/send_message.rs +++ b/quill/api/src/send_message.rs @@ -6,10 +6,10 @@ pub trait SendMessage<'a, Index> { fn send_message(&'a self, message: &str); } -impl<'a, T, Index> SendMessage<'a, Index> for (&'a Entity, &'a T) +impl<'a, T, Index> SendMessage<'a, Index> for (&'a Entity, T) where - &'a T: TupleRef<'a>, - <&'a T as TupleRef<'a>>::HList: PluckerRef + T: TupleRef<'a>, + >::HList: PluckerRef { fn send_message(&'a self, message: &str) { let foo = self.1.hlist(); diff --git a/quill/example-plugins/query-entities/src/lib.rs b/quill/example-plugins/query-entities/src/lib.rs index f6554a655..6c44c8af8 100644 --- a/quill/example-plugins/query-entities/src/lib.rs +++ b/quill/example-plugins/query-entities/src/lib.rs @@ -1,7 +1,7 @@ //! An example plugin that spawns 10,000 entities //! on startup, then moves them each tick using a query. -use quill::{EntityInit, Game, Plugin, Position, Tuple, TupleRef, entities::{PiglinBrute, Player}, send_message::SendMessage}; +use quill::{EntityInit, Game, Plugin, Position, entities::{PiglinBrute, Player}, send_message::SendMessage}; use rand::Rng; quill::plugin!(QueryEntities); @@ -49,8 +49,7 @@ fn query_system(plugin: &mut QueryEntities, game: &mut Game) { } for (entity, (player,)) in game.query::<(&Player,)>() { - TupleRef::hlist(&(&player,)); - (&entity, &(&player,)).send_message("Hello world!"); - // (&player).send_message("Hello world!"); + (&entity, (&player,)).send_message("Hello world!"); + (&entity, (&player,)).send_message("Hello world!"); } } From 69967a365b0c2d8e5bc322ef98870871cb3baf6d Mon Sep 17 00:00:00 2001 From: = Date: Sun, 7 Mar 2021 12:02:34 +0100 Subject: [PATCH 06/10] cargo fmt && cargo clippy --- quill/api/src/hlist.rs | 11 +++++------ quill/api/src/lib.rs | 4 ++-- quill/api/src/send_message.rs | 4 ++-- quill/example-plugins/query-entities/src/lib.rs | 10 +++++++--- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/quill/api/src/hlist.rs b/quill/api/src/hlist.rs index 68406ec2b..ffbf50b49 100644 --- a/quill/api/src/hlist.rs +++ b/quill/api/src/hlist.rs @@ -72,7 +72,7 @@ macro_rules! hlist_pat { () => { () }; ($head:ident $(,$tail:ident)* $(,)?) => { HCons { - head: $head, + head: $head, tail: hlist_pat!($($tail),*) } }; @@ -93,7 +93,7 @@ macro_rules! impl_tuple { impl<'a, $head $(,$tail)*> HListRef<'a> for HList!(&'a $head $(,&'a $tail)*) { type Tuple = (&'a $head, $(&'a $tail),*); - + #[inline] fn flatten(&'a self) -> Self::Tuple { #[allow(non_snake_case)] @@ -118,7 +118,7 @@ macro_rules! impl_tuple { impl<'a, $head $(,$tail)*> TupleRef<'a> for (&'a $head, $(&'a $tail),*) { type HList = HCons<&'a $head, <($(&'a $tail,)*) as Tuple>::HList>; - + #[inline] fn hlist(&'a self) -> Self::HList { #[allow(non_snake_case)] @@ -147,7 +147,6 @@ smaller_tuples_too!( T19, T20 ); - pub trait Plucker { type Remainder; @@ -198,7 +197,8 @@ where } } -impl<'a, Target, Head, Tail, TailIndex> PluckerRef> for HCons<&'a Head, Tail> +impl<'a, Target, Head, Tail, TailIndex> PluckerRef> + for HCons<&'a Head, Tail> where Tail: PluckerRef, { @@ -207,4 +207,3 @@ where >::pluck(&self.tail) } } - diff --git a/quill/api/src/lib.rs b/quill/api/src/lib.rs index 4c293ceb3..3b6d43727 100644 --- a/quill/api/src/lib.rs +++ b/quill/api/src/lib.rs @@ -4,10 +4,10 @@ pub mod entities; mod entity; mod entity_builder; mod game; -pub mod query; pub mod hlist; -mod setup; +pub mod query; pub mod send_message; +mod setup; pub use entity::{Entity, EntityId}; pub use entity_builder::EntityBuilder; diff --git a/quill/api/src/send_message.rs b/quill/api/src/send_message.rs index 7772670fa..33ab68412 100644 --- a/quill/api/src/send_message.rs +++ b/quill/api/src/send_message.rs @@ -9,10 +9,10 @@ pub trait SendMessage<'a, Index> { impl<'a, T, Index> SendMessage<'a, Index> for (&'a Entity, T) where T: TupleRef<'a>, - >::HList: PluckerRef + >::HList: PluckerRef, { fn send_message(&'a self, message: &str) { let foo = self.1.hlist(); let player = PluckerRef::::pluck(&foo); } -} \ No newline at end of file +} diff --git a/quill/example-plugins/query-entities/src/lib.rs b/quill/example-plugins/query-entities/src/lib.rs index 6c44c8af8..68331b312 100644 --- a/quill/example-plugins/query-entities/src/lib.rs +++ b/quill/example-plugins/query-entities/src/lib.rs @@ -1,7 +1,11 @@ //! An example plugin that spawns 10,000 entities //! on startup, then moves them each tick using a query. -use quill::{EntityInit, Game, Plugin, Position, entities::{PiglinBrute, Player}, send_message::SendMessage}; +use quill::{ + entities::{PiglinBrute, Player}, + send_message::SendMessage, + EntityInit, Game, Plugin, Position, +}; use rand::Rng; quill::plugin!(QueryEntities); @@ -47,9 +51,9 @@ fn query_system(plugin: &mut QueryEntities, game: &mut Game) { ..position }); } - + for (entity, (player,)) in game.query::<(&Player,)>() { (&entity, (&player,)).send_message("Hello world!"); - (&entity, (&player,)).send_message("Hello world!"); + (&entity, (&player,)).send_message("Hello world!"); } } From 33d8a8a6aa3d5a9646ff1c2a0753cb4832d89ba6 Mon Sep 17 00:00:00 2001 From: = Date: Sun, 7 Mar 2021 12:03:31 +0100 Subject: [PATCH 07/10] prefixed _ on unsued variables --- quill/api/src/send_message.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/quill/api/src/send_message.rs b/quill/api/src/send_message.rs index 33ab68412..51d0d1ef3 100644 --- a/quill/api/src/send_message.rs +++ b/quill/api/src/send_message.rs @@ -11,8 +11,8 @@ where T: TupleRef<'a>, >::HList: PluckerRef, { - fn send_message(&'a self, message: &str) { + fn send_message(&'a self, _message: &str) { let foo = self.1.hlist(); - let player = PluckerRef::::pluck(&foo); + let _player: &Player = PluckerRef::::pluck(&foo); } } From 835a657d7df4123f66a50b89e20a364c2c89e6e7 Mon Sep 17 00:00:00 2001 From: = Date: Sun, 7 Mar 2021 12:18:11 +0100 Subject: [PATCH 08/10] removed unnecessary unit --- quill/api/src/hlist.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/quill/api/src/hlist.rs b/quill/api/src/hlist.rs index ffbf50b49..eb0966d9b 100644 --- a/quill/api/src/hlist.rs +++ b/quill/api/src/hlist.rs @@ -40,9 +40,7 @@ impl<'a> HListRef<'a> for () { type Tuple = (); #[inline] - fn flatten(&'a self) -> Self::Tuple { - () - } + fn flatten(&'a self) -> Self::Tuple {} } impl Tuple for () { @@ -56,9 +54,7 @@ impl<'a> TupleRef<'a> for () { type HList = (); #[inline] - fn hlist(&'a self) -> Self::HList { - () - } + fn hlist(&'a self) -> Self::HList {} } macro_rules! HList { From 944a64927ef71af824be4653fe004fdb50a0d3c2 Mon Sep 17 00:00:00 2001 From: = Date: Sun, 7 Mar 2021 12:30:35 +0100 Subject: [PATCH 09/10] local clippy being annnoying --- quill/api/src/send_message.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/quill/api/src/send_message.rs b/quill/api/src/send_message.rs index 51d0d1ef3..82dad4947 100644 --- a/quill/api/src/send_message.rs +++ b/quill/api/src/send_message.rs @@ -12,7 +12,6 @@ where >::HList: PluckerRef, { fn send_message(&'a self, _message: &str) { - let foo = self.1.hlist(); - let _player: &Player = PluckerRef::::pluck(&foo); + let _player: &Player = PluckerRef::::pluck(&self.1.hlist()); } } From 6ac28954a97f4934fe89f659ebc4c7d4d303495c Mon Sep 17 00:00:00 2001 From: = Date: Sun, 7 Mar 2021 22:57:10 +0100 Subject: [PATCH 10/10] got rid of TupleRef --- quill/api/src/hlist.rs | 79 ++++++------------- quill/api/src/send_message.rs | 16 ++-- .../example-plugins/query-entities/src/lib.rs | 3 +- 3 files changed, 33 insertions(+), 65 deletions(-) diff --git a/quill/api/src/hlist.rs b/quill/api/src/hlist.rs index eb0966d9b..49bda1fc3 100644 --- a/quill/api/src/hlist.rs +++ b/quill/api/src/hlist.rs @@ -11,24 +11,12 @@ pub trait HList: Sized { fn flatten(self) -> Self::Tuple; } -pub trait HListRef<'a>: Sized { - type Tuple: TupleRef<'a, HList = Self>; - - fn flatten(&'a self) -> Self::Tuple; -} - pub trait Tuple: Sized { type HList: HList; fn hlist(self) -> Self::HList; } -pub trait TupleRef<'a>: Sized { - type HList: HListRef<'a, Tuple = Self>; - - fn hlist(&'a self) -> Self::HList; -} - impl HList for () { type Tuple = (); @@ -36,13 +24,6 @@ impl HList for () { fn flatten(self) -> Self::Tuple {} } -impl<'a> HListRef<'a> for () { - type Tuple = (); - - #[inline] - fn flatten(&'a self) -> Self::Tuple {} -} - impl Tuple for () { type HList = (); @@ -50,13 +31,6 @@ impl Tuple for () { fn hlist(self) -> Self::HList {} } -impl<'a> TupleRef<'a> for () { - type HList = (); - - #[inline] - fn hlist(&'a self) -> Self::HList {} -} - macro_rules! HList { () => { () }; ($head:ty $(,$tail:ty)* $(,)?) => { @@ -87,17 +61,6 @@ macro_rules! impl_tuple { } } - impl<'a, $head $(,$tail)*> HListRef<'a> for HList!(&'a $head $(,&'a $tail)*) { - type Tuple = (&'a $head, $(&'a $tail),*); - - #[inline] - fn flatten(&'a self) -> Self::Tuple { - #[allow(non_snake_case)] - let hlist_pat!($head, $($tail),*) = self; - ($head, $($tail),*) - } - } - impl<$head $(,$tail)*> Tuple for ($head, $($tail),*) { type HList = HCons<$head, <($($tail,)*) as Tuple>::HList>; @@ -111,20 +74,6 @@ macro_rules! impl_tuple { } } } - - impl<'a, $head $(,$tail)*> TupleRef<'a> for (&'a $head, $(&'a $tail),*) { - type HList = HCons<&'a $head, <($(&'a $tail,)*) as Tuple>::HList>; - - #[inline] - fn hlist(&'a self) -> Self::HList { - #[allow(non_snake_case)] - let ($head, $($tail),*) = self; - HCons { - head: $head, - tail: ($(*$tail,)*).hlist(), - } - } - } }; } @@ -153,6 +102,10 @@ pub trait PluckerRef { fn pluck(&self) -> &Target; } +pub trait PluckerMut { + fn pluck(&mut self) -> &mut Target; +} + pub struct Here(); impl Plucker for HCons { @@ -164,10 +117,17 @@ impl Plucker for HCons { } } -impl<'a, Target, Tail> PluckerRef for HCons<&'a Target, Tail> { +impl PluckerRef for HCons<&'_ Target, Tail> { #[inline] fn pluck(&self) -> &Target { - self.head + &self.head + } +} + +impl PluckerMut for HCons<&'_ mut Target, Tail> { + #[inline] + fn pluck(&mut self) -> &mut Target { + &mut self.head } } @@ -193,8 +153,7 @@ where } } -impl<'a, Target, Head, Tail, TailIndex> PluckerRef> - for HCons<&'a Head, Tail> +impl PluckerRef> for HCons where Tail: PluckerRef, { @@ -203,3 +162,13 @@ where >::pluck(&self.tail) } } + +impl PluckerMut> for HCons +where + Tail: PluckerMut, +{ + #[inline] + fn pluck(&mut self) -> &mut Target { + >::pluck(&mut self.tail) + } +} diff --git a/quill/api/src/send_message.rs b/quill/api/src/send_message.rs index 82dad4947..43cc809c3 100644 --- a/quill/api/src/send_message.rs +++ b/quill/api/src/send_message.rs @@ -1,17 +1,17 @@ use quill_common::entities::Player; -use crate::{Entity, PluckerRef, TupleRef}; +use crate::{Entity, PluckerRef, Tuple}; -pub trait SendMessage<'a, Index> { - fn send_message(&'a self, message: &str); +pub trait SendMessage { + fn send_message(&self, message: &str); } -impl<'a, T, Index> SendMessage<'a, Index> for (&'a Entity, T) +impl SendMessage for (&'_ Entity, T) where - T: TupleRef<'a>, - >::HList: PluckerRef, + T: Tuple, + ::HList: PluckerRef, { - fn send_message(&'a self, _message: &str) { - let _player: &Player = PluckerRef::::pluck(&self.1.hlist()); + fn send_message(&self, _message: &str) { + // let _player: &Player = PluckerRef::::pluck(self.1.hlist()); } } diff --git a/quill/example-plugins/query-entities/src/lib.rs b/quill/example-plugins/query-entities/src/lib.rs index 68331b312..791d137e4 100644 --- a/quill/example-plugins/query-entities/src/lib.rs +++ b/quill/example-plugins/query-entities/src/lib.rs @@ -53,7 +53,6 @@ fn query_system(plugin: &mut QueryEntities, game: &mut Game) { } for (entity, (player,)) in game.query::<(&Player,)>() { - (&entity, (&player,)).send_message("Hello world!"); - (&entity, (&player,)).send_message("Hello world!"); + (&entity, (&player,)).send_message("foo"); } }