Subclass

Derive Macro Subclass 

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

A derive macro for fromsoftware_shared::Subclass.

§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.

§Determining the Superclass

By default, the type of the first field in the subclass is used as the superclass. You can explicitly choose one or more superclasses instead using the #[subclass(base = SuperclassType)] attribute on the struct.

§Additional Features

This macro will also add trait implementations for AsRef<SuperclassType>, AsMut<SuperclassType>, and TryFrom<&SuperclassType>.

It will also implement Deref<Target = SuperclassType> and DerefMut, but because a type can only have one implementation of these traits, if this declares multiple superclasses they will only be implemented for the first one. Since types can be transitively dereferenced, be sure to order the bottommost superclass first so that all superclass methods can be accessed.

§Safety

The fromsoftware_shared::Subclass 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 Subclass documentation carefully to understand the requirements to use this safety.