Trait objc2_encode::RefEncode
source · [−]pub unsafe trait RefEncode {
const ENCODING_REF: Encoding<'static>;
}
Expand description
Types whoose references has an Objective-C type-encoding.
Implementing this for T
provides Encode
implementations for:
*const T
*mut T
&T
&mut T
NonNull<T>
Option<&T>
Option<&mut T>
Option<NonNull<T>>
Reasoning behind this trait’s existence
External crates cannot implement Encode
for pointers or Option
s
containing references, so instead, they can implement this trait.
Additionally it would be very cumbersome if every type had to implement
Encode
for all possible pointer types.
Finally, having this trait allows for much cleaner generic code that need to represent types that can be encoded as pointers.
Safety
References to the object must be FFI-safe.
See the nomicon entry on representing opaque structs for
information on how to represent objects that you don’t know the layout of
(or use extern type
(RFC-1861) if you’re using nightly).
Objective-C will make assumptions about the type (like its size, alignment and ABI) from its encoding, so the implementer must verify that the encoding is accurate.
Concretely, Self::ENCODING_REF
must match the result of running
@encode
in Objective-C with a pointer to the type in question.
Required Associated Constants
const ENCODING_REF: Encoding<'static>
const ENCODING_REF: Encoding<'static>
The Objective-C type-encoding for a reference of this type.
Should be one of Encoding::Object
, Encoding::Block
,
Encoding::Class
, Encoding::Pointer
, Encoding::Sel
or
Encoding::Unknown
.
Examples
This is usually implemented either as an object pointer:
const ENCODING_REF: Encoding<'static> = Encoding::Object;
Or as a pointer to the type, delegating the rest to the Encode
implementation:
const ENCODING_REF: Encoding<'static> = Encoding::Pointer(&Self::ENCODING);
Implementations on Foreign Types
sourceimpl RefEncode for bool
impl RefEncode for bool
const ENCODING_REF: Encoding<'static> = Encoding::Pointer(&Self::ENCODING)
sourceimpl RefEncode for i16
impl RefEncode for i16
const ENCODING_REF: Encoding<'static> = Encoding::Pointer(&Self::ENCODING)
sourceimpl RefEncode for i32
impl RefEncode for i32
const ENCODING_REF: Encoding<'static> = Encoding::Pointer(&Self::ENCODING)
sourceimpl RefEncode for i64
impl RefEncode for i64
const ENCODING_REF: Encoding<'static> = Encoding::Pointer(&Self::ENCODING)
sourceimpl RefEncode for isize
impl RefEncode for isize
const ENCODING_REF: Encoding<'static> = Encoding::Pointer(&Self::ENCODING)
sourceimpl RefEncode for u16
impl RefEncode for u16
const ENCODING_REF: Encoding<'static> = Encoding::Pointer(&Self::ENCODING)
sourceimpl RefEncode for u32
impl RefEncode for u32
const ENCODING_REF: Encoding<'static> = Encoding::Pointer(&Self::ENCODING)
sourceimpl RefEncode for u64
impl RefEncode for u64
const ENCODING_REF: Encoding<'static> = Encoding::Pointer(&Self::ENCODING)
sourceimpl RefEncode for usize
impl RefEncode for usize
const ENCODING_REF: Encoding<'static> = Encoding::Pointer(&Self::ENCODING)
sourceimpl RefEncode for f32
impl RefEncode for f32
const ENCODING_REF: Encoding<'static> = Encoding::Pointer(&Self::ENCODING)
sourceimpl RefEncode for f64
impl RefEncode for f64
const ENCODING_REF: Encoding<'static> = Encoding::Pointer(&Self::ENCODING)
sourceimpl RefEncode for i8
impl RefEncode for i8
Pointers to i8
use the special Encoding::String
encoding.
const ENCODING_REF: Encoding<'static> = Encoding::String
sourceimpl RefEncode for u8
impl RefEncode for u8
Pointers to u8
use the special Encoding::String
encoding.