Skip to content

HList and Plucker for components #388

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

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
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
174 changes: 174 additions & 0 deletions quill/api/src/hlist.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
use std::marker::PhantomData;

pub struct HCons<Head, Tail> {
pub head: Head,
pub tail: Tail,
}

pub trait HList: Sized {
type Tuple: Tuple<HList = Self>;

fn flatten(self) -> Self::Tuple;
}

pub trait Tuple: Sized {
type HList: HList<Tuple = Self>;

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! HList {
() => { () };
($head:ty $(,$tail:ty)* $(,)?) => {
HCons<$head, HList!($($tail),*)>
};
}

macro_rules! hlist_pat {
() => { () };
($head:ident $(,$tail:ident)* $(,)?) => {
HCons {
head: $head,
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<$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(),
}
}
}
};
}

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<Target, Index> {
type Remainder;

fn pluck(self) -> (Target, Self::Remainder);
}

pub trait PluckerRef<Target, Index> {
fn pluck(&self) -> &Target;
}

pub trait PluckerMut<Target, Index> {
fn pluck(&mut self) -> &mut Target;
}

pub struct Here();

impl<Target, Tail> Plucker<Target, Here> for HCons<Target, Tail> {
type Remainder = Tail;

#[inline]
fn pluck(self) -> (Target, Self::Remainder) {
(self.head, self.tail)
}
}

impl<Target, Tail> PluckerRef<Target, Here> for HCons<&'_ Target, Tail> {
#[inline]
fn pluck(&self) -> &Target {
&self.head
}
}

impl<Target, Tail> PluckerMut<Target, Here> for HCons<&'_ mut Target, Tail> {
#[inline]
fn pluck(&mut self) -> &mut Target {
&mut self.head
}
}

pub struct There<T>(PhantomData<T>);

impl<Target, Head, Tail, TailIndex> Plucker<Target, There<TailIndex>> for HCons<Head, Tail>
where
Tail: Plucker<Target, TailIndex>,
{
type Remainder = HCons<Head, <Tail as Plucker<Target, TailIndex>>::Remainder>;

#[inline]
fn pluck(self) -> (Target, Self::Remainder) {
let (target, tail_remainder): (Target, <Tail as Plucker<Target, TailIndex>>::Remainder) =
<Tail as Plucker<Target, TailIndex>>::pluck(self.tail);
(
target,
HCons {
head: self.head,
tail: tail_remainder,
},
)
}
}

impl<Target, Head, Tail, TailIndex> PluckerRef<Target, There<TailIndex>> for HCons<Head, Tail>
where
Tail: PluckerRef<Target, TailIndex>,
{
#[inline]
fn pluck(&self) -> &Target {
<Tail as PluckerRef<Target, TailIndex>>::pluck(&self.tail)
}
}

impl<Target, Head, Tail, TailIndex> PluckerMut<Target, There<TailIndex>> for HCons<Head, Tail>
where
Tail: PluckerMut<Target, TailIndex>,
{
#[inline]
fn pluck(&mut self) -> &mut Target {
<Tail as PluckerMut<Target, TailIndex>>::pluck(&mut self.tail)
}
}
3 changes: 3 additions & 0 deletions quill/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ pub mod entities;
mod entity;
mod entity_builder;
mod game;
pub mod hlist;
pub mod query;
pub mod send_message;
mod setup;

pub use entity::{Entity, EntityId};
pub use entity_builder::EntityBuilder;
pub use game::Game;
pub use hlist::*;
pub use setup::Setup;

#[doc(inline)]
Expand Down
16 changes: 6 additions & 10 deletions quill/api/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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<HostComponent>) {
$(
$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);
Expand Down Expand Up @@ -137,6 +132,7 @@ where
impl<Q> Iterator for QueryIter<Q>
where
Q: Query,
<Q as Query>::Item: Tuple,
{
type Item = (Entity, Q::Item);

Expand Down
17 changes: 17 additions & 0 deletions quill/api/src/send_message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use quill_common::entities::Player;

use crate::{Entity, PluckerRef, Tuple};

pub trait SendMessage<Index> {
fn send_message(&self, message: &str);
}

impl<T, Index> SendMessage<Index> for (&'_ Entity, T)
where
T: Tuple,
<T as Tuple>::HList: PluckerRef<Player, Index>,
{
fn send_message(&self, _message: &str) {
// let _player: &Player = PluckerRef::<Player, Index>::pluck(self.1.hlist());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not what the lifetimes should be here, since right now Tuple is only implemented for the owned T and not &T.

}
}
2 changes: 1 addition & 1 deletion quill/example-plugins/block-place/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}
}
10 changes: 9 additions & 1 deletion quill/example-plugins/query-entities/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
//! 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::{
entities::{PiglinBrute, Player},
send_message::SendMessage,
EntityInit, Game, Plugin, Position,
};
use rand::Rng;

quill::plugin!(QueryEntities);
Expand Down Expand Up @@ -47,4 +51,8 @@ fn query_system(plugin: &mut QueryEntities, game: &mut Game) {
..position
});
}

for (entity, (player,)) in game.query::<(&Player,)>() {
(&entity, (&player,)).send_message("foo");
}
}