[][src]Trait trellis_m4::sercom::v1::AnyPad

pub trait AnyPad: Sealed {
    type Sercom: Sercom;
    type PadNum: PadNum;
    type Map: Map<Self::Sercom, Self::PadNum>;
    pub fn as_concrete(self) -> Pad<Self::Sercom, Self::PadNum, Self::Map>;
}

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 Pads. 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 AnyPads

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.

Associated Types

Loading content...

Required methods

pub fn as_concrete(self) -> Pad<Self::Sercom, Self::PadNum, Self::Map>[src]

Convert a type that implements AnyPad to a concrete Pad

Even though there is a one-to-one mapping between Pad<I, M> and AnyPad<Sercom = S, PadNum = P, Map = M>, the compiler doesn't know that. This method provides a way to convert from an AnyPad to a concrete Pad.

Loading content...

Implementors

impl<S, P, M> AnyPad for Pad<S, P, M> where
    P: PadNum,
    M: Map<S, P>,
    S: Sercom
[src]

type Sercom = S

type PadNum = P

type Map = M

Loading content...