Struct objc2::rc::Allocated

source ·
pub struct Allocated<T: ?Sized> { /* private fields */ }
Expand description

An Objective-C object that has been allocated, but not initialized.

Objective-C splits the allocation and initialization steps up into two, so we need to track it in the type-system whether something has been intialized or not.

Note that allocation in Objective-C can fail, e.g. in Out Of Memory situations! This is handled by objc2 automatically, but if you really need to, you can check for this explicitly by inspecting the pointer returned from as_ptr.

Note also that this represents that the current class’s instance variables are not yet initialized; but subclass instance variables may have been so.

See Apple’s documentation on Object Allocation for a few more details.

§Memory layout

This is guaranteed to have the same size and alignment as a pointer to the object, *const T. The pointer may be NULL.

Implementations§

source§

impl<T: ?Sized + Message> Allocated<T>

source

pub fn as_ptr(this: &Self) -> *const T

Returns a raw pointer to the object.

The pointer is valid for at least as long as the Allocated is held.

See Allocated::as_mut_ptr for the mutable equivalent.

This is an associated method, and must be called as Allocated::as_ptr(obj).

source

pub fn as_mut_ptr(this: &mut Self) -> *mut T

Returns a raw mutable pointer to the object.

The pointer is valid for at least as long as the Allocated is held.

See Allocated::as_ptr for the immutable equivalent.

This is an associated method, and must be called as Allocated::as_mut_ptr(obj).

§Note about mutable references

In general, you’re not allowed to create a mutable reference from Allocated, unless you’re defining the object and know that to be safe.

For example, +[NSMutableString alloc] is allowed to return a non-unique object as an optimization, and then only figure out afterwards whether it needs to allocate, or if it can store an NSString internally.

Similarly, while e.g. +[NSData alloc] may return a unique object, calling -[NSData init] afterwards could return a shared empty NSData instance.

source

pub fn set_ivars(self, ivars: T::Ivars) -> PartialInit<T>
where T: DeclaredClass + Sized,

Initialize the instance variables for this object.

This consumes the allocated instance, and returns the now partially initialized instance instead, which can be further used in msg_send_id! super calls.

This works very similarly to Swift’s two-phase initialization scheme, see that for details.

§Panics

If debug assertions are enabled, this function will panic if the allocated instance is NULL, which usually only happens in Out of Memory situations.

If debug assertions are disabled, this will return a NULL instance and the ivars will be dropped. The NULL instance cannot cause unsoundness and will likely lead to an initialization failure later on instead, but not panicking here is done as a code-size optimization.

Trait Implementations§

source§

impl<T: Debug + ?Sized> Debug for Allocated<T>

source§

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

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

impl<T: ?Sized> Drop for Allocated<T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<T: ?Sized> Pointer for Allocated<T>

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Allocated<T>
where T: ?Sized,

§

impl<T> !RefUnwindSafe for Allocated<T>

§

impl<T> !Send for Allocated<T>

§

impl<T> !Sync for Allocated<T>

§

impl<T> !Unpin for Allocated<T>

§

impl<T> !UnwindSafe for Allocated<T>

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<T, U> TryFrom<U> for T
where U: Into<T>,

§

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

§

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.
source§

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