Skip to content

Commit 3cfd903

Browse files
VictorKoendersVictor Koenders
and
Victor Koenders
authored
Made IdentOrIndex cloneable (#67)
Co-authored-by: Victor Koenders <[email protected]>
1 parent c194f91 commit 3cfd903

File tree

3 files changed

+17
-19
lines changed

3 files changed

+17
-19
lines changed

src/parse/attributes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{Error, Result};
44
use std::iter::Peekable;
55

66
/// An attribute for the given struct, enum, field, etc
7-
#[derive(Debug)]
7+
#[derive(Debug, Clone)]
88
#[non_exhaustive]
99
pub struct Attribute {
1010
/// The location this attribute was parsed at

src/parse/body.rs

+15-17
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ fn test_struct_body_take() {
110110
assert_eq!(ident, "Foo");
111111
let body = StructBody::take(stream).unwrap();
112112
if let Some(Fields::Struct(v)) = body.fields {
113-
assert!(v.len() == 0);
113+
assert!(v.is_empty());
114114
} else {
115115
panic!("wrong fields {:?}", body.fields);
116116
}
@@ -121,7 +121,7 @@ fn test_struct_body_take() {
121121
assert_eq!(ident, "Foo");
122122
let body = StructBody::take(stream).unwrap();
123123
if let Some(Fields::Tuple(v)) = body.fields {
124-
assert!(v.len() == 0);
124+
assert!(v.is_empty());
125125
} else {
126126
panic!("wrong fields {:?}", body.fields);
127127
}
@@ -297,9 +297,7 @@ fn test_enum_body_take() {
297297
let fields = body.variants[1].fields.as_ref().unwrap();
298298
assert_eq!(fields.len(), 1);
299299
assert_eq!(fields.names().len(), 1);
300-
assert!(
301-
matches!(fields.names()[0], IdentOrIndex::Ident { ident, .. } if ident.to_string() == "a")
302-
);
300+
assert!(matches!(&fields.names()[0], IdentOrIndex::Ident { ident, .. } if *ident == "a"));
303301
assert_eq!(body.variants[1].get_integer(), 2);
304302

305303
let stream = &mut token_stream("enum Foo { Round(), Curly{}, Without }");
@@ -395,14 +393,14 @@ impl Fields {
395393
.map(|(index, field)| IdentOrIndex::Index {
396394
index,
397395
span: field.span(),
398-
attributes: &field.attributes,
396+
attributes: field.attributes.clone(),
399397
})
400398
.collect(),
401399
Self::Struct(fields) => fields
402400
.iter()
403401
.map(|(ident, field)| IdentOrIndex::Ident {
404-
ident,
405-
attributes: &field.attributes,
402+
ident: ident.clone(),
403+
attributes: field.attributes.clone(),
406404
})
407405
.collect(),
408406
};
@@ -552,14 +550,14 @@ impl UnnamedField {
552550
/// a: u32, // will be IdentOrIndex::Ident { ident: "a", .. }
553551
/// },
554552
/// }
555-
#[derive(Debug)]
556-
pub enum IdentOrIndex<'a> {
553+
#[derive(Debug, Clone)]
554+
pub enum IdentOrIndex {
557555
/// The variant is a named field
558556
Ident {
559557
/// The name of the field
560-
ident: &'a Ident,
558+
ident: Ident,
561559
/// The attributes of the field
562-
attributes: &'a Vec<Attribute>,
560+
attributes: Vec<Attribute>,
563561
},
564562
/// The variant is an unnamed field
565563
Index {
@@ -568,15 +566,15 @@ pub enum IdentOrIndex<'a> {
568566
/// The span of the field type
569567
span: Span,
570568
/// The attributes of this field
571-
attributes: &'a Vec<Attribute>,
569+
attributes: Vec<Attribute>,
572570
},
573571
}
574572

575-
impl<'a> IdentOrIndex<'a> {
573+
impl IdentOrIndex {
576574
/// Get the ident. Will panic if this is an `IdentOrIndex::Index`
577-
pub fn unwrap_ident(&self) -> &'a Ident {
575+
pub fn unwrap_ident(&self) -> Ident {
578576
match self {
579-
Self::Ident { ident, .. } => ident,
577+
Self::Ident { ident, .. } => ident.clone(),
580578
x => panic!("Expected ident, found {:?}", x),
581579
}
582580
}
@@ -611,7 +609,7 @@ impl<'a> IdentOrIndex<'a> {
611609
}
612610
}
613611

614-
impl std::fmt::Display for IdentOrIndex<'_> {
612+
impl std::fmt::Display for IdentOrIndex {
615613
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
616614
match self {
617615
IdentOrIndex::Ident { ident, .. } => write!(fmt, "{}", ident),

src/parse/generics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ fn test_generic_constraints_try_take() {
627627
assert_eq!(constraints[0].ident(), "T");
628628
let body = StructBody::take(stream).unwrap();
629629
if let Some(Fields::Struct(v)) = body.fields {
630-
assert!(v.len() == 0);
630+
assert!(v.is_empty());
631631
} else {
632632
panic!("wrong fields {:?}", body.fields);
633633
}

0 commit comments

Comments
 (0)