[−][src]Trait pygamer::sercom::AnyPad
Meta-type representing any Pad
All instances of Pad
implement this trait. When used as a trait bound,
it acts to encapsulate a Pad
. Without this trait, a completely generic
Pad
requires three type parameters, i.e. Pad<S, P, M>
. But when using
this trait, only one type parameter is required, i.e. P: AnyPad
. However,
even / though we have dropped type parameters, no information is lost,
because the Sercom
, PadNum
and Map
type parameters are stored as
associated types in the trait. The implementation of AnyPad
looks
something like this:
impl<S: Sercom, P: PadNum, M: Map<S, P>> AnyPad for Pad<S, P, M> { type Sercom = S; type PadNum = P; type Map = M; // ... }
Thus, there is a one-to-one mapping between Pad<S, P, M>
and
AnyPad<Sercom = S, PadNum = P, Map = M>
, so you can always recover the
full, concrete type from an implementation of AnyPad
. The type alias
ConcretePad
is / provided for just this purpose.
AnyPad
as a trait bound
When using AnyPad
as a trait bound, you can constrain the associated
types to restrict the acceptable Pad
s. For example, you could restrict
a function to accept a particular pad number.
fn example<P>(pad: P) where P: AnyPad<PadNum = Pad2> { }
Or you could accept any pad number, as long as it's in the desired SERCOM.
fn example<P>(pad: P) where P: AnyPad<Sercom = Sercom4> { }
You can also apply more complex bounds.
fn example<P>(pad: P) where P: AnyPad, P::PadNum: UserTrait, { }
Generic AnyPad
s
Working with a generic type constrained by AnyPad
is slightly different
than working with a concrete Pad
. When compiling a generic function, the
compiler cannot assume anything about the specific concrete type. It can
only use what it knows about the AnyPad
trait. To cast a generic type to
a concrete type, use the as_concrete
method. To
cast back to the generic type, use the Pad
method
as_any
.