Skip to main content

rusteal_core/
traits.rs

1// Marker traits for UE reflection types. Codegen generates impls for all
2// exported UClasses, UStructs, and UEnums.
3
4use rusteal_ffi::{UClassHandle, UObjectHandle, UStructHandle};
5
6use crate::error::RustealResult;
7
8/// Implemented by codegen for every exported UClass.
9///
10/// Provides the static UClass handle needed for cast checks and reflection
11/// queries. The handle is typically cached in a `OnceLock` on first access.
12pub trait UeClass: 'static {
13    /// Get the UClass* for this type (cached after first call).
14    fn static_class() -> UClassHandle;
15}
16
17/// Implemented by codegen for every exported UScriptStruct.
18pub trait UeStruct: 'static {
19    /// Get the UScriptStruct* for this type.
20    fn static_struct() -> UStructHandle;
21}
22
23/// Implemented by codegen for every exported UEnum.
24pub trait UeEnum: 'static {
25    /// The underlying integer representation (u8, i32, i64, etc.).
26    type Repr: Copy;
27}
28
29/// Declares the immediate UE parent class for codegen-exported classes.
30///
31/// Enables blanket `Deref` impls on `UObjectRef<T>`, `Checked<T>`, and
32/// `Pinned<T>` so inherited methods resolve automatically through the
33/// Deref chain instead of being flattened into each child's Ext trait.
34///
35/// Codegen generates `impl HasParent for Pawn { type Parent = Actor; }` etc.
36/// Root classes (e.g., `UObject`) do NOT implement this trait.
37pub trait HasParent: UeClass {
38    type Parent: UeClass;
39}
40
41/// Trait for types that hold a UObject handle and can validate it.
42///
43/// Both `UObjectRef<T>` and `Pinned<T>` implement this, enabling fallible
44/// validity checks. For infallible access, use [`ValidHandle`] instead.
45pub trait UeHandle {
46    /// Return the raw handle if the object is alive, or `Err(ObjectDestroyed)`.
47    fn checked_handle(&self) -> RustealResult<UObjectHandle>;
48
49    /// Return the raw handle without validity check.
50    fn raw_handle(&self) -> UObjectHandle;
51}
52
53/// Trait for types that have been pre-validated and can provide a handle
54/// without fallibility. `Checked<T>` and `Pinned<T>` implement this.
55///
56/// Codegen extension traits use this as a supertrait so that methods
57/// return `T` directly instead of `RustealResult<T>`.
58pub trait ValidHandle {
59    /// Return the raw handle. The implementor guarantees (or debug-asserts)
60    /// that the handle is valid.
61    fn handle(&self) -> UObjectHandle;
62}