Skip to content
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

Add attributes to gfx_defines macros #1080

Merged
merged 2 commits into from
Nov 25, 2016
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
35 changes: 24 additions & 11 deletions src/render/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ macro_rules! gfx_format {
/// Defines vertex, constant and pipeline formats in one block
#[macro_export]
macro_rules! gfx_defines {
(vertex $name:ident {
($(#[$attr:meta])* vertex $name:ident {
$( $field:ident : $ty:ty = $e:expr, )+
}) => {
gfx_vertex_struct!($name {$($field:$ty = $e,)+});
gfx_vertex_struct_meta!($(#[$attr])* vertex_struct_meta $name {$($field:$ty = $e,)+});
};

(constant $name:ident {
($(#[$attr:meta])* constant $name:ident {
$( $field:ident : $ty:ty = $e:expr, )+
}) => {
gfx_constant_struct!($name {$($field:$ty = $e,)+});
gfx_constant_struct_meta!($(#[$attr])* constant_struct_meta $name {$($field:$ty = $e,)+});
};

(pipeline $name:ident {
Expand All @@ -52,22 +52,35 @@ macro_rules! gfx_defines {
gfx_pipeline!($name {$($field:$ty = $e,)+});
};

($keyword:ident $name:ident {
// The recursive case for vertex structs
($(#[$attr:meta])* vertex $name:ident {
Copy link
Member

Choose a reason for hiding this comment

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

is this ever going to hit, given that the same pattern is parsed above?

Copy link
Author

Choose a reason for hiding this comment

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

This pattern contains $($tail:tt)+, it's the recursive case. The above pattern only matches for a single struct.

$( $field:ident : $ty:ty = $e:expr, )+
} $($tail:tt)+) => {
gfx_defines! {
$keyword $name { $($field : $ty = $e,)+ }
$(#[$attr])*
vertex $name { $($field : $ty = $e,)+ }
}
gfx_defines!($($tail)+);
};

// The recursive case for constant structs
($(#[$attr:meta])* constant $name:ident {
$( $field:ident : $ty:ty = $e:expr, )+
} $($tail:tt)+) => {
gfx_defines! {
$(#[$attr])*
constant $name { $($field : $ty = $e,)+ }
}
gfx_defines!($($tail)+);
};

// The recursive case for the other keywords
($keyword:ident $name:ident {
$( $field:ident : $ty:ty = $e:expr ),*
}) => {
$( $field:ident : $ty:ty = $e:expr, )+
} $($tail:tt)+) => {
gfx_defines! {
$keyword $name {
$($field : $ty = $e ,)*
}
$keyword $name { $($field : $ty = $e,)+ }
}
gfx_defines!($($tail)+);
};
}
40 changes: 38 additions & 2 deletions src/render/src/macros/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,20 @@
macro_rules! gfx_impl_struct {
($runtime_format:ty : $compile_format:path = $root:ident {
$( $field:ident: $ty:ty = $name:expr, )*
}) => (gfx_impl_struct_meta! {
impl_struct_meta $runtime_format : $compile_format = $root {
$( $field : $ty = $name, )*
}
})
}

#[macro_export]
macro_rules! gfx_impl_struct_meta {
Copy link
Member

Choose a reason for hiding this comment

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

any reason you decided to not just put the extra stuff into gfx_impl_struct instead of routing through the new gfx_impl_struct_meta?

Copy link
Author

Choose a reason for hiding this comment

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

Backwards compatibility in case anybody called gfx_impl_struct directly.

Copy link
Author

Choose a reason for hiding this comment

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

I could probably have two patterns inside gfx_impl_struct, one with $(#[$attr:meta])* impl_struct_meta and one without, but then I believe I'd have to duplicate the whole body, which is worse to me than having an extra name.

($(#[$attr:meta])* impl_struct_meta $runtime_format:ty : $compile_format:path = $root:ident {
$( $field:ident: $ty:ty = $name:expr, )*
}) => {
#[derive(Clone, Copy, Debug)]
$(#[$attr])*
pub struct $root {
$( pub $field: $ty, )*
}
Expand Down Expand Up @@ -66,7 +78,19 @@ macro_rules! gfx_impl_struct {
macro_rules! gfx_vertex_struct {
($root:ident {
$( $field:ident: $ty:ty = $name:expr, )*
}) => (gfx_impl_struct!{
}) => (gfx_vertex_struct_meta! {
vertex_struct_meta $root {
$( $field : $ty = $name, )*
}
})
}

#[macro_export]
macro_rules! gfx_vertex_struct_meta {
($(#[$attr:meta])* vertex_struct_meta $root:ident {
$( $field:ident: $ty:ty = $name:expr, )*
}) => (gfx_impl_struct_meta!{
$(#[$attr])* impl_struct_meta
$crate::format::Format : $crate::format::Formatted =
$root {
$( $field: $ty = $name, )*
Expand All @@ -78,7 +102,19 @@ macro_rules! gfx_vertex_struct {
macro_rules! gfx_constant_struct {
($root:ident {
$( $field:ident: $ty:ty = $name:expr, )*
}) => (gfx_impl_struct!{
}) => (gfx_constant_struct_meta!{
constant_struct_meta $root {
$( $field : $ty = $name, )*
}
})
}

#[macro_export]
macro_rules! gfx_constant_struct_meta {
($(#[$attr:meta])* constant_struct_meta $root:ident {
$( $field:ident: $ty:ty = $name:expr, )*
}) => (gfx_impl_struct_meta!{
$(#[$attr])* impl_struct_meta
$crate::shade::ConstFormat : $crate::shade::Formatted =
$root {
$( $field: $ty = $name, )*
Expand Down
5 changes: 5 additions & 0 deletions tests/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub struct Rg16;
gfx_format!(Rg16: R16_G16 = Vec2<Float>);

gfx_defines!{
#[derive(PartialEq)]
vertex Vertex {
_x: i8 = "x",
_y: f32 = "y",
Expand All @@ -20,6 +21,10 @@ gfx_defines!{
constant Local {
pos: [u32; 4] = "pos",
}
#[derive(PartialEq)] #[derive(PartialOrd)]
constant LocalMeta {
pos: [u32; 4] = "pos_meta",
}

pipeline testpipe {
vertex: gfx::VertexBuffer<Vertex> = (),
Expand Down