partially
Provides a configurable derive macro that generates another struct with the same fields, but wrapped in
Option<T>, and implementsPartialto allow conditionally applying the generated struct fields to the base struct fields.
partially provides the Partial trait, which allows applying another structs values to self, with the intent that the other struct mirrors the fields of self, but wrapped in Option<T>.
Further, partially_derive (or partially with the derive feature enabled) supports automatically generating a mirrored struct with each field wrapped in Option<T>, and generates a Partial implementation that allows applying the Some fields of the mirrored struct to the base struct. I expect most folks will be most interested in using the derive macro.
Usage
With derive:
// `partially` installed with feature `derive`
use Partial;
// define a base structure, with the `Partial` derive macro
// further, instruct the macro to derive `Default` on the generated structure
// example usage
Without derive:
use Partial;
Struct Options
derive
Usage example:
#[partially(derive(Debug, Default))].
Instructs the macro to generate a #[derive(...)] attribute on the generated struct.
rename
Usage example:
#[partially(rename = "MyGeneratedStruct")].
Instructs the macro to use a given identifier for the generated struct. By default, PartialBaseStructName is used, where BaseStructName is the name of the original struct.
crate
Usage example:
#[partially(crate = "my_partially_crate")].
Instructs the macro to use a different base path for the Partial trait implementation. By default, partially is used. This can be useful if you've forked the partially crate.
Field Options
rename
Usage example:
#[partially(rename = "new_field_name")].
Instructs the macro to use a given identifier for the generated field. By default, the same name as the base struct is used.
omit
Usage example:
#[partially(omit)].
Instructs the macro to omit the field from the generated struct. By default, no fields are omitted.
transparent
Usage example:
#[partially(transparent)].
Instructs the macro to skip wrapping the generated field in Option<T>, instead transparently mirroring the field type into the generated struct.
as_type
Usage example:
#[partially(as_type = "Option<f32>")].
Instructs the macro to use the provided type instead of Option<T> when generating the field. Note that the provided type will be used verbatim, so if you expect an Option<T> value, you'll need to manually specify that.
Note: When using as_type, the given type must Into<BaseType> where BaseType is the original field type. This is required for Partial trait implementation.