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§

source§

impl<T: NativeClass> Instance<T, Unique>

source

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.

source

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>

source

pub fn into_base(self) -> Ref<T::Base, Own>

Returns the base object, dropping the script wrapper.

source

pub fn into_script(self) -> T::UserData

Returns the script wrapper.

source

pub fn decouple(self) -> (Ref<T::Base, Own>, T::UserData)

Returns the base object and the script wrapper.

source

pub fn base(&self) -> &Ref<T::Base, Own>

Returns a reference to the base object.

source

pub fn script(&self) -> &T::UserData

Returns a reference to the script wrapper.

source§

impl<T: NativeClass, Own: Ownership> Instance<T, Own>where
    RefImplBound: SafeAsRaw<<T::Base as GodotObject>::Memory, Own>,

source

pub fn try_from_base(
    owner: Ref<T::Base, Own>
) -> Result<Self, Ref<T::Base, Own>>

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.

source

pub fn from_base(owner: Ref<T::Base, Own>) -> Option<Self>

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().

source§

impl<T: NativeClass, Own: Ownership> Instance<T, Own>where
    RefImplBound: SafeDeref<<T::Base as GodotObject>::Memory, Own>,

source

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.

source

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.

source

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.

source§

impl<T: NativeClass> Instance<T, Shared>

Methods for instances with manually-managed base classes.

source

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.

source§

impl<T: NativeClass> Instance<T, Shared>where
    T::Base: GodotObject<Memory = ManuallyManaged>,

source

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>,

source

pub fn free(self)

Frees the base object and user-data wrapper.

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

source§

impl<T: NativeClass> Instance<T, Unique>where
    T::Base: GodotObject<Memory = ManuallyManaged> + QueueFree,

source

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 Nodes.

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

source§

impl<T: NativeClass> Instance<T, Unique>

source

pub fn into_shared(self) -> Instance<T, Shared>

Coverts into a Shared instance.

source§

impl<T: NativeClass> Instance<T, Unique>where
    T::Base: GodotObject<Memory = RefCounted>,

source

pub fn into_thread_local(self) -> Instance<T, ThreadLocal>

Coverts into a ThreadLocal instance.

source§

impl<T: NativeClass> Instance<T, Shared>

source

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.

source§

impl<T: NativeClass> Instance<T, Shared>where
    T::Base: GodotObject<Memory = RefCounted>,

source

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,

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug + NativeClass, Own: Debug + Ownership> Debug for Instance<T, Own>where
    T::Base: Debug,
    T::UserData: Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T> Export for Instance<T, Shared>where
    T: NativeClass,
    Instance<T, Shared>: ToVariant,

§

type Hint = NoHint

A type-specific hint type that is valid for the type being exported. Read more
source§

fn export_info(_hint: Option<Self::Hint>) -> ExportInfo

Returns ExportInfo given an optional typed hint.
source§

impl<T> FromVariant for Instance<T, Shared>where
    T: NativeClass,
    T::Base: GodotObject<Memory = RefCounted>,

source§

impl<T> OwnedToVariant for Instance<T, Unique>where
    T: NativeClass,

source§

impl<T, Own: Ownership> ToVariant for Instance<T, Own>where
    T: NativeClass,
    Ref<T::Base, Own>: ToVariant,

source§

impl<'a, T, U> AsArg<U> for &'a Instance<T, Shared>where
    T: NativeClass,
    T::Base: GodotObject + SubClass<U>,
    T::UserData: Map,
    U: GodotObject,

source§

impl<T, U, Own: Ownership> AsArg<U> for Instance<T, Own>where
    T: NativeClass,
    T::Base: GodotObject + SubClass<U>,
    T::UserData: Map,
    U: GodotObject,

Auto Trait Implementations§

§

impl<T, Own> RefUnwindSafe for Instance<T, Own>where
    Own: RefUnwindSafe,
    <T as NativeClass>::Base: RefUnwindSafe,
    <<<T as NativeClass>::Base as GodotObject>::Memory as MemorySpec>::PtrWrapper: RefUnwindSafe,
    <T as NativeClass>::UserData: RefUnwindSafe,

§

impl<T, Own> Send for Instance<T, Own>where
    Own: Send,
    <T as NativeClass>::UserData: Send,

§

impl<T, Own> Sync for Instance<T, Own>where
    Own: Sync,
    <T as NativeClass>::UserData: Sync,

§

impl<T, Own> Unpin for Instance<T, Own>where
    Own: Unpin,
    <<<T as NativeClass>::Base as GodotObject>::Memory as MemorySpec>::PtrWrapper: Unpin,
    <T as NativeClass>::UserData: Unpin,

§

impl<T, Own> UnwindSafe for Instance<T, Own>where
    Own: UnwindSafe,
    <T as NativeClass>::Base: RefUnwindSafe,
    <<<T as NativeClass>::Base as GodotObject>::Memory as MemorySpec>::PtrWrapper: UnwindSafe,
    <T as NativeClass>::UserData: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere
    T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere
    U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> ToOwned for Twhere
    T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere
    U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere
    U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.