pub struct Instance<T: NativeClass, Own: Ownership = Shared> { /* private fields */ }
Expand description

A persistent reference to a GodotObject with a rust NativeClass attached.

Instances 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

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.

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.

Returns the base object, dropping the script wrapper.

Returns the script wrapper.

Returns the base object and the script wrapper.

Returns a reference to the base object.

Returns a reference to the script wrapper.

Try to downcast Ref<T::Base, Own> to Instance<T>, without changing the reference count if reference-counted.

Errors

Returns the original Ref if the cast failed.

Try to downcast Ref<T::Base, Own> to Instance<T>, without changing the reference count if reference-counted. Shorthand for Self::try_from_base().ok().

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.

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.

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.

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.

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.

Frees the base object and user-data wrapper.

Same as self.into_base().free().

Queues the base object and user-data wrapper for deallocation in the near future. This should be preferred to free for Nodes.

Same as self.into_base().queue_free().

Coverts into a Shared instance.

Coverts into a ThreadLocal instance.

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.

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

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
A type-specific hint type that is valid for the type being exported. Read more
Returns ExportInfo given an optional typed hint.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.