[][src]Trait sequoia_openpgp::packet::key::KeyRole

pub trait KeyRole: Debug {
    fn convert_key<P: KeyParts>(key: Key<P, UnspecifiedRole>) -> Key<P, Self>
    where
        Self: Sized
;
fn convert_key_ref<P: KeyParts>(
        key: &Key<P, UnspecifiedRole>
    ) -> &Key<P, Self>
    where
        Self: Sized
;
fn convert_bundle<P: KeyParts>(
        bundle: KeyBundle<P, UnspecifiedRole>
    ) -> KeyBundle<P, Self>
    where
        Self: Sized
;
fn convert_bundle_ref<P: KeyParts>(
        bundle: &KeyBundle<P, UnspecifiedRole>
    ) -> &KeyBundle<P, Self>
    where
        Self: Sized
; }

A marker trait that captures a Key's role.

A Key can either be a primary key (key::PrimaryRole) or a subordinate key (key::SubordinateRole). For those cases where the type information needs to be erased (e.g., interfaces like Cert::keys), we provide the key::UnspecifiedRole marker.

Required methods

fn convert_key<P: KeyParts>(key: Key<P, UnspecifiedRole>) -> Key<P, Self> where
    Self: Sized

Converts a key with an unspecified role into this kind of key.

This function is helpful when you need to convert a concrete type into a generic type. Using From works, but requires adding a type bound to the generic type, which is ugly and invasive.

Examples

use sequoia_openpgp as openpgp;
use openpgp::Result;
use openpgp::packet::prelude::*;

fn f<R>(cert: &Cert, mut key: Key<key::UnspecifiedParts, R>)
    -> Result<Key<key::UnspecifiedParts, R>>
    where R: key::KeyRole
{
    // ...

    if criterium {
        // Cert::primary_key's return type is concrete
        // (Key<key::PublicParts, key::PrimaryRole>).  We need to
        // convert it to the generic type Key<key::UnspecifiedParts, R>.
        // First, we "downcast" it to have unspecified parts and an
        // unspecified role, then we use a method defined by the
        // generic type to perform the conversion to the generic
        // type R.
        key = R::convert_key(
            cert.primary_key().key().clone()
                .parts_into_unspecified()
                .role_into_unspecified());
    }

    // ...

    Ok(key)
}

fn convert_key_ref<P: KeyParts>(key: &Key<P, UnspecifiedRole>) -> &Key<P, Self> where
    Self: Sized

Converts a key reference with an unspecified role into this kind of key reference.

This function is helpful when you need to convert a concrete type into a generic type. Using From works, but requires adding a type bound to the generic type, which is ugly and invasive.

fn convert_bundle<P: KeyParts>(
    bundle: KeyBundle<P, UnspecifiedRole>
) -> KeyBundle<P, Self> where
    Self: Sized

Converts a key bundle with an unspecified role into this kind of key bundle.

This function is helpful when you need to convert a concrete type into a generic type. Using From works, but requires adding a type bound to the generic type, which is ugly and invasive.

fn convert_bundle_ref<P: KeyParts>(
    bundle: &KeyBundle<P, UnspecifiedRole>
) -> &KeyBundle<P, Self> where
    Self: Sized

Converts a key bundle reference with an unspecified role into this kind of key bundle reference.

This function is helpful when you need to convert a concrete type into a generic type. Using From works, but requires adding a type bound to the generic type, which is ugly and invasive.

Loading content...

Implementors

impl KeyRole for PrimaryRole[src]

impl KeyRole for SubordinateRole[src]

impl KeyRole for UnspecifiedRole[src]

Loading content...