Struct gdnative::prelude::Ref[][src]

pub struct Ref<T, Access = Shared> where
    Access: ThreadAccess,
    T: GodotObject
{ /* fields omitted */ }

A polymorphic smart pointer for Godot objects whose behavior changes depending on the memory management method of the underlying type and the thread access status.

Manually-managed types

Shared references to manually-managed types, like Ref<Node, Shared>, act like raw pointers. They are safe to alias, can be sent between threads, and can also be taken as method arguments (converted from Variant). They can't be used directly. Instead, it's required to obtain a safe view first. See the "Obtaining a safe view" section below for more information.

ThreadLocal references to manually-managed types cannot normally be obtained, since it does not add anything over Shared ones.

Unique references to manually-managed types, like Ref<Node, Unique>, can't be aliased or sent between threads, but can be used safely. However, they won't be automatically freed on drop, and are leaked if not passed to the engine or freed manually with free. Unique references can be obtained through constructors safely, or assume_unique in unsafe contexts.

Reference-counted types

Shared references to reference-counted types, like Ref<Reference, Shared>, act like Arc smart pointers. New references can be created with Clone, and they can be sent between threads. The pointer is presumed to be always valid. As such, more operations are available even when thread safety is not assumed. However, API methods still can't be used directly, and users are required to obtain a safe view first. See the "Obtaining a safe view" section below for more information.

ThreadLocal reference to reference-counted types, like Ref<Reference, ThreadLocal>, add the ability to call API methods safely. Unlike Unique references, it's unsafe to convert them to Shared because there might be other ThreadLocal references in existence.

Obtaining a safe view

In a lot of cases, references obtained from the engine as return values or arguments aren't safe to use, due to lack of pointer validity and thread safety guarantees in the API. As such, it's usually required to use unsafe code to obtain safe views of the same object before API methods can be called. The ways to cast between different reference types are as follows:

FromToMethodNote
Unique&'a TDeref (API methods can be called directly) / as_ref-
ThreadLocal&'a TDeref (API methods can be called directly) / as_refOnly if T is a reference-counted type.
Shared&'a Tunsafe assume_safe::<'a>The underlying object must be valid, and exclusive to this thread during 'a.
UniqueThreadLocalinto_thread_local-
UniqueSharedinto_shared-
SharedThreadLocalunsafe assume_thread_localThe reference must be local to the current thread.
Shared / ThreadLocalUniqueunsafe assume_uniqueThe reference must be unique.
ThreadLocalSharedunsafe assume_unique().into_shared()The reference must be unique.

Using as method arguments or return values

In order to enforce thread safety statically, the ability to be passed to the engine is only given to some reference types. Specifically, they are:

  • All owned Ref<T, Unique> references. The Unique access is lost if passed into a method.
  • Owned and borrowed Shared references, including temporary ones (TRef).

It's unsound to pass ThreadLocal references to the engine because there is no guarantee that the reference will stay on the same thread.

Conditional trait implementations

Many trait implementations for Ref are conditional, dependent on the type parameters. When viewing rustdoc documentation, you may expand the documentation on their respective impl blocks for more detailed explanations of the trait bounds.

Implementations

impl<T> Ref<T, Unique> where
    T: GodotObject + Instanciable
[src]

pub fn new() -> Ref<T, Unique>[src]

Creates a new instance of T.

The lifetime of the returned object is not automatically managed if T is a manually- managed type.

impl<T> Ref<T, Unique> where
    T: GodotObject
[src]

pub fn by_class_name(class_name: &str) -> Option<Ref<T, Unique>>[src]

Creates a new instance of a sub-class of T by its class name. Returns None if the class does not exist, cannot be constructed, has a different RefKind from, or is not a sub-class of T.

The lifetime of the returned object is not automatically managed if T is a manually- managed type. This means that if Object is used as the type parameter, any Reference objects created, if returned, will be leaked. As a result, such calls will return None. Casting between Object and Reference is possible on TRef and bare references.

