Skip to content

Commit c496ec1

Browse files
authored
Merge pull request SSheldon#4 from madsmtm/objc-foundation-encoding
Make `objc_foundation` use new encoding types
2 parents 32a9de8 + 8bd3985 commit c496ec1

File tree

13 files changed

+43
-17
lines changed

13 files changed

+43
-17
lines changed

objc/src/declare.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ The following example demonstrates declaring a class named `MyNumber` that has
1010
one ivar, a `u32` named `_number` and a `number` method that returns it:
1111
1212
``` no_run
13-
# use objc::class;
13+
# use objc::{class, sel};
1414
# use objc::declare::ClassDecl;
1515
# use objc::runtime::{Class, Object, Sel};
1616
# fn main() {

objc/src/macros.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ To check for a class that may not exist, use `Class::get`.
66
77
# Example
88
``` no_run
9+
# use objc::class;
910
# fn main() {
1011
let cls = class!(NSObject);
1112
# }
@@ -30,6 +31,7 @@ Registers a selector, returning a `Sel`.
3031
3132
# Example
3233
```
34+
# use objc::sel;
3335
# fn main() {
3436
let sel = sel!(description);
3537
let sel = sel!(setObject:forKey:);
@@ -62,6 +64,7 @@ Variadic arguments are not currently supported.
6264
6365
# Example
6466
``` no_run
67+
# use objc::msg_send;
6568
# use objc::runtime::Object;
6669
# fn main() {
6770
# unsafe {

