summaryrefslogtreecommitdiff
path: root/src/codegen/mod.rs
diff options
context:
space:
mode:
authorbors-servo <lbergstrom+bors@mozilla.com>2018-04-07 08:56:20 -0400
committerGitHub <noreply@github.com>2018-04-07 08:56:20 -0400
commit0a601d6b3cd9fc32e119db2c33961d8f4db7e89e (patch)
treeb8b50e1e7cffeb1f77bf10ef2e2f9257ce60a90d /src/codegen/mod.rs
parent0b4f5be312cf4ccc5b382a612180ed58a4d10983 (diff)
parent7024cf682a6ff63bbafd63a6b570826ce422e2d4 (diff)
Auto merge of #1293 - strake:use_associated_consts, r=emilio
optionally use associated constants in bitfields See #1166 r? @emilio
Diffstat (limited to 'src/codegen/mod.rs')
-rw-r--r--src/codegen/mod.rs40
1 files changed, 26 insertions, 14 deletions
diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs
index 39babb6b..b30da19b 100644
--- a/src/codegen/mod.rs
+++ b/src/codegen/mod.rs
@@ -2126,7 +2126,7 @@ impl EnumVariation {
fn is_bitfield(&self) -> bool {
match *self {
- EnumVariation::Bitfield => true,
+ EnumVariation::Bitfield {..} => true,
_ => false
}
}
@@ -2247,6 +2247,7 @@ impl<'a> EnumBuilder<'a> {
mangling_prefix: Option<&str>,
rust_ty: quote::Tokens,
result: &mut CodegenResult<'b>,
+ is_ty_named: bool,
) -> Self {
let variant_name = ctx.rust_mangle(variant.name());
let expr = match variant.val() {
@@ -2278,19 +2279,28 @@ impl<'a> EnumBuilder<'a> {
}
}
- EnumBuilder::Bitfield { .. } => {
- let constant_name = match mangling_prefix {
- Some(prefix) => {
- Cow::Owned(format!("{}_{}", prefix, variant_name))
- }
- None => variant_name,
- };
-
- let ident = ctx.rust_ident(constant_name);
- result.push(quote! {
- #doc
- pub const #ident : #rust_ty = #rust_ty ( #expr );
- });
+ EnumBuilder::Bitfield { canonical_name, .. } => {
+ if ctx.options().rust_features().associated_const && is_ty_named {
+ let enum_ident = ctx.rust_ident(canonical_name);
+ let variant_ident = ctx.rust_ident(variant_name);
+ result.push(quote! {
+ impl #enum_ident {
+ #doc
+ pub const #variant_ident : #rust_ty = #rust_ty ( #expr );
+ }
+ });
+ } else {
+ let ident = ctx.rust_ident(match mangling_prefix {
+ Some(prefix) => {
+ Cow::Owned(format!("{}_{}", prefix, variant_name))
+ }
+ None => variant_name,
+ });
+ result.push(quote! {
+ #doc
+ pub const #ident : #rust_ty = #rust_ty ( #expr );
+ });
+ }
self
}
@@ -2625,6 +2635,7 @@ impl CodeGenerator for Enum {
constant_mangling_prefix,
enum_rust_ty.clone(),
result,
+ enum_ty.name().is_some(),
);
}
}
@@ -2635,6 +2646,7 @@ impl CodeGenerator for Enum {
constant_mangling_prefix,
enum_rust_ty.clone(),
result,
+ enum_ty.name().is_some(),
);
let variant_name = ctx.rust_ident(variant.name());