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: Copy + 'static {
25    /// The underlying integer representation (u8, i32, i64, etc.).
26    type Repr: Copy;
27
28    /// The UEnum, typed like a class handle (an enum `#[uproperty]` needs it).
29    fn static_enum() -> UClassHandle;
30
31    /// The value as UE stores it in a property.
32    fn to_i64(self) -> i64;
33
34    /// The variant for a value UE stored; `None` for a value the enum lacks.
35    fn from_i64(value: i64) -> Option<Self>;
36}
37
38/// Declares the immediate UE parent class for codegen-exported classes.
39///
40/// Enables blanket `Deref` impls on `UObjectRef<T>`, `Checked<T>`, and
41/// `Pinned<T>` so inherited methods resolve automatically through the
42/// Deref chain instead of being flattened into each child's Ext trait.
43///
44/// Codegen generates `impl HasParent for Pawn { type Parent = Actor; }` etc.
45/// Root classes (e.g., `UObject`) do NOT implement this trait.
46pub trait HasParent: UeClass {
47    type Parent: UeClass;
48}
49
50/// `Self` is `U` or one of its subclasses: a `UObjectRef<Self>` is a valid
51/// `UObjectRef<U>`, as a `Character*` is an `AActor*` in C++.
52///
53/// Codegen implements it for every class and each of its ancestors
54/// (`impl Inherits<Actor> for Character`); a Rust `#[uclass]` gets it for
55/// everything its parent inherits. See `UObjectRef::upcast_to`.
56pub trait Inherits<U: UeClass>: UeClass {}
57
58/// Trait for types that hold a UObject handle and can validate it.
59///
60/// Both `UObjectRef<T>` and `Pinned<T>` implement this, enabling fallible
61/// validity checks. For infallible access, use [`ValidHandle`] instead.
62pub trait UeHandle {
63    /// Return the raw handle if the object is alive, or `Err(ObjectDestroyed)`.
64    fn checked_handle(&self) -> RustealResult<UObjectHandle>;
65
66    /// Return the raw handle without validity check.
67    fn raw_handle(&self) -> UObjectHandle;
68}
69
70/// Trait for types that have been pre-validated and can provide a handle
71/// without fallibility. `Checked<T>` and `Pinned<T>` implement this.
72///
73/// Codegen extension traits use this as a supertrait so that methods
74/// return `T` directly instead of `RustealResult<T>`.
75pub trait ValidHandle {
76    /// Return the raw handle. The implementor guarantees (or debug-asserts)
77    /// that the handle is valid.
78    fn handle(&self) -> UObjectHandle;
79}