Trait Inherits

Source
pub unsafe trait Inherits<Base: GodotClass>: GodotClass { }
Expand description

Non-strict inheritance relationship in the Godot class hierarchy.

Derived: Inherits<Base> means that either Derived is a subclass of Base, or the class Base itself (hence “non-strict”).

This trait is automatically implemented for all Godot engine classes and user-defined classes that derive GodotClass. It has GodotClass as a supertrait, allowing your code to have bounds solely on Derived: Inherits<Base> rather than Derived: Inherits<Base> + GodotClass.

Inheritance is transitive across indirect base classes: Node3D implements Inherits<Node> and Inherits<Object>.

The trait is also reflexive: T always implements Inherits<T>.

§Usage

The primary use case for this trait is polymorphism: you write a function that accepts anything that derives from a certain class (including the class itself):

fn print_node<T>(node: Gd<T>)
where
    T: Inherits<Node>,
{
    let up = node.upcast(); // type Gd<Node> inferred
    println!("Node #{} with name {}", up.instance_id(), up.get_name());
    up.free();
}

// Call with different types
print_node(Node::new_alloc());   // works on T=Node as well
print_node(Node2D::new_alloc()); // or derived classes
print_node(Node3D::new_alloc());

A variation of the above pattern works without Inherits or generics, if you move the upcast() into the call site:

fn print_node(node: Gd<Node>) { /* ... */ }

// Call with different types
print_node(Node::new_alloc());            // no upcast needed
print_node(Node2D::new_alloc().upcast());
print_node(Node3D::new_alloc().upcast());

§Safety

This trait must only be implemented for subclasses of Base.

Importantly, this means it is always safe to upcast a value of type Gd<Self> to Gd<Base>.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl Inherits<CanvasItem> for Node2D

Source§

impl Inherits<InputEvent> for InputEventAction

Source§

impl Inherits<MainLoop> for SceneTree

Source§

impl Inherits<Mesh> for ArrayMesh

Source§

impl Inherits<Mesh> for PrimitiveMesh

Source§

impl Inherits<Node> for CanvasItem

Source§

impl Inherits<Node> for EditorPlugin

Source§

impl Inherits<Node> for HttpRequest

Source§

impl Inherits<Node> for Node2D

Source§

impl Inherits<Node> for Node3D

Source§

impl Inherits<Node> for Viewport

Source§

impl Inherits<Node> for Window

Source§

impl Inherits<Object> for ArrayMesh

Source§

impl Inherits<Object> for CanvasItem

Source§

impl Inherits<Object> for ClassDb

Source§

impl Inherits<Object> for EditorExportPlugin

Source§

impl Inherits<Object> for EditorPlugin

Source§

impl Inherits<Object> for Engine

Source§

impl Inherits<Object> for FileAccess

Source§

impl Inherits<Object> for GDScript

Source§

impl Inherits<Object> for HttpRequest

Source§

impl Inherits<Object> for Input

Source§

impl Inherits<Object> for InputEvent

Source§

impl Inherits<Object> for InputEventAction

Source§

impl Inherits<Object> for MainLoop

Source§

impl Inherits<Object> for Mesh

Source§

impl Inherits<Object> for Node2D

Source§

impl Inherits<Object> for Node3D

Source§

impl Inherits<Object> for Node

Source§

impl Inherits<Object> for Os

Source§

impl Inherits<Object> for PackedScene

Source§

impl Inherits<Object> for PrimitiveMesh

Source§

impl Inherits<Object> for RefCounted

Source§

impl Inherits<Object> for RenderingServer

Source§

impl Inherits<Object> for Resource

Source§

impl Inherits<Object> for ResourceFormatLoader

Source§

impl Inherits<Object> for ResourceLoader

Source§

impl Inherits<Object> for ResourceSaver

Source§

impl Inherits<Object> for SceneTree

Source§

impl Inherits<Object> for Script

Source§

impl Inherits<Object> for ScriptExtension

Source§

impl Inherits<Object> for ScriptLanguage

Source§

impl Inherits<Object> for ScriptLanguageExtension

Source§

impl Inherits<Object> for Texture

Source§

impl Inherits<Object> for Time

Source§

impl Inherits<Object> for Viewport

Source§

impl Inherits<Object> for Window

Source§

impl Inherits<RefCounted> for ArrayMesh

Source§

impl Inherits<RefCounted> for EditorExportPlugin

Source§

impl Inherits<RefCounted> for FileAccess

Source§

impl Inherits<RefCounted> for GDScript

Source§

impl Inherits<RefCounted> for InputEvent

Source§

impl Inherits<RefCounted> for InputEventAction

Source§

impl Inherits<RefCounted> for Mesh

Source§

impl Inherits<RefCounted> for PackedScene

Source§

impl Inherits<RefCounted> for PrimitiveMesh

Source§

impl Inherits<RefCounted> for Resource

Source§

impl Inherits<RefCounted> for ResourceFormatLoader

Source§

impl Inherits<RefCounted> for Script

Source§

impl Inherits<RefCounted> for ScriptExtension

Source§

impl Inherits<RefCounted> for Texture

Source§

impl Inherits<Resource> for ArrayMesh

Source§

impl Inherits<Resource> for GDScript

Source§

impl Inherits<Resource> for InputEvent

Source§

impl Inherits<Resource> for InputEventAction

Source§

impl Inherits<Resource> for Mesh

Source§

impl Inherits<Resource> for PackedScene

Source§

impl Inherits<Resource> for PrimitiveMesh

Source§

impl Inherits<Resource> for Script

Source§

impl Inherits<Resource> for ScriptExtension

Source§

impl Inherits<Resource> for Texture

Source§

impl Inherits<Script> for GDScript

Source§

impl Inherits<Script> for ScriptExtension

Source§

impl Inherits<ScriptLanguage> for ScriptLanguageExtension

Source§

impl Inherits<Viewport> for Window

Source§

impl<T: GodotClass> Inherits<T> for T