Superclass

Derive Macro Superclass 

Source
#[derive(Superclass)]
{
    // Attributes available to this derive:
    #[superclass]
}
Expand description

A derive macro for fromsoftware_shared::Superclass.

§Finding the RVA

This adds an implementation of Subclass that gets its VMT address from a standard RVA struct. This assumes:

  • The crate using this contains a crate::rva module that exposes a get() function.

  • The get() function’s return value has a public field whose name is a snake-case version of this struct’s name, followed by _vmt.

For example, ChrIns uses crate::rva::get().chr_ins_vmt as its VMT RVA.

§Subclass Enums

By default, this macro will just generate a straightforward implementation of Superclass. But if you want, you can add a #[superclass(children(ChildName1, ChildName2))] attribute to the struct. If you do, the macro will also define two enums, one immutable and one mutable.

For example:

#[repr(C)]
#[derive(Superclass)]
#[superclass(children(Cow, Pig))]
pub struct Animal {
  _vftable: usize,
}

will generate

pub enum AnimalSubclasses<'sub> {
  Cow(&'sub Cow),
  Pig(&'sub Pig),
  Animal(&'sub Animal),
}

pub enum AnimalSubclassesMut<'sub> {
  Cow(&'sub mut Cow),
  Pig(&'sub mut Pig),
  Animal(&'sub mut Animal),
}

impl AnimalSubclasses<'_> {
  pub fn superclass(&self) -> &Animal;
}

impl AnimalSubclassesMut<'_> {
  pub fn superclass(&self) -> &Animal;
  pub fn superclass_mut(&mut self) -> &mut Animal;
}

impl<'sub> From<AnimalSubclassesMut<'sub>> for AnimalSubclasses<'sub> {}
impl<'sub> From<&'sub T> for AnimalSubclasses<'sub> where T: Subclass<Animal> {}
impl<'sub> From<&'sub mut T> for AnimalSubclassesMut<'sub> where T: Subclass<Animal> {}

§Safety

The fromsoftware_shared::Superclass trait is unsafe, and even though there’s currently no way to require that a derive macro be explicitly flagged as unsafe, this does not add any additional safety guarantees beyond a manual implementation. PLease read the Superclass documentation carefully to understand the requirements to use this safety.