|
1 |
| -# `objc-encode` |
| 1 | +# `objc-encode` - Objective-C type-encoding in Rust |
2 | 2 |
|
3 | 3 | [](https://crates.io/crates/objc-encode)
|
4 | 4 | [](../LICENSE.txt)
|
5 | 5 | [](https://docs.rs/objc-encode/)
|
6 | 6 | [](https://github.com/madsmtm/objc/actions)
|
7 | 7 |
|
8 |
| -Objective-C type encoding creation and parsing in Rust. |
| 8 | +The Objective-C directive `@encode` encodes types as strings for usage in |
| 9 | +various places in the runtime. |
9 | 10 |
|
10 |
| -The Objective-C compiler encodes types as strings for usage in the runtime. |
11 |
| -This crate aims to provide a strongly-typed (rather than stringly-typed) way |
12 |
| -to create and describe these type encodings without memory allocation in Rust. |
| 11 | +This crate provides the `Encoding` type to describe and compare these |
| 12 | +type-encodings without memory allocation. |
13 | 13 |
|
| 14 | +Additionally it provides traits for annotating types that has a corresponding |
| 15 | +Objective-C encoding, respectively `Encode` for structs and `RefEncode` for |
| 16 | +references (and `EncodeArguments` for function arguments). |
14 | 17 |
|
15 |
| -## Implementing Encode |
| 18 | +These types are exported under the `objc` crate as well, so usually you would |
| 19 | +just use that crate. |
16 | 20 |
|
17 |
| -This crate declares an `Encode` trait that can be implemented for types that |
18 |
| -the Objective-C compiler can encode. Implementing this trait looks like: |
| 21 | +# Examples |
19 | 22 |
|
20 |
| -```rust |
21 |
| -use objc::{Encode, Encoding}; |
22 |
| - |
23 |
| -#[cfg(target_pointer_width = "32")] |
24 |
| -type CGFloat = f32; |
| 23 | +Implementing `Encode` and `RefEncode`: |
25 | 24 |
|
26 |
| -#[cfg(target_pointer_width = "64")] |
27 |
| -type CGFloat = f64; |
| 25 | +```rust |
| 26 | +use objc_encode::{Encode, Encoding, RefEncode}; |
28 | 27 |
|
29 | 28 | #[repr(C)]
|
30 |
| -struct CGPoint { |
31 |
| - x: CGFloat, |
32 |
| - y: CGFloat, |
| 29 | +struct MyObject { |
| 30 | + a: f32, |
| 31 | + b: bool, |
33 | 32 | }
|
34 | 33 |
|
35 |
| -unsafe impl Encode for CGPoint { |
36 |
| - const ENCODING: Encoding<'static> = |
37 |
| - Encoding::Struct("CGPoint", &[CGFloat::ENCODING, CGFloat::ENCODING]); |
| 34 | +unsafe impl Encode for MyObject { |
| 35 | + const ENCODING: Encoding<'static> = Encoding::Struct( |
| 36 | + "MyObject", |
| 37 | + &[f32::ENCODING, bool::ENCODING |
| 38 | + ]); |
38 | 39 | }
|
39 |
| -``` |
40 | 40 |
|
41 |
| -For an example of how this works with more complex types, like structs |
42 |
| -containing structs, see the `core_graphics` example. |
| 41 | +assert_eq!(&MyObject::ENCODING, "{MyObject=fB}"); |
43 | 42 |
|
44 |
| -## Comparing with encoding strings |
| 43 | +unsafe impl RefEncode for MyObject { |
| 44 | + const ENCODING_REF: Encoding<'static> = Encoding::Pointer(&Self::ENCODING); |
| 45 | +} |
| 46 | + |
| 47 | +assert_eq!(&MyObject::ENCODING_REF, "^{MyObject=fB}"); |
| 48 | +``` |
45 | 49 |
|
46 | 50 | An `Encoding` can be compared with an encoding string from the Objective-C
|
47 | 51 | runtime:
|
48 | 52 |
|
49 | 53 | ```rust
|
50 | 54 | use objc::Encode;
|
51 |
| - |
52 | 55 | assert!(&i32::ENCODING == "i");
|
53 | 56 | ```
|
54 | 57 |
|
55 |
| -## Generating encoding strings |
56 |
| - |
57 |
| -Every `Encoding` implements `Display` as its string representation. |
58 |
| -This can be generated conveniently through the `to_string` method: |
| 58 | +`Encoding` implements `Display` as its string representation. This can be |
| 59 | +generated conveniently through the `to_string` method: |
59 | 60 |
|
60 | 61 | ```rust
|
61 | 62 | use objc::Encode;
|
62 |
| - |
63 | 63 | assert_eq!(i32::ENCODING.to_string(), "i");
|
64 | 64 | ```
|
| 65 | + |
| 66 | +See the [`examples`] folder for more complex usage. |
| 67 | + |
| 68 | +# Installation |
| 69 | + |
| 70 | +```toml |
| 71 | +[dependencies] |
| 72 | +objc-encode = "1.1.0" |
| 73 | +``` |
| 74 | + |
| 75 | +# License |
| 76 | + |
| 77 | +This project is licensed under the MIT license, see [`../LICENSE.txt`]. |
| 78 | + |
| 79 | +Work is in progress to make it dual-licensed under the Apache License |
| 80 | +(Version 2.0) as well. |
| 81 | + |
| 82 | +[`examples`]: https://github.com/madsmtm/objc/tree/master/objc_encode/examples |
| 83 | +[`../LICENSE.txt`]: https://github.com/madsmtm/objc/blob/master/LICENSE.txt |
0 commit comments