Pick

Derive Macro Pick 

Source
#[derive(Pick)]
{
    // Attributes available to this derive:
    #[pick]
}
Expand description

Constructs structs by picking the set of fields from the original struct.

§Example

#[derive(Pick)]
#[pick(
    arg(ident = AuthorContent, fields(author, content), derive(Debug)),
    arg(ident = LikedComments, fields(liked, comments))
)]
pub struct Article {
    author: String,
    content: String,
    liked: usize,
    comments: String,
}

The above code will generate the following structs:

#[derive(Debug)]
pub struct AuthorContent {
    author: String,
    content: String,
}

pub struct LikedComments {
    liked: usize,
    comments: String,
}

Several trait implementations are also generated:

  • From<Article> for AuthorContent
  • From<Article> for LikedComments

§Attributes

#[derive(Pick)]
#[pick(
    [forward_attrs(<ATTR, ...> | not(<ATTR, ...>))], // Forward specific attributes to all generated structs
    arg(
        ident = <IDENT>, // The identifier of the generated struct
        fields(<FIELD>, ...), // The fields to pick from the original struct
        [derive(<DERIVE>, ...)], // Derive attributes for the generated struct
        [forward_attrs(<ATTR, ...> | not(<ATTR, ...>))], // Forward specific attributes to the generated struct
            // If given, will override the container level `forward_attrs`
    ), ...
)]
pub struct BasedStruct {
    #[pick(
        #[forward_attrs(<ATTR, ...> | not(<ATTR, ...>))], // Forward specific attributes to the generated field
            // If given, will override the container level and arg level `forward_attrs`
    )]
    field: FieldType,
}