Skip to content
Merged
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
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 30 additions & 2 deletions components/src/channels.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
pub trait Channel<T>: std::marker::Copy {
fn set(&self, value: T) -> Self;
use std::marker::Copy;

pub trait Channel<T>: Copy {
fn get(&self) -> T;
fn set(&self, value: T) -> Self;
}

pub trait DuelChannel<A, B>: Copy + Channel<A> + Channel<B> {
fn duel_get(&self) -> A {
self.get()
}
fn duel_set(&self, value: B) -> Self {
self.set(value)
}
}

#[cfg(test)]
Expand All @@ -22,6 +33,21 @@ mod tests {
assert_eq!(channel.event, new_event);
}

#[test]
fn test_duel_get() {
let channel = get_test_channel();
let event: Event = channel.duel_get();
assert_eq!(event, channel.event);
}

#[test]
fn test_duel_set() {
let channel = get_test_channel();
let new_event = Event::Exit;
let channel = channel.duel_set(new_event);
assert_eq!(channel.event, new_event);
}

fn get_test_channel() -> EventChannel {
EventChannel {
event: Event::Keyboard,
Expand All @@ -44,6 +70,8 @@ mod tests {
}
}

impl DuelChannel<Event, Event> for EventChannel {}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Event {
Keyboard,
Expand Down
1 change: 1 addition & 0 deletions components/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod channels;
#[doc(hidden)]
pub mod __private {
pub use crate::channels::Channel;
pub use crate::channels::DuelChannel;
}

#[cfg(feature = "macros")]
Expand Down
16 changes: 16 additions & 0 deletions components/tests/macros.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use components::channels::Channel;
use components::channels::DuelChannel;
use components::Channels;

#[test]
Expand All @@ -16,6 +17,21 @@ fn test_derive_channels_set() {
assert_eq!(channel.position, new_position);
}

#[test]
fn test_derive_duel_channels_get() {
let channel = get_test_channel();
let color: Rgb = channel.duel_get();
assert_eq!(color, channel.color);
}

#[test]
fn test_derive_duel_channels_set() {
let channel = get_test_channel();
let new_position = (-5, 20);
let channel = channel.duel_set(new_position);
assert_eq!(channel.position, new_position);
}

fn get_test_channel() -> GraphicsChannel {
let color = Rgb {
red: 255,
Expand Down
3 changes: 2 additions & 1 deletion components_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ proc-macro = true
name = "components_macros"

[dependencies]
itertools = "0.14.0"
proc-macro2 = "1.0.101"
quote = "1.0.40"
syn = "2.0.106"
syn = { version = "2.0.106", features = ["extra-traits"] }
21 changes: 16 additions & 5 deletions components_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ extern crate proc_macro;
use proc_macro2::{TokenStream, Span};
use quote::quote;
use syn::{Data, DeriveInput};
use itertools::Itertools;

#[proc_macro_derive(Channels)]
pub fn channels(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
Expand All @@ -22,22 +23,32 @@ fn channels_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
_ => return Err(non_struct_error()),
};

let generated_code = fields.iter().map(|f| (f.clone().ident.expect("struct fields have names"), f.ty.clone())).map(|(field, ty)| {
let fields = fields.iter().map(|f| (f.clone().ident.expect("struct fields have names"), f.ty.clone()));
let channel_impls = fields.clone().map(|(field, ty)| {
quote! {
impl #impl_generics ::components::__private::Channel<#ty> for #name #ty_generics #where_clause {
fn get(&self) -> #ty {
self.#field
}
fn set(&self, value: #ty) -> Self {
let mut clone = *self;
clone.#field = value;
clone
}
fn get(&self) -> #ty {
self.#field
}
}
}
});
let duel_channel_impls = fields.clone().cartesian_product(fields)
.map(|(a, b)| (a.1, b.1))
.filter(|(a, b)| a != b)
.map(|(ty_a, ty_b)| {
quote! {
impl #impl_generics ::components::__private::DuelChannel<#ty_a, #ty_b> for #name #ty_generics #where_clause {}
}
});
Ok(quote! {
#(#generated_code)*
#(#channel_impls)*
#(#duel_channel_impls)*
})
}

Expand Down