Enum objc2_encode::Encoding
source · [−]#[non_exhaustive]
pub enum Encoding<'a> {
Show 29 variants
Char,
Short,
Int,
Long,
LongLong,
UChar,
UShort,
UInt,
ULong,
ULongLong,
Float,
Double,
LongDouble,
FloatComplex,
DoubleComplex,
LongDoubleComplex,
Bool,
Void,
String,
Object,
Block,
Class,
Sel,
Unknown,
BitField(u8, &'a Encoding<'a>),
Pointer(&'a Encoding<'a>),
Array(usize, &'a Encoding<'a>),
Struct(&'a str, &'a [Encoding<'a>]),
Union(&'a str, &'a [Encoding<'a>]),
}
Expand description
An Objective-C type-encoding.
Can be retrieved in Objective-C for a type T
using the @encode(T)
directive.
NSLog(@"Encoding of NSException: %s", @encode(NSException));
The Display
implementation converts the Encoding
into its string representation, that the the @encode
directive would
return. This can be used conveniently through the to_string
method:
use objc2_encode::Encoding;
assert_eq!(Encoding::Int.to_string(), "i");
For more information on the string value of an encoding, see Apple’s documentation.
Examples
Comparing an encoding to a string from the Objective-C runtime:
use objc2_encode::Encoding;
assert!(Encoding::Array(10, &Encoding::FloatComplex).equivalent_to_str("[10jf]"));
Variants (Non-exhaustive)
This enum is marked as non-exhaustive
Char
A C char
. Corresponds to the c
code.
Short
A C short
. Corresponds to the s
code.
Int
A C int
. Corresponds to the i
code.
Long
A C long
. Corresponds to the l
code.
This is treated as a 32-bit quantity in 64-bit programs.
LongLong
A C long long
. Corresponds to the q
code.
UChar
A C unsigned char
. Corresponds to the C
code.
UShort
A C unsigned short
. Corresponds to the S
code.
UInt
A C unsigned int
. Corresponds to the I
code.
ULong
A C unsigned long
. Corresponds to the L
code.
ULongLong
A C unsigned long long
. Corresponds to the Q
code.
Float
A C float
. Corresponds to the f
code.
Double
A C double
. Corresponds to the d
code.
LongDouble
A C long double
. Corresponds to the D
code.
FloatComplex
A C float _Complex
. Corresponds to the jf
code.
DoubleComplex
A C _Complex
or double _Complex
. Corresponds to the jd
code.
LongDoubleComplex
A C long double _Complex
. Corresponds to the jD
code.
Bool
A C++ bool
/ C99 _Bool
. Corresponds to the B
code.
Void
A C void
. Corresponds to the v
code.
String
A C char *
. Corresponds to the *
code.
Object
An Objective-C object (id
). Corresponds to the @
code.
Block
An Objective-C block. Corresponds to the @?
code.
Class
An Objective-C class (Class
). Corresponds to the #
code.
Sel
An Objective-C selector (SEL
). Corresponds to the :
code.
Unknown
An unknown type. Corresponds to the ?
code.
This is usually used to encode functions.
BitField(u8, &'a Encoding<'a>)
A bitfield with the given number of bits, and the given type.
The type is not currently used, but may be in the future for better compatibility with Objective-C runtimes.
Corresponds to the b
num code.
Pointer(&'a Encoding<'a>)
A pointer to the given type.
Corresponds to the ^
type code.
Array(usize, &'a Encoding<'a>)
An array with the given length and type.
Corresponds to the [len type]
code.
Struct(&'a str, &'a [Encoding<'a>])
A struct with the given name and fields.
The order of the fields must match the order of the order in this.
It is not uncommon for the name to be "?"
.
Corresponds to the {name=fields...}
code.
Union(&'a str, &'a [Encoding<'a>])
A union with the given name and fields.
The order of the fields must match the order of the order in this.
Corresponds to the (name=fields...)
code.
Implementations
sourceimpl Encoding<'_>
impl Encoding<'_>
sourcepub const C_LONG: Self = {
// Alternative: `mem::size_of::() == 4`
// That would exactly match what `clang` does:
// https://github.com/llvm/llvm-project/blob/release/13.x/clang/lib/AST/ASTContext.cpp#L7245
if cfg!(any(target_pointer_width = "32", windows)) {
// @encode(long) = 'l'
Self::Long
} else {
// @encode(long) = 'q'
Self::LongLong
}
}
pub const C_LONG: Self = {
// Alternative: `mem::size_of::() == 4`
// That would exactly match what `clang` does:
// https://github.com/llvm/llvm-project/blob/release/13.x/clang/lib/AST/ASTContext.cpp#L7245
if cfg!(any(target_pointer_width = "32", windows)) {
// @encode(long) = 'l'
Self::Long
} else {
// @encode(long) = 'q'
Self::LongLong
}
}
sourcepub const C_U_LONG: Self = {
if cfg!(any(target_pointer_width = "32", windows)) {
// @encode(unsigned long) = 'L'
Encoding::ULong
} else {
// @encode(unsigned long) = 'Q'
Encoding::ULongLong
}
}
pub const C_U_LONG: Self = { if cfg!(any(target_pointer_width = "32", windows)) { // @encode(unsigned long) = 'L' Encoding::ULong } else { // @encode(unsigned long) = 'Q' Encoding::ULongLong } }
The encoding of c_ulong
.
See Encoding::C_LONG
for explanation.
sourcepub fn equivalent_to(&self, other: &Self) -> bool
pub fn equivalent_to(&self, other: &Self) -> bool
Check if one encoding is equivalent to another.
Currently, equivalence testing requires that the encodings are equal, except for qualifiers. This may be changed in the future to e.g. ignore struct names, to allow structs behind multiple pointers to be considered equivalent, or similar changes that may be required because of limitations in Objective-C compiler implementations.
For example, you should not rely on two equivalent encodings to have the same size or ABI - that is provided on a best-effort basis.
sourcepub fn equivalent_to_str(&self, s: &str) -> bool
pub fn equivalent_to_str(&self, s: &str) -> bool
Check if an encoding is equivalent to the given string representation.
See Encoding::equivalent_to
for details about the meaning of
“equivalence”.
sourcepub fn equivalent_to_start_of_str<'a>(&self, s: &'a str) -> Option<&'a str>
pub fn equivalent_to_start_of_str<'a>(&self, s: &'a str) -> Option<&'a str>
Check if an encoding is equivalent to the start of the given string representation.
If it is equivalent, the remaining part of the string is returned.
Otherwise this returns None
.
See Encoding::equivalent_to
for details about the meaning of
“equivalence”.
Trait Implementations
sourceimpl Display for Encoding<'_>
impl Display for Encoding<'_>
Formats this Encoding
in a similar way that the @encode
directive
would ordinarily do.
You should not rely on the output of this to be stable across versions. It may change if found to be required to be compatible with exisiting Objective-C compilers.
impl<'a> Copy for Encoding<'a>
impl<'a> Eq for Encoding<'a>
impl<'a> StructuralEq for Encoding<'a>
impl<'a> StructuralPartialEq for Encoding<'a>
Auto Trait Implementations
impl<'a> RefUnwindSafe for Encoding<'a>
impl<'a> Send for Encoding<'a>
impl<'a> Sync for Encoding<'a>
impl<'a> Unpin for Encoding<'a>
impl<'a> UnwindSafe for Encoding<'a>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more