impl<T, Access> Ref<T, Access> where
    Access: ThreadAccess,
    T: GodotObject,
    RefImplBound: SafeDeref<<T as GodotObject>::RefKind, Access>, 
[src]

Method for references that can be safely used.

pub fn as_ref(&self) -> TRef<'_, T, Access>[src]

Returns a safe temporary reference that tracks thread access.

Ref<T, Access> can be safely dereferenced if either:

  • T is reference-counted and Access is not Shared,
  • or, T is manually-managed and Access is Unique.

impl<T, Access> Ref<T, Access> where
    Access: ThreadAccess,
    T: GodotObject,
    RefImplBound: SafeAsRaw<<T as GodotObject>::RefKind, Access>, 
[src]

Methods for references that point to valid objects, but are not necessarily safe to use.

  • All Refs to reference-counted types always point to valid objects.
  • Ref to manually-managed types are only guaranteed to be valid if Unique.

pub fn cast<U>(self) -> Option<Ref<U, Access>> where
    U: GodotObject<RefKind = <T as GodotObject>::RefKind> + SubClass<T>, 
[src]

Performs a dynamic reference cast to target type, keeping the reference count. Shorthand for try_cast().ok().

The cast method can only be used for downcasts. For statically casting to a supertype, use upcast instead.

This is only possible between types with the same RefKinds, since otherwise the reference can get leaked. Casting between Object and Reference is possible on TRef and bare references.

pub fn upcast<U>(self) -> Ref<U, Access> where
    T: SubClass<U>,
    U: GodotObject<RefKind = <T as GodotObject>::RefKind>, 
[src]

Performs a static reference upcast to a supertype, keeping the reference count. This is guaranteed to be valid.

This is only possible between types with the same RefKinds, since otherwise the reference can get leaked. Casting between Object and Reference is possible on TRef and bare references.

pub fn try_cast<U>(self) -> Result<Ref<U, Access>, Ref<T, Access>> where
    U: GodotObject<RefKind = <T as GodotObject>::RefKind> + SubClass<T>, 
[src]

Performs a dynamic reference cast to target type, keeping the reference count.

This is only possible between types with the same RefKinds, since otherwise the reference can get leaked. Casting between Object and Reference is possible on TRef and bare references.

Errors

Returns Err(self) if the cast failed.

pub fn cast_instance<C>(self) -> Option<Instance<C, Access>> where
    C: NativeClass<Base = T>, 
[src]

Performs a downcast to a NativeClass instance, keeping the reference count. Shorthand for try_cast_instance().ok().

The resulting Instance is not necessarily safe to use directly.

pub fn try_cast_instance<C>(self) -> Result<Instance<C, Access>, Ref<T, Access>> where
    C: NativeClass<Base = T>, 
[src]

Performs a downcast to a NativeClass instance, keeping the reference count.

Errors

Returns Err(self) if the cast failed.

impl<T> Ref<T, Shared> where
    T: GodotObject
[src]

Methods for references that can't be used directly, and have to be assumed safe unsafely.

