Skip to content

Commit 1600f6e

Browse files
committed
Update objc_encode documentation to mention new RefEncode type
Also moves the primary documentation into the README.md
1 parent b3ecc1e commit 1600f6e

File tree

3 files changed

+54
-76
lines changed

3 files changed

+54
-76
lines changed

objc_encode/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
[package]
22
name = "objc-encode"
3-
version = "1.1.0" # Remember to update html_root_url in lib.rs
3+
# Remember to update html_root_url in lib.rs and README.md
4+
version = "1.1.0"
45
authors = ["Steven Sheldon", "Mads Marquart <[email protected]>"]
56
edition = "2018"
67

7-
description = "Objective-C type encoding creation and parsing."
8+
description = "Objective-C type-encodings"
89
keywords = ["objective-c", "macos", "ios", "encode"]
910
categories = [
1011
"development-tools::ffi",

objc_encode/README.md

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,83 @@
1-
# `objc-encode`
1+
# `objc-encode` - Objective-C type-encoding in Rust
22

33
[![Latest version](https://badgen.net/crates/v/objc-encode)](https://crates.io/crates/objc-encode)
44
[![License](https://badgen.net/badge/license/MIT/blue)](../LICENSE.txt)
55
[![Documentation](https://docs.rs/objc-encode/badge.svg)](https://docs.rs/objc-encode/)
66
[![CI Status](https://github.com/madsmtm/objc/workflows/CI/badge.svg)](https://github.com/madsmtm/objc/actions)
77

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

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

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).
1417

15-
## Implementing Encode
18+
These types are exported under the `objc` crate as well, so usually you would
19+
just use that crate.
1620

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
1922

20-
```rust
21-
use objc::{Encode, Encoding};
22-
23-
#[cfg(target_pointer_width = "32")]
24-
type CGFloat = f32;
23+
Implementing `Encode` and `RefEncode`:
2524

26-
#[cfg(target_pointer_width = "64")]
27-
type CGFloat = f64;
25+
```rust
26+
use objc_encode::{Encode, Encoding, RefEncode};
2827

2928
#[repr(C)]
30-
struct CGPoint {
31-
x: CGFloat,
32-
y: CGFloat,
29+
struct MyObject {
30+
a: f32,
31+
b: bool,
3332
}
3433

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+
]);
3839
}
39-
```
4040

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}");
4342

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+
```
4549

4650
An `Encoding` can be compared with an encoding string from the Objective-C
4751
runtime:
4852

4953
```rust
5054
use objc::Encode;
51-
5255
assert!(&i32::ENCODING == "i");
5356
```
5457

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:
5960

6061
```rust
6162
use objc::Encode;
62-
6363
assert_eq!(i32::ENCODING.to_string(), "i");
6464
```
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

objc_encode/src/lib.rs

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,4 @@
1-
/*!
2-
Objective-C type encoding creation and parsing in Rust.
3-
4-
The Objective-C compiler encodes types as strings for usage in the runtime.
5-
This crate aims to provide a strongly-typed (rather than stringly-typed) way
6-
to create and describe these type encodings without memory allocation in Rust.
7-
8-
# Implementing Encode
9-
10-
This crate declares an [`Encode`] trait that can be implemented for types that
11-
the Objective-C compiler can encode. Implementing this trait looks like:
12-
13-
```ignore
14-
unsafe impl Encode for CGPoint {
15-
const ENCODING: Encoding<'static> =
16-
Encoding::Struct("CGPoint", &[CGFloat::ENCODING, CGFLOAT::ENCODING]);
17-
}
18-
```
19-
20-
For an example of how this works with more complex types, like structs
21-
containing structs, see the `core_graphics` example.
22-
23-
# Comparing with encoding strings
24-
25-
An [`Encoding`] can be compared with an encoding string from the Objective-C
26-
runtime:
27-
28-
```
29-
# use objc::Encode;
30-
assert!(&i32::ENCODING == "i");
31-
```
32-
33-
# Generating encoding strings
34-
35-
Every [`Encoding`] implements [`Display`][`core::fmt::Display`] as its string
36-
representation. This can be generated conveniently through the
37-
[`to_string`][`alloc::string::ToString::to_string`] method:
38-
39-
```
40-
# use objc::Encode;
41-
assert_eq!(i32::ENCODING.to_string(), "i");
42-
```
43-
*/
1+
//! # Objective-C type-encoding
442
453
#![no_std]
464
#![warn(missing_docs)]

0 commit comments

Comments
 (0)