objc-encode 0.0.1

Objective-C type encoding creation and parsing in Rust.
docs.rs failed to build objc-encode-0.0.1
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Visit the last successful build: objc-encode-1.1.0

Objective-C type encoding creation and parsing in Rust.

The Objective-C compiler encodes types as strings for usage in the runtime. This crate aims to provide a strongly-typed (rather than stringly-typed) way to create and describe these type encodings without memory allocation in Rust.

Implementing Encode

This crate declares an Encode trait that can be implemented for types that the Objective-C compiler can encode. Implementing this trait looks like:

unsafe impl Encode for CGPoint {
    type Encoding = Struct<&'static str, (Primitive, Primitive)>;

    fn encode() -> Self::Encoding {
        Struct::new("CGPoint", (CGFloat::encode(), CGFloat::encode()))
    }
}

For an example of how this works with more complex types, like structs containing structs, see the core_graphics example.

Comparing with encoding strings

If you have an encoding string from the Objective-C runtime, it can be parsed and compared with another encoding through a StrEncoding:

let parsed = StrEncoding::from_str("i").unwrap();
assert!(i32::encode().eq_encoding(parsed));

Generating encoding strings

The string representation of an Encoding can be generated via its write method:

let mut result = String::new();
i32::encode().write(&mut result).unwrap();
assert_eq!(result, "i");

The encodings defined in this crate also implement Display for convenience, allowing the to_string method to be used:

assert_eq!(i32::encode().to_string(), "i");