for_all_subclasses

Attribute Macro for_all_subclasses 

Source
#[for_all_subclasses]
Expand description

A proc macro attribute for defining an extension trait that makes a set of methods available for all subclasses of a superclass.

This expects to be used on a trait impl whose trait name is not defined and whose target is Subclass<...>. This impl should include functions. Unlike normal trait implementations, this impl should have a visibility modifier (unless you want it to be private). For example:

#[for_all_subclasses]
pub impl ChrInsExt for Subclass<ChrIns> {
    pub fn apply_speffect(&mut self, sp_effect: i32, dont_sync: bool) {
        let rva = Program::current()
            .rva_to_va(rva::get().chr_ins_apply_speffect)
            .unwrap();

        let call = unsafe { transmute::<u64, extern "C" fn(&mut ChrIns, i32, bool) -> u64>(rva) };
        call(self, sp_effect, dont_sync);
    }

    pub fn remove_speffect(&mut self, sp_effect: i32) {
        let rva = Program::current()
            .rva_to_va(rva::get().chr_ins_remove_speffect)
            .unwrap();

        let call = unsafe { transmute::<u64, extern "C" fn(&mut ChrIns, i32) -> u64>(rva) };
        call(self, sp_effect);
    }
}

This will define a trait with the given name and visibility, then implement it for all subclasses of the given superclass. This allow superclass methods to be freely called on any subclass, delegating to the superclass they contain.