Skip to content

Commit a021c4c

Browse files
authored
Merge pull request #1 from madsmtm/workspace
Move crates into a cargo workspace
2 parents c8696b0 + 8fc1393 commit a021c4c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+2698
-34
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
doc/
21
target/
32
Cargo.lock
43

Cargo.toml

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,8 @@
1-
[package]
2-
name = "objc"
3-
version = "0.2.7"
4-
authors = ["Steven Sheldon"]
5-
edition = "2018"
6-
7-
description = "Objective-C Runtime bindings and wrapper for Rust."
8-
keywords = ["objective-c", "osx", "ios", "cocoa", "uikit"]
9-
readme = "README.md"
10-
repository = "http://github.com/SSheldon/rust-objc"
11-
documentation = "http://ssheldon.github.io/rust-objc/objc/"
12-
license = "MIT"
13-
14-
exclude = [
15-
".gitignore",
16-
".travis.yml",
17-
"doc.sh",
18-
"travis_install.sh",
19-
"travis_test.sh",
20-
"tests-ios/**",
21-
]
22-
23-
[features]
24-
exception = ["objc_exception"]
25-
verify_message = []
26-
27-
[dependencies]
28-
malloc_buf = "1.0"
29-
objc-encode = "1.0"
30-
31-
[dependencies.objc_exception]
32-
version = "0.1"
33-
optional = true
1+
[workspace]
2+
members = [
3+
"objc",
4+
"objc_encode",
5+
"objc_exception",
6+
"objc_foundation",
7+
"objc_id",
8+
]
File renamed without changes.
File renamed without changes.

objc/Cargo.toml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[package]
2+
name = "objc"
3+
version = "0.2.7"
4+
authors = ["Steven Sheldon"]
5+
edition = "2018"
6+
7+
description = "Objective-C Runtime bindings and wrapper for Rust."
8+
keywords = ["objective-c", "osx", "ios", "cocoa", "uikit"]
9+
readme = "README.md"
10+
repository = "http://github.com/SSheldon/rust-objc"
11+
documentation = "http://ssheldon.github.io/rust-objc/objc/"
12+
license = "MIT"
13+
14+
exclude = [
15+
".gitignore",
16+
".travis.yml",
17+
"doc.sh",
18+
"travis_install.sh",
19+
"travis_test.sh",
20+
"tests-ios/**",
21+
]
22+
23+
[features]
24+
exception = ["objc_exception"]
25+
verify_message = []
26+
27+
[dependencies]
28+
malloc_buf = "1.0"
29+
objc-encode = { path = "../objc_encode", version = "1.0" }
30+
objc_exception = { path = "../objc_exception", version = "0.1", optional = true }
File renamed without changes.

doc.sh renamed to objc/doc.sh

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

objc_encode/Cargo.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "objc-encode"
3+
version = "1.1.0"
4+
authors = ["Steven Sheldon"]
5+
edition = "2018"
6+
7+
description = "Objective-C type encoding creation and parsing in Rust."
8+
keywords = ["objective-c", "osx", "ios", "cocoa", "uikit"]
9+
categories = ["development-tools::ffi", "no-std"]
10+
readme = "README.md"
11+
repository = "http://github.com/SSheldon/rust-objc-encode"
12+
documentation = "http://ssheldon.github.io/rust-objc/objc_encode/"
13+
license = "MIT"
14+
15+
exclude = [
16+
".gitignore",
17+
]

objc_encode/README.md

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

objc_encode/examples/core_graphics.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
extern crate objc_encode;
2+
3+
use objc_encode::{Encode, Encoding};
4+
5+
#[cfg(target_pointer_width = "32")]
6+
type CGFloat = f32;
7+
8+
#[cfg(target_pointer_width = "64")]
9+
type CGFloat = f64;
10+
11+
#[repr(C)]
12+
struct CGPoint {
13+
x: CGFloat,
14+
y: CGFloat,
15+
}
16+
17+
unsafe impl Encode for CGPoint {
18+
const ENCODING: Encoding<'static> =
19+
Encoding::Struct("CGPoint", &[CGFloat::ENCODING, CGFloat::ENCODING]);
20+
}
21+
22+
#[repr(C)]
23+
struct CGSize {
24+
width: CGFloat,
25+
height: CGFloat,
26+
}
27+
28+
unsafe impl Encode for CGSize {
29+
const ENCODING: Encoding<'static> =
30+
Encoding::Struct("CGSize", &[CGFloat::ENCODING, CGFloat::ENCODING]);
31+
}
32+
33+
#[repr(C)]
34+
struct CGRect {
35+
origin: CGPoint,
36+
size: CGSize,
37+
}
38+
39+
unsafe impl Encode for CGRect {
40+
const ENCODING: Encoding<'static> =
41+
Encoding::Struct("CGRect", &[CGPoint::ENCODING, CGSize::ENCODING]);
42+
}
43+
44+
fn main() {
45+
println!("{}", CGRect::ENCODING);
46+
}

