Trait objc2::encode::OptionEncode
source · pub unsafe trait OptionEncode { }
Expand description
A helper trait for types that are encodable inside an Option
.
See the nomicon for details on which types uphold this promise.
This is used to work around the orphan rule, which would normally prevent
you from implementing Encode
/RefEncode
for Option<CustomType>
.
Safety
You must ensure that the implemented type T
has the same layout as
Option<T>
.
Examples
use objc2::encode::{Encode, Encoding, OptionEncode};
use core::ptr::NonNull;
use core::ffi::c_void;
#[repr(transparent)]
struct MyBlockType(NonNull<c_void>);
// SAFETY: `MyBlockType` is meant to represent a pointer to a block
unsafe impl Encode for MyBlockType {
const ENCODING: Encoding = Encoding::Block;
}
// SAFETY: `MyBlockType` is `repr(transparent)` over `NonNull`, which
// means that `Option<MyBlockType>` has the same layout.
unsafe impl OptionEncode for MyBlockType {}
assert_eq!(<Option<MyBlockType>>::ENCODING, MyBlockType::ENCODING);