Skip to main content

OwnedPtr

Struct OwnedPtr 

Source
pub struct OwnedPtr<T, A: GameAllocator = NoOpAllocator> { /* private fields */ }
Expand description

Pointer to a structure that the containing structure owns. You will generally use this to model structures in foreign memory when extending the game libraries. Do not use this in your own code as you’re risking all rusts safety reasoning.

§Safety

§OwnedPtr in FFI

When declaring definitions of C++ structs and functions that use this type, the author must ensure several invariants hold true:

  • This must be the only way to access the data it refers to without an unsafe block (not including the unsafe block necessary to call FromStatic::instance). This means there may not be any other structs or methods that provide an OwnedPtr or a reference to the underlying data.

    Although generally this means that the struct that h olds an OwnedPtr is the same one that “owns” the data it refers to in the sense of being responsible for constructing and destroying it, that’s not a hard requirement. In some cases, it may be more ergonomic to expose an OwnedPtr (or a reference) through a struct that’s easy to obtain and use NonNull for the struct that actually has ownership.

  • This must ensure that the backing memory is allocated by an allocator that’s compatible with A. Note that all memory allocated in any way is compatible with NoOpAllocator, so this requirement only matters if A is set explicitly.

§OwnedPtr and Drop

For any type T where Rust code might take ownership over an OwnedPtr<T>, the author should be sure that T’s Drop implementation is correct. (This is true in general for FFI types that Rust code can own.) There are two concerns here:

  • Generally, C++ code will declare a specific destroy function for each type. In addition to calling the destructor method (~T()), this destroys any fields as well.

  • Rust drops fields in declaration order but C++ destroys them in reverse declaration order.

To mitigate these issues, all fields with non-trivial drop/destructor implementations should by wrapped in std::mem::ManuallyDrop. If the C++ code has a destroy method, it should be called from Drop::drop; otherwise, the implementation of Drop::drop should drop these fields in reverse declaration order.

Implementations§

Source§

impl<T, A: GameAllocator> OwnedPtr<T, A>

Source

pub fn new(value: T) -> Self

Allocates memory with A and places value into it.

This doesn’t actually allocate if T is zero-sized.

Important: any type constructed this way should have an appropriate Drop::drop implementation defined. See above for details.

Source

pub fn as_ptr(&self) -> *mut T

Trait Implementations§

Source§

impl<T, A: GameAllocator> AsMut<T> for OwnedPtr<T, A>

Source§

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

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<T, A: GameAllocator> AsRef<T> for OwnedPtr<T, A>

Source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T, A: GameAllocator> Debug for OwnedPtr<T, A>

Source§

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

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

impl<T: Default, A: GameAllocator> Default for OwnedPtr<T, A>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T, A: GameAllocator> Deref for OwnedPtr<T, A>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T, A: GameAllocator> DerefMut for OwnedPtr<T, A>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<T, A: GameAllocator> Drop for OwnedPtr<T, A>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<T, A: GameAllocator> Pointer for OwnedPtr<T, A>

Source§

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

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

impl<T: Send, A: GameAllocator + Send> Send for OwnedPtr<T, A>

Source§

impl<T, A: GameAllocator + Sync> Sync for OwnedPtr<T, A>
where T: Sync,

Auto Trait Implementations§

§

impl<T, A> Freeze for OwnedPtr<T, A>

§

impl<T, A> RefUnwindSafe for OwnedPtr<T, A>

§

impl<T, A> Unpin for OwnedPtr<T, A>
where A: Unpin,

§

impl<T, A> UnsafeUnpin for OwnedPtr<T, A>

§

impl<T, A> UnwindSafe for OwnedPtr<T, A>

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

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

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.