objc_encode/src/encode.rs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
use core::ffi::c_void;
2+
3+
use crate::Encoding;
4+
5+
/// Types that have an Objective-C type encoding.
6+
///
7+
/// Unsafe because Objective-C will make assumptions about the type (like its
8+
/// size and alignment) from its encoding, so the implementer must verify that
9+
/// the encoding is accurate.
10+
pub unsafe trait Encode {
11+
/// Returns the Objective-C type encoding for Self.
12+
const ENCODING: Encoding<'static>;
13+
}
14+
15+
macro_rules! encode_impls {
16+
($($t:ty : $e:ident,)*) => ($(
17+
unsafe impl Encode for $t {
18+
const ENCODING: Encoding<'static> = Encoding::$e;
19+
}
20+
)*);
21+
}
22+
23+
encode_impls!(
24+
i8: Char,
25+
i16: Short,
26+
i32: Int,
27+
i64: LongLong,
28+
u8: UChar,
29+
u16: UShort,
30+
u32: UInt,
31+
u64: ULongLong,
32+
f32: Float,
33+
f64: Double,
34+
bool: Bool,
35+
(): Void,
36+
*mut i8: String,
37+
*const i8: String,
38+
*mut u8: String,
39+
*const u8: String,
40+
);
41+
42+
unsafe impl Encode for isize {
43+
#[cfg(target_pointer_width = "32")]
44+
const ENCODING: Encoding<'static> = i32::ENCODING;
45+
46+
#[cfg(target_pointer_width = "64")]
47+
const ENCODING: Encoding<'static> = i64::ENCODING;
48+
}
49+
50+
unsafe impl Encode for usize {
51+
#[cfg(target_pointer_width = "32")]
52+
const ENCODING: Encoding<'static> = u32::ENCODING;
53+
54+
#[cfg(target_pointer_width = "64")]
55+
const ENCODING: Encoding<'static> = u64::ENCODING;
56+
}
57+
58+
unsafe impl Encode for *mut c_void {
59+
const ENCODING: Encoding<'static> = Encoding::Pointer(&Encoding::Void);
60+
}
61+
62+
unsafe impl Encode for *const c_void {
63+
const ENCODING: Encoding<'static> = Encoding::Pointer(&Encoding::Void);
64+
}
65+
66+
// TODO: Replace this with a const generics implementation when they stabilise (#44580)
67+
macro_rules! slice_encode_impl {
68+
($($n:literal),* $(,)?) => {
69+
$(
70+
unsafe impl<T: Encode> Encode for [T; $n] {
71+
const ENCODING: Encoding<'static> = Encoding::Array($n, &<T as Encode>::ENCODING);
72+
}
73+
)*
74+
};
75+
}
76+
77+
slice_encode_impl!(
78+
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
79+
27, 28, 29, 30, 31, 32
80+
);
81+
82+
/*
83+
External crates cannot implement Encode for pointers or Optionals, but they
84+
*can* implement it for references. rust-lang/rust#25126
85+
86+
As a workaround, we provide implementations for these types that return the
87+
same encoding as references.
88+
*/
89+
unsafe impl<T> Encode for *const T where for<'b> &'b T: Encode {
90+
const ENCODING: Encoding<'static> = <&T>::ENCODING;
91+
}
92+
93+
unsafe impl<T> Encode for *mut T where for<'b> &'b mut T: Encode {
94+
const ENCODING: Encoding<'static> = <&mut T>::ENCODING;
95+
}
96+
97+
unsafe impl<'a, T> Encode for Option<&'a T> where for<'b> &'b T: Encode {
98+
const ENCODING: Encoding<'static> = <&T>::ENCODING;
99+
}
100+
101+
unsafe impl<'a, T> Encode for Option<&'a mut T> where for<'b> &'b mut T: Encode {
102+
const ENCODING: Encoding<'static> = <&mut T>::ENCODING;
103+
}

0 commit comments

Comments
 (0)