pub struct CFRetained<T: ?Sized> { /* private fields */ }
Expand description
A reference counted pointer type for CoreFoundation types.
CFRetained
strongly references or “retains” the given object T
, and
decrements the retain count or “releases” it again when dropped, thereby
ensuring it will be deallocated at the right time.
The type T
inside CFRetained<T>
can be anything that implements
Type
, i.e. any CoreFoundation-like type.
§Comparison to other types
CFRetained<T>
is equivalent to objc2::rc::Retained
, and can be
converted to/from that when the "objc2"
feature is enabled. Note though
that this uses the underlying CoreFoundation primitives CFRetain
/
CFRelease
/ CFAutorelease
instead of objc_retain
/ objc_release
/
objc_autorelease
, to avoid depending on the Objective-C runtime if not
needed.
You can also view CFRetained<T>
as the CoreFoundation equivalent of
std::sync::Arc
, that is, it is a thread-safe reference-counting smart
pointer that allows cloning by bumping the reference count.
Unlike Arc
, objects can be retained directly from a &T
using
Type::retain
(for Arc
you need &Arc<T>
).
Weak references are not supported though without the Objective-C runtime.
§Forwarding implementations
Since CFRetained<T>
is a smart pointer, it Deref
s to T
.
It also forwards the implementation of a bunch of standard library traits
such as PartialEq
, AsRef
, and so on, so that it becomes possible
to use e.g. CFRetained<CFString>
as-if it was CFString
. Note that
having a CFString
directly is not possible since CoreFoundation objects
cannot live on the stack, but instead must reside on the heap, and as such
must be accessed behind a pointer or a reference (i.e. &CFString
).
§Memory layout
This is guaranteed to have the same size and alignment as a pointer to the
object, *const T
.
Additionally, it participates in the null-pointer optimization, that is,
Option<CFRetained<T>>
is guaranteed to have the same size as
CFRetained<T>
.
Implementations§
Source§impl<T: ?Sized + Type> CFRetained<T>
impl<T: ?Sized + Type> CFRetained<T>
Sourcepub unsafe fn from_raw(ptr: NonNull<T>) -> Self
pub unsafe fn from_raw(ptr: NonNull<T>) -> Self
Construct a CFRetained
from a pointer that already has +1 retain
count.
This is useful when you have been given a pointer to a type from some API that returns a retained pointer (i.e. follows the Create rule).
§Safety
You must uphold the same requirements as described in
CFRetained::retain
.
Additionally, you must ensure the given object pointer has +1 retain count.
Sourcepub fn into_raw(this: Self) -> NonNull<T>
pub fn into_raw(this: Self) -> NonNull<T>
Consumes the CFRetained
, returning a raw pointer with +1 retain
count.
After calling this function, the caller is responsible for the memory
previously managed by the CFRetained
.
This is effectively the opposite of CFRetained::from_raw
, see that
for more details on when this function is useful.
This is an associated method, and must be called as
CFRetained::into_raw(obj)
.
Sourcepub fn as_ptr(this: &Self) -> NonNull<T>
pub fn as_ptr(this: &Self) -> NonNull<T>
Returns a raw pointer to the type.
The pointer is valid for at least as long as the CFRetained
is held.
This is an associated method, and must be called as
CFRetained::as_ptr(&obj)
.
Sourcepub unsafe fn cast_unchecked<U: Type>(this: Self) -> CFRetained<U> ⓘ
pub unsafe fn cast_unchecked<U: Type>(this: Self) -> CFRetained<U> ⓘ
Unchecked conversion to another CoreFoundation type.
This is equivalent to an unsafe
cast
between two pointers, see
CFRetained::downcast
for a safe alternative.
This is an associated method, and must be called as
CFRetained::cast_unchecked(obj)
.
§Safety
You must ensure that the type can be reinterpreted as the given type.
If T
is not 'static
, you must ensure that U
ensures that the
data contained by T
is kept alive for as long as U
lives.
Additionally, you must ensure that any safety invariants that the new type has are upheld.
Source§impl<T: Type> CFRetained<T>
impl<T: Type> CFRetained<T>
Sourcepub fn downcast<U: ConcreteType>(self) -> Result<CFRetained<U>, Self>where
T: 'static,
pub fn downcast<U: ConcreteType>(self) -> Result<CFRetained<U>, Self>where
T: 'static,
Attempt to downcast the type to that of type U
.
This is the owned variant. Use CFType::downcast_ref
if you want to
convert a reference type. See also ConcreteType
for more details
on which types support being converted to.
See CFType::downcast_ref
for more details.
§Errors
If casting failed, this will return the original back as the Err
variant. If you do not care about this, and just want an Option
,
use .downcast().ok()
.
Sourcepub unsafe fn retain(ptr: NonNull<T>) -> Self
pub unsafe fn retain(ptr: NonNull<T>) -> Self
Retain the pointer and construct a CFRetained
from it.
This is useful when you have been given a pointer to a type from some API that returns a non-retained reference (i.e. follows the Get rule).
See also Type::retain
for a safe alternative when you already have
a reference to the type.
§Safety
The pointer must be valid as a reference (aligned, dereferenceable and
initialized, see the std::ptr
module for more information).
You must ensure that if T
is non-'static
(i.e. has a lifetime
parameter), that any data that T
may reference lives for at least as
long as the return value.
Sourcepub fn autorelease_ptr(this: Self) -> *mut T
pub fn autorelease_ptr(this: Self) -> *mut T
Autoreleases the CFRetained
, returning a pointer.
The object is not immediately released, but will be when the innermost / current autorelease pool is drained.
This is an associated method, and must be called as
CFRetained::autorelease_ptr(obj)
.
§Safety
This method is safe to call, but the returned pointer is only guaranteed to be valid until the innermost autorelease pool is drained.
Trait Implementations§
Source§impl<T: ?Sized> Borrow<T> for CFRetained<T>
impl<T: ?Sized> Borrow<T> for CFRetained<T>
Source§impl<T: Type> Clone for CFRetained<T>
impl<T: Type> Clone for CFRetained<T>
Source§fn clone(&self) -> Self
fn clone(&self) -> Self
Retain the type, increasing its reference count.
This calls Type::retain
internally.
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<T: ?Sized> Deref for CFRetained<T>
impl<T: ?Sized> Deref for CFRetained<T>
Source§impl<T: ?Sized + Error> Error for CFRetained<T>
Available on crate feature std
only.
impl<T: ?Sized + Error> Error for CFRetained<T>
std
only.Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
Source§impl<'a, A, T: ?Sized> Extend<A> for &'a CFRetained<T>
impl<'a, A, T: ?Sized> Extend<A> for &'a CFRetained<T>
Source§fn extend<I: IntoIterator<Item = A>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = A>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<A, T: ?Sized> Extend<A> for CFRetained<T>
impl<A, T: ?Sized> Extend<A> for CFRetained<T>
Source§fn extend<I: IntoIterator<Item = A>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = A>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<T: ?Sized + ConcreteType + 'static> From<CFRetained<T>> for CFRetained<CFType>
impl<T: ?Sized + ConcreteType + 'static> From<CFRetained<T>> for CFRetained<CFType>
Source§fn from(obj: CFRetained<T>) -> Self
fn from(obj: CFRetained<T>) -> Self
Convert to CFType
.
Source§impl<T: ?Sized + Message> From<CFRetained<T>> for Retained<T>
Available on crate feature objc2
only.
impl<T: ?Sized + Message> From<CFRetained<T>> for Retained<T>
objc2
only.Source§fn from(obj: CFRetained<T>) -> Self
fn from(obj: CFRetained<T>) -> Self
Convert a CFRetained
into a objc2::rc::Retained
.
This conversion is cost-free, since CoreFoundation types are fully interoperable with Objective-C retain/release message sending.
Source§impl<T: ?Sized + Type + Message> From<Retained<T>> for CFRetained<T>
Available on crate feature objc2
only.
impl<T: ?Sized + Type + Message> From<Retained<T>> for CFRetained<T>
objc2
only.Source§fn from(obj: Retained<T>) -> Self
fn from(obj: Retained<T>) -> Self
Convert a objc2::rc::Retained
into a CFRetained
.
This only works if the type is a CoreFoundation type (implements the
Type
trait).
This conversion is cost-free.
Source§impl<'a, T: ?Sized> Future for &'a CFRetained<T>
impl<'a, T: ?Sized> Future for &'a CFRetained<T>
Source§impl<'a, T: ?Sized> Hasher for &'a CFRetained<T>
impl<'a, T: ?Sized> Hasher for &'a CFRetained<T>
Source§fn write_u128(&mut self, i: u128)
fn write_u128(&mut self, i: u128)
u128
into this hasher.Source§fn write_usize(&mut self, i: usize)
fn write_usize(&mut self, i: usize)
usize
into this hasher.Source§fn write_i128(&mut self, i: i128)
fn write_i128(&mut self, i: i128)
i128
into this hasher.Source§fn write_isize(&mut self, i: isize)
fn write_isize(&mut self, i: isize)
isize
into this hasher.Source§fn write_length_prefix(&mut self, len: usize)
fn write_length_prefix(&mut self, len: usize)
hasher_prefixfree_extras
)Source§impl<T: ?Sized> Hasher for CFRetained<T>
impl<T: ?Sized> Hasher for CFRetained<T>
Source§fn write_u128(&mut self, i: u128)
fn write_u128(&mut self, i: u128)
u128
into this hasher.Source§fn write_usize(&mut self, i: usize)
fn write_usize(&mut self, i: usize)
usize
into this hasher.Source§fn write_i128(&mut self, i: i128)
fn write_i128(&mut self, i: i128)
i128
into this hasher.Source§fn write_isize(&mut self, i: isize)
fn write_isize(&mut self, i: isize)
isize
into this hasher.Source§fn write_length_prefix(&mut self, len: usize)
fn write_length_prefix(&mut self, len: usize)
hasher_prefixfree_extras
)Source§impl<T: Type> IntoIterator for CFRetained<CFArray<T>>
Available on crate feature CFArray
only.
impl<T: Type> IntoIterator for CFRetained<CFArray<T>>
CFArray
only.Source§impl<T: Type> IntoIterator for CFRetained<CFMutableArray<T>>
Available on crate feature CFArray
only.
impl<T: Type> IntoIterator for CFRetained<CFMutableArray<T>>
CFArray
only.Source§impl<T: Ord + ?Sized> Ord for CFRetained<T>
impl<T: Ord + ?Sized> Ord for CFRetained<T>
Source§impl<T: ?Sized + PartialEq<U>, U: ?Sized> PartialEq<CFRetained<U>> for CFRetained<T>
impl<T: ?Sized + PartialEq<U>, U: ?Sized> PartialEq<CFRetained<U>> for CFRetained<T>
Source§impl<T: ?Sized + PartialOrd<U>, U: ?Sized> PartialOrd<CFRetained<U>> for CFRetained<T>
impl<T: ?Sized + PartialOrd<U>, U: ?Sized> PartialOrd<CFRetained<U>> for CFRetained<T>
Source§impl<T: ?Sized> Pointer for CFRetained<T>
impl<T: ?Sized> Pointer for CFRetained<T>
Source§impl<'a, T: ?Sized> Read for &'a CFRetained<T>
Available on crate feature std
only.
impl<'a, T: ?Sized> Read for &'a CFRetained<T>
std
only.Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
read
, except that it reads into a slice of buffers. Read moreSource§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize>
buf
. Read moreSource§fn read_to_string(&mut self, buf: &mut String) -> Result<usize>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize>
buf
. Read moreSource§fn read_exact(&mut self, buf: &mut [u8]) -> Result<()>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<()>
buf
. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)Source§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read moreSource§impl<T: ?Sized> Read for CFRetained<T>
Available on crate feature std
only.
impl<T: ?Sized> Read for CFRetained<T>
std
only.Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize>
read
, except that it reads into a slice of buffers. Read moreSource§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize>
buf
. Read moreSource§fn read_to_string(&mut self, buf: &mut String) -> Result<usize>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize>
buf
. Read moreSource§fn read_exact(&mut self, buf: &mut [u8]) -> Result<()>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<()>
buf
. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)Source§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read moreSource§impl<'a, T: ?Sized> Seek for &'a CFRetained<T>
Available on crate feature std
only.
impl<'a, T: ?Sized> Seek for &'a CFRetained<T>
std
only.Source§fn seek(&mut self, pos: SeekFrom) -> Result<u64>
fn seek(&mut self, pos: SeekFrom) -> Result<u64>
Source§fn stream_position(&mut self) -> Result<u64>
fn stream_position(&mut self) -> Result<u64>
1.55.0 · Source§fn rewind(&mut self) -> Result<(), Error>
fn rewind(&mut self) -> Result<(), Error>
Source§impl<T: ?Sized> Seek for CFRetained<T>
Available on crate feature std
only.
impl<T: ?Sized> Seek for CFRetained<T>
std
only.Source§fn seek(&mut self, pos: SeekFrom) -> Result<u64>
fn seek(&mut self, pos: SeekFrom) -> Result<u64>
Source§fn stream_position(&mut self) -> Result<u64>
fn stream_position(&mut self) -> Result<u64>
1.55.0 · Source§fn rewind(&mut self) -> Result<(), Error>
fn rewind(&mut self) -> Result<(), Error>
Source§impl<'a, T: ?Sized> Write for &'a CFRetained<T>
impl<'a, T: ?Sized> Write for &'a CFRetained<T>
Source§impl<'a, T: ?Sized> Write for &'a CFRetained<T>
Available on crate feature std
only.
impl<'a, T: ?Sized> Write for &'a CFRetained<T>
std
only.Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn write_all(&mut self, buf: &[u8]) -> Result<()>
fn write_all(&mut self, buf: &[u8]) -> Result<()>
Source§fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<()>
fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)Source§impl<T: ?Sized> Write for CFRetained<T>
Available on crate feature std
only.
impl<T: ?Sized> Write for CFRetained<T>
std
only.Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn write_all(&mut self, buf: &[u8]) -> Result<()>
fn write_all(&mut self, buf: &[u8]) -> Result<()>
Source§fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<()>
fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)impl<T: Eq + ?Sized> Eq for CFRetained<T>
impl<T: ?Sized + RefUnwindSafe> RefUnwindSafe for CFRetained<T>
impl<T: ?Sized + Sync + Send> Send for CFRetained<T>
CFRetained<T>
is Send
if T
is Send + Sync
.
impl<T: ?Sized + Sync + Send> Sync for CFRetained<T>
CFRetained<T>
is Sync
if T
is Send + Sync
.