Struct gdnative_core::object::Instance
source · pub struct Instance<T: NativeClass, Own: Ownership = Shared> { /* private fields */ }
Expand description
A persistent reference to a GodotObject with a rust NativeClass attached.
Instance
s can be worked on directly using map
and map_mut
if the base object is
reference-counted. Otherwise, use assume_safe
to obtain a temporary TInstance
.
See the type-level documentation on Ref
for more information on typed thread accesses.
Implementations§
source§impl<T: NativeClass> Instance<T, Unique>
impl<T: NativeClass> Instance<T, Unique>
sourcepub fn new() -> Selfwhere
T::Base: Instanciable,
pub fn new() -> Selfwhere
T::Base: Instanciable,
Creates a T::Base
with the script T
attached. Both T::Base
and T
must have zero
argument constructors.
If T::Base
is manually-managed, then the resulting Instance
must be passed to
the engine or manually freed with Instance::free
. Otherwise, the base object will be
leaked.
Must be called after the library is initialized.
sourcepub fn emplace(script: T) -> Selfwhere
T::Base: Instanciable,
pub fn emplace(script: T) -> Selfwhere
T::Base: Instanciable,
Creates a T::Base
with a given instance of the script T
attached. T::Base
must
have a zero-argument constructor.
This may be used to create instances of scripts that do not have zero-argument constructors:
// This type does not have a zero-argument constructor. As a result, `Instance::new`
// will panic and `Foo.new` from GDScript will result in errors when the object is used.
#[derive(NativeScript)]
#[inherit(Reference)]
#[no_constructor]
struct MyIntVector(i64, i64);
#[methods]
impl MyIntVector {
// - snip -
}
// With `Instance::emplace`, however, we can expose "constructors" from a factory
// auto-load for our script type.
#[derive(NativeScript)]
#[inherit(Node)]
#[user_data(Aether<Self>)]
struct MyIntVectorFactory;
#[methods]
impl MyIntVectorFactory {
#[export]
fn make(&self, _owner: &Node, x: i64, y: i64) -> Instance<MyIntVector, Unique> {
Instance::emplace(MyIntVector(x, y))
}
}
If T::Base
is manually-managed, then the resulting Instance
must be passed to
the engine or manually freed with Instance::free
. Otherwise, the base object will be
leaked.
Must be called after the library is initialized.
source§impl<T: NativeClass, Own: Ownership> Instance<T, Own>
impl<T: NativeClass, Own: Ownership> Instance<T, Own>
source§impl<T: NativeClass, Own: Ownership> Instance<T, Own>where
RefImplBound: SafeAsRaw<<T::Base as GodotObject>::Memory, Own>,
impl<T: NativeClass, Own: Ownership> Instance<T, Own>where
RefImplBound: SafeAsRaw<<T::Base as GodotObject>::Memory, Own>,
source§impl<T: NativeClass, Own: Ownership> Instance<T, Own>where
RefImplBound: SafeDeref<<T::Base as GodotObject>::Memory, Own>,
impl<T: NativeClass, Own: Ownership> Instance<T, Own>where
RefImplBound: SafeDeref<<T::Base as GodotObject>::Memory, Own>,
sourcepub fn map<F, U>(&self, op: F) -> Result<U, <T::UserData as Map>::Err>where
T::UserData: Map,
F: FnOnce(&T, TRef<'_, T::Base, Own>) -> U,
pub fn map<F, U>(&self, op: F) -> Result<U, <T::UserData as Map>::Err>where
T::UserData: Map,
F: FnOnce(&T, TRef<'_, T::Base, Own>) -> U,
Calls a function with a NativeClass instance and its owner, and returns its return value. Can be used on reference counted types for multiple times.
sourcepub fn map_mut<F, U>(&self, op: F) -> Result<U, <T::UserData as MapMut>::Err>where
T::UserData: MapMut,
F: FnOnce(&mut T, TRef<'_, T::Base, Own>) -> U,
pub fn map_mut<F, U>(&self, op: F) -> Result<U, <T::UserData as MapMut>::Err>where
T::UserData: MapMut,
F: FnOnce(&mut T, TRef<'_, T::Base, Own>) -> U,
Calls a function with a NativeClass instance and its owner, and returns its return value. Can be used on reference counted types for multiple times.
sourcepub fn map_owned<F, U>(&self, op: F) -> Result<U, <T::UserData as MapOwned>::Err>where
T::UserData: MapOwned,
F: FnOnce(T, TRef<'_, T::Base, Own>) -> U,
pub fn map_owned<F, U>(&self, op: F) -> Result<U, <T::UserData as MapOwned>::Err>where
T::UserData: MapOwned,
F: FnOnce(T, TRef<'_, T::Base, Own>) -> U,
Calls a function with a NativeClass instance and its owner, and returns its return value. Can be used on reference counted types for multiple times.
Methods for instances with manually-managed base classes.
sourcepub unsafe fn assume_safe<'a, 'r>(&'r self) -> TInstance<'a, T, Shared>where
AssumeSafeLifetime<'a, 'r>: LifetimeConstraint<<T::Base as GodotObject>::Memory>,
pub unsafe fn assume_safe<'a, 'r>(&'r self) -> TInstance<'a, T, Shared>where
AssumeSafeLifetime<'a, 'r>: LifetimeConstraint<<T::Base as GodotObject>::Memory>,
Assume that self
is safe to use.
This is not guaranteed to be a no-op at runtime.
Safety
It’s safe to call assume_safe
only if the constraints of Ref::assume_safe
are satisfied for the base object.
sourcepub unsafe fn is_instance_sane(&self) -> bool
pub unsafe fn is_instance_sane(&self) -> bool
Returns true
if the pointer currently points to a valid object of the correct type.
This does NOT guarantee that it’s safe to use this pointer.
Safety
This thread must have exclusive access to the object during the call.
source§impl<T: NativeClass> Instance<T, Unique>where
T::Base: GodotObject<Memory = ManuallyManaged>,
impl<T: NativeClass> Instance<T, Unique>where
T::Base: GodotObject<Memory = ManuallyManaged>,
source§impl<T: NativeClass> Instance<T, Unique>where
T::Base: GodotObject<Memory = ManuallyManaged> + QueueFree,
impl<T: NativeClass> Instance<T, Unique>where
T::Base: GodotObject<Memory = ManuallyManaged> + QueueFree,
sourcepub fn queue_free(self)
pub fn queue_free(self)
Queues the base object and user-data wrapper for deallocation in the near future.
This should be preferred to free
for Node
s.
Same as self.into_base().queue_free()
.
source§impl<T: NativeClass> Instance<T, Unique>
impl<T: NativeClass> Instance<T, Unique>
Coverts into a Shared
instance.
source§impl<T: NativeClass> Instance<T, Unique>where
T::Base: GodotObject<Memory = RefCounted>,
impl<T: NativeClass> Instance<T, Unique>where
T::Base: GodotObject<Memory = RefCounted>,
sourcepub fn into_thread_local(self) -> Instance<T, ThreadLocal>
pub fn into_thread_local(self) -> Instance<T, ThreadLocal>
Coverts into a ThreadLocal
instance.
sourcepub unsafe fn assume_unique(self) -> Instance<T, Unique>
pub unsafe fn assume_unique(self) -> Instance<T, Unique>
Assume that self
is the unique reference to the underlying base object.
This is guaranteed to be a no-op at runtime if debug_assertions
is disabled. Runtime
sanity checks may be added in debug builds to help catch bugs.
Safety
Calling assume_unique
when self
isn’t the unique reference is instant undefined
behavior. This is a much stronger assumption than assume_safe
and should be used with
care.
sourcepub unsafe fn assume_thread_local(self) -> Instance<T, ThreadLocal>
pub unsafe fn assume_thread_local(self) -> Instance<T, ThreadLocal>
Assume that all references to the underlying object is local to the current thread.
This is guaranteed to be a no-op at runtime.
Safety
Calling assume_thread_local
when there are references on other threads is instant
undefined behavior. This is a much stronger assumption than assume_safe
and should
be used with care.
Trait Implementations§
source§impl<T, Own: Ownership> Clone for Instance<T, Own>where
T: NativeClass,
Ref<T::Base, Own>: Clone,
impl<T, Own: Ownership> Clone for Instance<T, Own>where
T: NativeClass,
Ref<T::Base, Own>: Clone,
source§impl<T: Debug + NativeClass, Own: Debug + Ownership> Debug for Instance<T, Own>where
T::Base: Debug,
T::UserData: Debug,
impl<T: Debug + NativeClass, Own: Debug + Ownership> Debug for Instance<T, Own>where
T::Base: Debug,
T::UserData: Debug,
source§fn export_info(_hint: Option<Self::Hint>) -> ExportInfo
fn export_info(_hint: Option<Self::Hint>) -> ExportInfo
ExportInfo
given an optional typed hint.