objc/src/message/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub unsafe trait Message {
9191
9292
# Example
9393
``` no_run
94-
# use objc::{class, msg_send};
94+
# use objc::{class, msg_send, sel};
9595
# use objc::runtime::{BOOL, Class, Object};
9696
# use objc::Message;
9797
# fn main() {

objc_foundation/Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,5 @@ exclude = [".gitignore"]
1414

1515
[dependencies]
1616
block = "0.1"
17-
# TODO: Refer to the crates in this repo with `path = "XYZ"`
18-
objc = "0.2.2"
19-
objc_id = "0.1"
17+
objc = { path = "../objc", version = "0.2.7" }
18+
objc_id = { path = "../objc_id", version = "0.1" }

objc_foundation/src/array.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use std::ops::{Index, Range};
44
use std::os::raw::c_void;
55

66
use objc::runtime::{Class, Object};
7+
use objc::{class, msg_send};
8+
use objc::{Encode, Encoding};
79
use objc_id::{Id, Owned, Ownership, ShareId, Shared};
810

911
use super::{INSCopying, INSFastEnumeration, INSMutableCopying, INSObject, NSEnumerator};
@@ -58,6 +60,11 @@ impl NSRange {
5860
}
5961
}
6062

63+
unsafe impl Encode for NSRange {
64+
const ENCODING: Encoding<'static> =
65+
Encoding::Struct("_NSRange", &[usize::ENCODING, usize::ENCODING]);
66+
}
67+
6168
unsafe fn from_refs<A>(refs: &[&A::Item]) -> Id<A>
6269
where
6370
A: INSArray,

objc_foundation/src/data.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::slice;
55

66
use super::{INSCopying, INSMutableCopying, INSObject, NSRange};
77
use block::{Block, ConcreteBlock};
8+
use objc::msg_send;
89
use objc_id::Id;
910

1011
pub trait INSData: INSObject {

objc_foundation/src/dictionary.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::ops::Index;
44
use std::ptr;
55

66
use objc::runtime::Class;
7+
use objc::{class, msg_send};
78
use objc_id::{Id, Owned, Ownership, ShareId};
89

910
use super::{INSCopying, INSFastEnumeration, INSObject, NSArray, NSEnumerator, NSSharedArray};

objc_foundation/src/enumerator.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::os::raw::c_ulong;
44
use std::ptr;
55
use std::slice;
66

7+
use objc::msg_send;
78
use objc::runtime::Object;
89
use objc_id::Id;
910

objc_foundation/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
#![crate_name = "objc_foundation"]
2-
#![crate_type = "lib"]
3-
4-
#[macro_use]
5-
extern crate objc;
62

73
pub use self::array::{
84
INSArray, INSMutableArray, NSArray, NSComparisonResult, NSMutableArray, NSMutableSharedArray,

objc_foundation/src/macros.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,17 @@ macro_rules! object_struct {
77

88
unsafe impl ::objc::Message for $name {}
99

10+
unsafe impl<'a> ::objc::Encode for &'a $name {
11+
const ENCODING: ::objc::Encoding<'static> = ::objc::Encoding::Object;
12+
}
13+
14+
unsafe impl<'a> ::objc::Encode for &'a mut $name {
15+
const ENCODING: ::objc::Encoding<'static> = ::objc::Encoding::Object;
16+
}
17+
1018
impl $crate::INSObject for $name {
1119
fn class() -> &'static ::objc::runtime::Class {
12-
class!($name)
20+
::objc::class!($name)
1321
}
1422
}
1523

@@ -50,5 +58,13 @@ macro_rules! object_impl {
5058
);
5159
($name:ident, $($t:ident),*) => (
5260
unsafe impl<$($t),*> ::objc::Message for $name<$($t),*> { }
61+
62+
unsafe impl<'a, $($t),*> ::objc::Encode for &'a $name<$($t),*> {
63+
const ENCODING: ::objc::Encoding<'static> = ::objc::Encoding::Object;
64+
}
65+
66+
unsafe impl<'a, $($t),*> ::objc::Encode for &'a mut $name<$($t),*> {
67+
const ENCODING: ::objc::Encoding<'static> = ::objc::Encoding::Object;
68+
}
5369
);
5470
}

objc_foundation/src/object.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::any::Any;
22

3+
use objc::msg_send;
34
use objc::runtime::{Class, BOOL, NO};
45
use objc::Message;
56
use objc_id::{Id, ShareId};

objc_foundation/src/string.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::os::raw::{c_char, c_void};
33
use std::slice;
44
use std::str;
55

6+
use objc::msg_send;
67
use objc_id::{Id, ShareId};
78

89
use super::INSObject;

objc_foundation/src/value.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use std::os::raw::{c_char, c_void};
66
use std::str;
77

88
use objc::runtime::Class;
9-
use objc::{Encode, Encoding};
9+
use objc::Encode;
10+
use objc::{class, msg_send};
1011
use objc_id::Id;
1112

1213
use super::{INSCopying, INSObject};
@@ -15,7 +16,7 @@ pub trait INSValue: INSObject {
1516
type Value: 'static + Copy + Encode;
1617

1718
fn value(&self) -> Self::Value {
18-
assert!(Self::Value::encode() == self.encoding());
19+
assert!(&Self::Value::ENCODING == self.encoding());
1920
let mut value = MaybeUninit::<Self::Value>::uninit();
2021
let ptr = value.as_mut_ptr() as *mut c_void;
2122
unsafe {
@@ -24,20 +25,19 @@ pub trait INSValue: INSObject {
2425
}
2526
}
2627

27-
fn encoding(&self) -> Encoding {
28+
fn encoding(&self) -> &str {
2829
unsafe {
2930
let result: *const c_char = msg_send![self, objCType];
3031
let s = CStr::from_ptr(result);
31-
let s = str::from_utf8(s.to_bytes()).unwrap();
32-
Encoding::from_str(s)
32+
str::from_utf8(s.to_bytes()).unwrap()
3333
}
3434
}
3535

3636
fn from_value(value: Self::Value) -> Id<Self> {
3737
let cls = Self::class();
3838
let value_ptr: *const Self::Value = &value;
3939
let bytes = value_ptr as *const c_void;
40-
let encoding = CString::new(Self::Value::encode().as_str()).unwrap();
40+
let encoding = CString::new(Self::Value::ENCODING.to_string()).unwrap();
4141
unsafe {
4242
let obj: *mut Self = msg_send![cls, alloc];
4343
let obj: *mut Self = msg_send![obj, initWithBytes:bytes
@@ -85,6 +85,6 @@ mod tests {
8585
fn test_value() {
8686
let val = NSValue::from_value(13u32);
8787
assert!(val.value() == 13);
88-
assert!(u32::encode() == val.encoding());
88+
assert!(&u32::ENCODING == val.encoding());
8989
}
9090
}

0 commit comments

Comments
 (0)