referrerpolicy=no-referrer-when-downgrade

Trait polkadot_sdk_frame::prelude::IsSubType

pub trait IsSubType<T> {
    // Required method
    fn is_sub_type(&self) -> Option<&T>;
}
Expand description

Something that can be checked to be a of sub type T.

This is useful for enums where each variant encapsulates a different sub type, and you need access to these sub types.

For example, in FRAME, this trait is implemented for the runtime Call enum. Pallets use this to check if a certain call is an instance of the local pallet’s Call enum.

§Example


enum Test {
    String(String),
    U32(u32),
}

impl IsSubType<String> for Test {
    fn is_sub_type(&self) -> Option<&String> {
        match self {
            Self::String(ref r) => Some(r),
            _ => None,
        }
    }
}

impl IsSubType<u32> for Test {
    fn is_sub_type(&self) -> Option<&u32> {
        match self {
            Self::U32(ref r) => Some(r),
            _ => None,
        }
    }
}

fn main() {
    let data = Test::String("test".into());

    assert_eq!("test", IsSubType::<String>::is_sub_type(&data).unwrap().as_str());
}

Required Methods§

fn is_sub_type(&self) -> Option<&T>

Returns Some(_) if self is an instance of sub type T.

Implementors§