Skip to content
Open
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
21 changes: 11 additions & 10 deletions derive/src/derive_bit_packed.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::attribute::ContainerAttributes;
use crate::attribute::FieldAttributes;
use crate::stream_builder_ext::StreamBuilderExt;
use virtue::prelude::*;

pub(crate) struct DeriveBitPacked {
Expand Down Expand Up @@ -421,7 +422,7 @@ impl DeriveBitPackedEnum {
for (variant_index, variant) in self.variants.iter().enumerate() {
match_body.ident_str("Self");
match_body.puncts("::");
match_body.ident(variant.name.clone());
match_body.ident_ref(&variant.name);

if let Some(fields) = variant.fields.as_ref() {
let delimiter = fields.delimiter();
Expand Down Expand Up @@ -576,7 +577,7 @@ impl DeriveBitPackedEnum {
if idx != 0 {
allowed_slice.punct(',');
}
allowed_slice.ident(variant.name.clone());
allowed_slice.ident_ref(&variant.name);
}
Ok(())
})?;
Expand Down Expand Up @@ -723,7 +724,7 @@ impl DeriveBitPackedEnum {
arm_body.group(Delimiter::Parenthesis, |variant_case_body| {
variant_case_body.ident_str("Self");
variant_case_body.puncts("::");
variant_case_body.ident(variant.name.clone());
variant_case_body.ident_ref(&variant.name);

if let Some(fields) = variant.fields.as_ref() {
let is_tuple = matches!(fields, Fields::Tuple(_));
Expand All @@ -732,7 +733,7 @@ impl DeriveBitPackedEnum {
|variant_body| {
for field in fields.names() {
if !is_tuple {
variant_body.ident(field.unwrap_ident().clone());
variant_body.ident_ref(&field.unwrap_ident());
variant_body.punct(':');
}
variant_body.push_parsed(format!("__{},", field.to_string_with_prefix("field_")))?;
Expand Down Expand Up @@ -781,7 +782,7 @@ impl DeriveBitPackedEnum {
arm_body.group(Delimiter::Parenthesis, |variant_case_body| {
variant_case_body.ident_str("Self");
variant_case_body.puncts("::");
variant_case_body.ident(variant.name.clone());
variant_case_body.ident_ref(&variant.name);

if let Some(fields) = variant.fields.as_ref() {
let is_tuple = matches!(fields, Fields::Tuple(_));
Expand All @@ -790,7 +791,7 @@ impl DeriveBitPackedEnum {
|variant_body| {
for field in fields.names() {
if !is_tuple {
variant_body.ident(field.unwrap_ident().clone());
variant_body.ident_ref(&field.unwrap_ident());
variant_body.punct(':');
}
variant_body.push_parsed(format!("__{},", field.to_string_with_prefix("field_")))?;
Expand Down Expand Up @@ -946,7 +947,7 @@ impl DeriveBitPackedEnum {
arm_body.group(Delimiter::Parenthesis, |variant_case_body| {
variant_case_body.ident_str("Self");
variant_case_body.puncts("::");
variant_case_body.ident(variant.name.clone());
variant_case_body.ident_ref(&variant.name);

if let Some(fields) = variant.fields.as_ref() {
let is_tuple = matches!(fields, Fields::Tuple(_));
Expand All @@ -955,7 +956,7 @@ impl DeriveBitPackedEnum {
|variant_body| {
for field in fields.names() {
if !is_tuple {
variant_body.ident(field.unwrap_ident().clone());
variant_body.ident_ref(&field.unwrap_ident());
variant_body.punct(':');
}
variant_body.push_parsed(format!("__{},", field.to_string_with_prefix("field_")))?;
Expand Down Expand Up @@ -1004,7 +1005,7 @@ impl DeriveBitPackedEnum {
arm_body.group(Delimiter::Parenthesis, |variant_case_body| {
variant_case_body.ident_str("Self");
variant_case_body.puncts("::");
variant_case_body.ident(variant.name.clone());
variant_case_body.ident_ref(&variant.name);

if let Some(fields) = variant.fields.as_ref() {
let is_tuple = matches!(fields, Fields::Tuple(_));
Expand All @@ -1013,7 +1014,7 @@ impl DeriveBitPackedEnum {
|variant_body| {
for field in fields.names() {
if !is_tuple {
variant_body.ident(field.unwrap_ident().clone());
variant_body.ident_ref(&field.unwrap_ident());
variant_body.punct(':');
}
variant_body.push_parsed(format!("__{},", field.to_string_with_prefix("field_")))?;
Expand Down
11 changes: 6 additions & 5 deletions derive/src/derive_enum.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::attribute::ContainerAttributes;
use crate::attribute::FieldAttributes;
use crate::stream_builder_ext::StreamBuilderExt;
use virtue::prelude::*;

const TUPLE_FIELD_PREFIX: &str = "field_";
Expand Down Expand Up @@ -61,7 +62,7 @@ impl DeriveEnum {
// Self::Variant
match_body.ident_str("Self");
match_body.puncts("::");
match_body.ident(variant.name.clone());
match_body.ident_ref(&variant.name);

// if we have any fields, declare them here
// Self::Variant { a, b, c }
Expand Down Expand Up @@ -280,7 +281,7 @@ impl DeriveEnum {
arm_body.group(Delimiter::Parenthesis, |variant_case_body| {
variant_case_body.ident_str("Self");
variant_case_body.puncts("::");
variant_case_body.ident(variant.name.clone());
variant_case_body.ident_ref(&variant.name);

variant_case_body.group(Delimiter::Brace, |variant_body| {
if let Some(fields) = variant.fields.as_ref() {
Expand All @@ -290,7 +291,7 @@ impl DeriveEnum {
if is_tuple {
variant_body.lit_usize(idx);
} else {
variant_body.ident(field.unwrap_ident().clone());
variant_body.ident_ref(&field.unwrap_ident());
}
variant_body.punct(':');
let attributes = field.attributes().get_attribute::<FieldAttributes>()?.unwrap_or_default();
Expand Down Expand Up @@ -401,7 +402,7 @@ impl DeriveEnum {
arm_body.group(Delimiter::Parenthesis, |variant_case_body| {
variant_case_body.ident_str("Self");
variant_case_body.puncts("::");
variant_case_body.ident(variant.name.clone());
variant_case_body.ident_ref(&variant.name);

variant_case_body.group(Delimiter::Brace, |variant_body| {
if let Some(fields) = variant.fields.as_ref() {
Expand All @@ -411,7 +412,7 @@ impl DeriveEnum {
if is_tuple {
variant_body.lit_usize(idx);
} else {
variant_body.ident(field.unwrap_ident().clone());
variant_body.ident_ref(&field.unwrap_ident());
}
variant_body.punct(':');
let attributes = field.attributes().get_attribute::<FieldAttributes>()?.unwrap_or_default();
Expand Down
1 change: 1 addition & 0 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#![allow(unused_must_use)]

mod attribute;
mod stream_builder_ext;
mod derive_bit_packed;
mod derive_enum;
mod derive_fingerprint;
Expand Down
13 changes: 13 additions & 0 deletions derive/src/stream_builder_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use virtue::prelude::Ident;
use virtue::generate::StreamBuilder;

pub(crate) trait StreamBuilderExt {
fn ident_ref(&mut self, ident: &Ident) -> &mut Self;
}

impl StreamBuilderExt for StreamBuilder {
fn ident_ref(&mut self, ident: &Ident) -> &mut Self {
self.ident(ident.clone());
self
}
}