partial

Macro partial 

Source
partial!() { /* proc-macro */ }
Expand description

Conveniently specify a partially borrowed struct type, by field(s).

§Syntax overview

Roughly

type X = partial!( Struct mut/const/! field field... , 
                          mut/const/! field field... * ,
                          ... );

More formally

Input      := BaseType FieldGroup,*
BaseType   := RustReference::TypePath
FieldGroup := Permission FieldSpec*
Permission := 'mut' | 'const' | '!'
FieldSpec  := '*' | RustReference::IDENTIFIER

§Examples

struct Struct { a:char, b:char, c:char, d:char, e:char };
type X = partial!( Struct mut a b c, const d, ! *);
type Y = partial!( Struct mut a ); // a is Mut, others are Const

§Interpretation

partial!() expands to the type of a partial view of Struct, according to the specified permissions.

Each list of fields starts with a a permission: mut (Mut), const (Const) or ! (No). The field lists (not the individual fields) are separated by commas.

* may appear once, in any group, and specifies the permission for fields not explicitly mentioned. If there is no *, Const is used.

Each field may appear only once. Ordering is not relevant.

§Expansion

Use of partial!() can be replaced by a plain concrete type Struct__Partial<P_field0, P_field1, P_field2, ...>. where P__... are Mut, Const or No, one per field.

For Reasons, the actual expansion of partial!() is something much more complicated and strange involving the Adjust trait impl and Struct__::FIELDS constant, both generated by the derive.

§impl partial!(...) { ... }

Rust does not support inherent impls on a type derived from a complicated chain of trait lookups, which is what partial!() produces.

If you want to make a method that takes a partial struct, you can:

  • Use an extension trait. e.g. via easy_ext (this is quite convenient).
  • Write out the Struct__Partial type directly, listing one Mut Const or No type paramter for each field in order;
  • Use a newtype and impl on that;
  • Make it a plain function rather than a method.

§Safety, and the field permission and borrowck systems

partial() allows you to name a type, for example for a variable or function parameter. It does not do any access control (borrowck consistency) checks. Those are done later, after the meaning of partial!() has been determined.

If what you ask for with your uses of partial(), reference reborrowing, etc., does not satisfy the rules, you will get a compile error.