Expand description
Multiplicity for encoding netdoc elements, via ad-hoc deref specialisation.
This module supports type-based handling of multiplicity, of Items (within Documents) and Arguments (in Item keyword lines).
It is for use by macros, rather than directly.
See also parse2::multiplicity which is the corresponding module for parsing.
§Explanation
We use autoref specialisation to allow macros to dispatch to
trait impls for Vec<T>, Option<T> etc. as well as simply unadorned T.
When methods on MultiplicitySelector are called, the compiler finds
the specific implementation for MultiplicitySelector<Option<_>> or ..Vec<_>,
or, failing that, derefs and finds the blanket impl on &MultiplicitySelector<T>.
For Objects, where only T and Option<T> are allowed,
we use OptionalityMethods.
We implement traits on helper types struct MultiplicitySelector<Field>,
DeterminedMultiplicitySelector and SingletonMultiplicitySelector.
The three selector types allow us to force the compiler to nail down the multiplicity, during type inference, before considering whether the “each” type implements the required trait.
This is done by calling the .selector() method:
deref specialisation and inherent method vs trait method priority selects
the appropriate .selector() method, giving another selector,
so that the compiler only considers other selector’s MultiplicityMethods,
when .check_... methods are used.
Otherwise, when a field has type (say) Vec<NotItemValueParseable>,
a call to .check_item_value_encodable could be resolved by autoref
so the compiler reports that Vec<..> doesn’t implement the needed trait.
We prevent this by having
MultiplicitySelector::<Vec<_>>::default().selector()
be an inherent method returning DeterminedMultiplicitySelector.
SingletonMultiplicitySelector is used explicitly in the derive when we
know that we want to encode exactly one element:
for example, a document’s intro item cannot be repeated or omitted.
Structs§
- Determined
Multiplicity Selector - Helper type implementing
MultiplicityMethods, after the multiplicity is determined - Multiplicity
Selector - Helper type that allows us to select an impl of
MultiplicityMethods - Singleton
Multiplicity Selector - Helper type implementing
MultiplicityMethods, when a field is statically a singleton
Traits§
- Multiplicity
Methods - Methods for handling some multiplicity of netdoc elements, during encoding
- Optionality
Methods - Methods for handling optionality of a netdoc Object, during encoding