-
Notifications
You must be signed in to change notification settings - Fork 144
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
Defman
wants to merge
10
commits into
feather-rs:main
Choose a base branch
from
Defman:RequireComponents
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
032f126
I need help
Defman b5dbaaa
...
Defman 8f65247
...
Defman e3f08d4
maybe?
Defman 16e7fd1
it works
Defman 69967a3
cargo fmt && cargo clippy
Defman 33d8a8a
prefixed _ on unsued variables
Defman 835a657
removed unnecessary unit
Defman 944a649
local clippy being annnoying
Defman 6ac2895
got rid of TupleRef
Defman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ownedT
and not&T
.