Enum objc2_encode::Encoding

source ·
#[non_exhaustive]
pub enum Encoding {
Show 31 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, Option<&'static (u64, Encoding)>), Pointer(&'static Encoding), Atomic(&'static Encoding), Array(u64, &'static Encoding), Struct(&'static str, &'static [Encoding]), Union(&'static str, &'static [Encoding]), None,
}
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
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

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, see Encoding::C_LONG.

§

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.

See Encoding::C_ULONG.

§

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 "j" "f" code.

§

DoubleComplex

A C _Complex or double _Complex. Corresponds to the "j" "d" code.

§

LongDoubleComplex

A C long double _Complex. Corresponds to the "j" "D" 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.

Some compilers may choose to store the name of the class in instance variables and properties as "@" class_name, see Extended Type Info in Objective-C (note that this does not include generics).

Such class names are currently ignored by objc2-encode.

§

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, Option<&'static (u64, Encoding)>)

A bitfield with the given number of bits, and the given type.

Corresponds to the "b" size code.

On GNUStep, this uses the "b" offset type size code, so this contains an Option that should be set for that. Only integral types are possible for the type.

A BitField(_, Some(_)) and a BitField(_, None) do not compare equal; instead, you should set the bitfield correctly depending on the target platform.

§

Pointer(&'static Encoding)

A pointer to the given type.

Corresponds to the "^" type code.

§

Atomic(&'static Encoding)

A C11 _Atomic type.

Corresponds to the "A" type code. Not all encodings are possible in this.

§

Array(u64, &'static Encoding)

An array with the given length and type.

Corresponds to the "[" length type "]" code.

§

Struct(&'static str, &'static [Encoding])

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.

Note that the = may be omitted in some situations; this is considered equal to the case where there are no fields.

§

Union(&'static str, &'static [Encoding])

A union with the given name and members.

The order of the members must match the order of the order in this.

Corresponds to the "(" name "=" members... ")" code.

Note that the = may be omitted in some situations; this is considered equal to the case where there are no members.

§

None

The type does not have an Objective-C encoding.

This is usually only used on types where Clang fails to generate the Objective-C encoding, like SIMD types marked with __attribute__((__ext_vector_type__(1))).

Implementations§

source§

impl Encoding

source

pub const C_LONG: Self = _

The encoding of c_long on the current target.

Ideally the encoding of long would be the same as int when it’s 32 bits wide and the same as long long when it is 64 bits wide; then c_long::ENCODING would just work.

Unfortunately, long have a different encoding than int when it is 32 bits wide; the l encoding.

source

pub const C_ULONG: Self = _

The encoding of c_ulong on the current target.

See Encoding::C_LONG for explanation.

source

pub fn equivalent_to(&self, other: &Self) -> bool

Check if one encoding is equivalent to another.

Currently, equivalence testing mostly requires that the encodings are equal, except for:

  • Any leading qualifiers that the encoding may have.
  • Structs or unions behind multiple pointers are considered equivalent, since Objective-C compilers strip this information to avoid unnecessary nesting.
  • Structs or unions with no fields/members are considered to represent “opqaue” types, and will therefore be equivalent to all other structs / unions.

The comparison may be changed in the future to e.g. ignore struct names 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.

source

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”.

source

pub fn equivalent_to_box(&self, other: &EncodingBox) -> bool

Check if an encoding is equivalent to a boxed encoding.

See Encoding::equivalent_to for details about the meaning of “equivalence”.

Trait Implementations§

source§

impl Clone for Encoding

source§

fn clone(&self) -> Encoding

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Encoding

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

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.

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Hash for Encoding

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq<Encoding> for EncodingBox

source§

fn eq(&self, other: &Encoding) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<EncodingBox> for Encoding

source§

fn eq(&self, other: &EncodingBox) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq for Encoding

source§

fn eq(&self, other: &Encoding) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for Encoding

source§

impl StructuralPartialEq for Encoding

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.