Partial

Derive Macro Partial 

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

Constructs a struct with all fields of the original struct set to optional.

§Example

#[derive(Partial)]
#[partial(ident = PartialArticle, derive(Debug))]
pub struct Article {
    author: String,
    content: String,
    liked: usize,
    comments: String,
}

The above code will generate the following struct:

#[derive(Debug)]
pub struct PartialArticle {
    author: Option<String>,
    content: Option<String>,
    liked: Option<usize>,
    comments: Option<String>,
}

Several trait implementations are also generated:

  • From<Article> for PartialArticle
  • From<PartialArticle> for Article

§Attributes

#[derive(Partial)]
#[partial(
    ident = <IDENT>, // The identifier of the generated struct
    [derive(<DERIVE>, ...)], // Derive attributes for the generated struct
    [forward_attrs(<ATTR, ...> | not(<ATTR, ...>))], // Forward specific attributes to the generated struct
)]
pub struct BasedStruct {
    #[partial(
        [default = <DEFAULT>], // The default value of the field in the generated From impl
        [forward_attrs(<ATTR, ...> | not(<ATTR, ...>))], // Forward specific attributes to the generated field
            // If given, will override the container level `forward_attrs`
    )]
    field: FieldType,
}