#[repr(C)]pub struct Ivar(/* private fields */);
Expand description
A type that represents an instance variable.
Implementations§
source§impl Ivar
impl Ivar
sourcepub fn type_encoding(&self) -> &str
pub fn type_encoding(&self) -> &str
Returns the instance variable’s @encode(type)
string.
sourcepub unsafe fn load_ptr<T: Encode>(&self, obj: &AnyObject) -> *mut T
pub unsafe fn load_ptr<T: Encode>(&self, obj: &AnyObject) -> *mut T
Returns a pointer to the instance variable / ivar on the given object.
This is similar to UnsafeCell::get
, see that for more information
on what is and isn’t safe to do.
Usually you will have defined the instance variable yourself with
ClassBuilder::add_ivar
, the type of the ivar T
must match the
type used in that.
Library implementors are strongly encouraged to expose a safe interface to the ivar.
§Panics
Panics when debug_assertions
are enabled if the type encoding of the
ivar differs from the type encoding of T
.
§Safety
The object must have the given instance variable on it, and it must be
of type T
. Any invariants that the object have assumed about the
value of the instance variable must not be violated.
Note that an object can have multiple instance variables with the same
name; you must ensure that when the instance variable was retrieved,
was retrieved from the class that it was defined on. In particular,
getting a class dynamically using e.g. AnyObject::class
, and using
an instance variable from that here is not sound in general.
No thread syncronization is done on accesses to the variable, so you must ensure that any access to the returned pointer do not cause data races, and that Rust’s mutability rules are not otherwise violated.
sourcepub unsafe fn load<'obj, T: Encode>(&self, obj: &'obj AnyObject) -> &'obj T
pub unsafe fn load<'obj, T: Encode>(&self, obj: &'obj AnyObject) -> &'obj T
Returns a reference to the instance variable with the given name.
See Ivar::load_ptr
for more information.
§Panics
Panics when debug_assertions
are enabled if the type encoding of the
ivar differs from the type encoding of T
.
§Safety
The object must have the given instance variable on it, and it must be
of type T
.
No thread syncronization is done, so you must ensure that no other
thread is concurrently mutating the variable. This requirement can be
considered upheld if all mutation happens through Ivar::load_mut
(since that takes the object mutably).
sourcepub unsafe fn load_mut<'obj, T: Encode>(
&self,
obj: &'obj mut AnyObject
) -> &'obj mut T
pub unsafe fn load_mut<'obj, T: Encode>( &self, obj: &'obj mut AnyObject ) -> &'obj mut T
Returns a mutable reference to the ivar with the given name.
See Ivar::load_ptr
for more information.
§Panics
Panics when debug_assertions
are enabled if the type encoding of the
ivar differs from the type encoding of T
.
§Safety
The object must have an instance variable with the given name, and it
must be of type T
.
This access happens through &mut
, which means we know it to be the
only reference, hence you do not need to do any work to ensure that
data races do not happen.