pub unsafe fn assume_safe<'a, 'r>(&'r self) -> TRef<'a, T, Shared> where
    AssumeSafeLifetime<'a, 'r>: LifetimeConstraint<<T as GodotObject>::RefKind>, 
[src]

Assume that self is safe to use, returning a reference that can be used to call API methods.

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

Suppose that the lifetime of the returned reference is 'a. It's safe to call assume_safe only if:

  1. During the entirety of 'a, the underlying object will always be valid.

    This is always true for reference-counted types. For them, the 'a lifetime will be constrained to the lifetime of &self.

    This means that any methods called on the resulting reference will not free it, unless it's the last operation within the lifetime.

    If any script methods are called, the code ran as a consequence will also not free it. This can happen via virtual method calls on other objects, or signals connected in a non-deferred way.

  2. During the entirety of 'a, the thread from which assume_safe is called has exclusive access to the underlying object.

    This is because all Godot objects have "interior mutability" in Rust parlance, and can't be shared across threads. The best way to guarantee this is to follow the official thread-safety guidelines across the codebase.

Failure to satisfy either of the conditions will lead to undefined behavior.

pub unsafe fn assume_unique(self) -> Ref<T, Unique>[src]

Assume that self is the unique reference to the underlying 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.

impl<T> Ref<T, Shared> where
    T: GodotObject<RefKind = ManuallyManaged>, 
[src]

Extra methods with explicit sanity checks for manually-managed unsafe references.

pub unsafe fn is_instance_sane(&self) -> bool[src]

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.

pub unsafe fn assume_safe_if_sane<'a>(&self) -> Option<TRef<'a, T, Shared>>[src]

Assume that self is safe to use, if a sanity check using is_instance_sane passed.

Safety

The same safety constraints as assume_safe applies. The sanity check does NOT guarantee that the operation is safe.

pub unsafe fn assume_unique_if_sane(self) -> Option<Ref<T, Unique>>[src]

Assume that self is the unique reference to the underlying object, if a sanity check using is_instance_sane passed.

Safety

Calling assume_unique_if_sane 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.

impl<T> Ref<T, Shared> where
    T: GodotObject<RefKind = RefCounted>, 
[src]

Methods for conversion from Shared to ThreadLocal access. This is only available for reference-counted types.

pub unsafe fn assume_thread_local(self) -> Ref<T, ThreadLocal>[src]

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.

impl<T> Ref<T, Unique> where
    T: GodotObject<RefKind = RefCounted>, 
[src]

Methods for conversion from Unique to ThreadLocal access. This is only available for reference-counted types.

pub fn into_thread_local(self) -> Ref<T, ThreadLocal>[src]

Convert to a thread-local reference.

This is guaranteed to be a no-op at runtime.

impl<T> Ref<T, Unique> where
    T: GodotObject
[src]

Methods for conversion from Unique to Shared access.

pub fn into_shared(self) -> Ref<T, Shared>[src]

Convert to a shared reference.

This is guaranteed to be a no-op at runtime.

impl<T> Ref<T, Unique> where
    T: GodotObject<RefKind = ManuallyManaged>, 
[src]

Methods for freeing Unique references to manually-managed objects.

pub fn free(self)[src]

Manually frees the object.

Manually-managed objects are not free-on-drop even when the access is unique, because it's impossible to know whether methods take "ownership" of them or not. It's up to the user to decide when they should be freed.

This is only available for Unique references. If you have a Ref with another access, and you are sure that it is unique, use assume_unique to convert it to a Unique one.

impl<T> Ref<T, Unique> where
    T: GodotObject<RefKind = ManuallyManaged> + QueueFree
[src]

Methods for freeing Unique references to manually-managed objects.

pub fn queue_free(self)[src]

Queues the object for deallocation in the near future. This is preferable for Nodes compared to Ref::free.

This is only available for Unique references. If you have a Ref with another access, and you are sure that it is unique, use assume_unique to convert it to a Unique one.

Trait Implementations

impl<'a, T, U> AsArg<U> for &'a Ref<T, Shared> where
    T: GodotObject + SubClass<U>,
    U: GodotObject
[src]

impl<T, U> AsArg<U> for Ref<T, Shared> where
    T: GodotObject + SubClass<U>,
    U: GodotObject
[src]

impl<T, U> AsArg<U> for Ref<T, Unique> where
    T: GodotObject + SubClass<U>,
    U: GodotObject
[src]

impl<'a, T> AsVariant for &'a Ref<T, Shared> where
    T: GodotObject
[src]

type Target = T

impl<T> AsVariant for Ref<T, Shared> where
    T: GodotObject
[src]

type Target = T

impl<T> AsVariant for Ref<T, Unique> where
    T: GodotObject
[src]

type Target = T

impl<T, Access> Borrow<T> for Ref<T, Access> where
    Access: ThreadAccess,
    T: GodotObject,
    RefImplBound: SafeDeref<<T as GodotObject>::RefKind, Access>, 
[src]

Ref<T, Access> can be safely dereferenced if either:

  • T is reference-counted and Access is not Shared,
  • or, T is manually-managed and Access is Unique.

impl<T, Access> Clone for Ref<T, Access> where
    Access: NonUniqueThreadAccess,
    T: GodotObject
[src]

Ref is Clone if the access is not Unique.

impl<T, Access> Copy for Ref<T, Access> where
    Access: NonUniqueThreadAccess,
    T: GodotObject<RefKind = ManuallyManaged>, 
[src]

Ref is Copy if the underlying object is manually-managed, and the access is not Unique.

impl<T, Access> Debug for Ref<T, Access> where
    Access: ThreadAccess,
    T: GodotObject
[src]

impl<T, Access> Deref for Ref<T, Access> where
    Access: ThreadAccess,
    T: GodotObject,
    RefImplBound: SafeDeref<<T as GodotObject>::RefKind, Access>, 
[src]

Ref<T, Access> can be safely dereferenced if either:

  • T is reference-counted and Access is not Shared,
  • or, T is manually-managed and Access is Unique.

type Target = T

The resulting type after dereferencing.

impl<T, Access> Eq for Ref<T, Access> where
    Access: ThreadAccess,
    T: GodotObject
[src]

Reference equality.

impl<T> Export for Ref<T, Shared> where
    T: GodotObject
[src]

type Hint = ()

A type-specific hint type that is valid for the type being exported.

impl<T> FromVariant for Ref<T, Shared> where
    T: GodotObject
[src]

impl<T, Access> Hash for Ref<T, Access> where
    Access: ThreadAccess,
    T: GodotObject
[src]

Hashes the raw pointer.

impl<T, Access> Ord for Ref<T, Access> where
    Access: ThreadAccess,
    T: GodotObject
[src]

Ordering of the raw pointer value.

impl<T> OwnedToVariant for Ref<T, Unique> where
    T: GodotObject
[src]

impl<T, Access, RhsAccess> PartialEq<Ref<T, RhsAccess>> for Ref<T, Access> where
    Access: ThreadAccess,
    T: GodotObject,
    RhsAccess: ThreadAccess
[src]

Reference equality.

impl<T, Access> PartialOrd<Ref<T, Access>> for Ref<T, Access> where
    Access: ThreadAccess,
    T: GodotObject
[src]

Ordering of the raw pointer value.

impl<T, Access> Send for Ref<T, Access> where
    Access: ThreadAccess + Send,
    T: GodotObject
[src]

Ref is Send if the thread access is Shared or Unique.

impl<T, Access> Sync for Ref<T, Access> where
    Access: ThreadAccess + Sync,
    T: GodotObject
[src]

Ref is Sync if the thread access is Shared.

impl<T> ToVariant for Ref<T, Shared> where
    T: GodotObject
[src]

Auto Trait Implementations

impl<T, Access> RefUnwindSafe for Ref<T, Access> where
    Access: RefUnwindSafe,
    T: RefUnwindSafe,
    <<T as GodotObject>::RefKind as RefKindSpec>::PtrWrapper: RefUnwindSafe
[src]

impl<T, Access> Unpin for Ref<T, Access> where
    Access: Unpin,
    <<T as GodotObject>::RefKind as RefKindSpec>::PtrWrapper: Unpin
[src]

impl<T, Access> UnwindSafe for Ref<T, Access> where
    Access: UnwindSafe,
    T: RefUnwindSafe,
    <<T as GodotObject>::RefKind as RefKindSpec>::PtrWrapper: UnwindSafe
[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> CallHasher for T where
    T: Hash + ?Sized

impl<Q, K> Equivalent<K> for Q where
    K: Borrow<Q> + ?Sized,
    Q: Eq + ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> OwnedToVariant for T where
    T: ToVariant
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.