Skip to content

Commit 1e01ca4

Browse files
committed
Implement Encode for most types
Using new objc-encode crate
1 parent 48d510a commit 1e01ca4

File tree

19 files changed

+278
-27
lines changed

19 files changed

+278
-27
lines changed

cocoa-foundation/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@ libc = "0.2"
1818
core-foundation = { path = "../core-foundation", version = "0.9" }
1919
core-graphics-types = { path = "../core-graphics-types", version = "0.1" }
2020
foreign-types = "0.3"
21-
objc = "0.2.3"
21+
# Current `master` of objc
22+
objc = { version = "=0.3.0-alpha.0", package = "objc2" }
23+
objc-encode = "1.1.0"

cocoa-foundation/src/foundation.rs

Lines changed: 64 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::os::raw::c_void;
1414
use base::{id, BOOL, NO, SEL, nil};
1515
use block::Block;
1616
use libc;
17+
use objc_encode::{Encode, Encoding};
1718

1819

1920
#[cfg(target_pointer_width = "32")]
@@ -37,7 +38,7 @@ mod macos {
3738
use base::id;
3839
use core_graphics_types::base::CGFloat;
3940
use core_graphics_types::geometry::CGRect;
40-
use objc;
41+
use objc_encode::{Encode, Encoding};
4142

4243
#[repr(C)]
4344
#[derive(Copy, Clone)]
@@ -56,13 +57,9 @@ mod macos {
5657
}
5758
}
5859

59-
unsafe impl objc::Encode for NSPoint {
60-
fn encode() -> objc::Encoding {
61-
let encoding = format!("{{CGPoint={}{}}}",
62-
CGFloat::encode().as_str(),
63-
CGFloat::encode().as_str());
64-
unsafe { objc::Encoding::from_str(&encoding) }
65-
}
60+
unsafe impl Encode for NSPoint {
61+
const ENCODING: Encoding<'static> =
62+
Encoding::Struct("CGPoint", &[CGFloat::ENCODING, CGFloat::ENCODING]);
6663
}
6764

6865
#[repr(C)]
@@ -82,13 +79,9 @@ mod macos {
8279
}
8380
}
8481

85-
unsafe impl objc::Encode for NSSize {
86-
fn encode() -> objc::Encoding {
87-
let encoding = format!("{{CGSize={}{}}}",
88-
CGFloat::encode().as_str(),
89-
CGFloat::encode().as_str());
90-
unsafe { objc::Encoding::from_str(&encoding) }
91-
}
82+
unsafe impl Encode for NSSize {
83+
const ENCODING: Encoding<'static> =
84+
Encoding::Struct("CGSize", &[CGFloat::ENCODING, CGFloat::ENCODING]);
9285
}
9386

9487
#[repr(C)]
@@ -122,13 +115,9 @@ mod macos {
122115
}
123116
}
124117

125-
unsafe impl objc::Encode for NSRect {
126-
fn encode() -> objc::Encoding {
127-
let encoding = format!("{{CGRect={}{}}}",
128-
NSPoint::encode().as_str(),
129-
NSSize::encode().as_str());
130-
unsafe { objc::Encoding::from_str(&encoding) }
131-
}
118+
unsafe impl Encode for NSRect {
119+
const ENCODING: Encoding<'static> =
120+
Encoding::Struct("CGRect", &[NSPoint::ENCODING, NSSize::ENCODING]);
132121
}
133122

134123
// Same as CGRectEdge
@@ -140,6 +129,8 @@ mod macos {
140129
NSRectMaxYEdge,
141130
}
142131

132+
impl_Encode!(NSRectEdge, u32);
133+
143134
#[link(name = "Foundation", kind = "framework")]
144135
extern {
145136
fn NSInsetRect(rect: NSRect, x: CGFloat, y: CGFloat) -> NSRect;
@@ -169,6 +160,11 @@ pub struct NSRange {
169160
pub length: NSUInteger,
170161
}
171162

163+
unsafe impl Encode for NSRange {
164+
const ENCODING: Encoding<'static> =
165+
Encoding::Struct("_NSRange", &[NSUInteger::ENCODING, NSUInteger::ENCODING]);
166+
}
167+
172168
impl NSRange {
173169
#[inline]
174170
pub fn new(location: NSUInteger, length: NSUInteger) -> NSRange {
@@ -212,6 +208,17 @@ pub struct NSOperatingSystemVersion {
212208
pub patchVersion: NSUInteger,
213209
}
214210

211+
unsafe impl Encode for NSOperatingSystemVersion {
212+
const ENCODING: Encoding<'static> = Encoding::Struct(
213+
"NSOperatingSystemVersion",
214+
&[
215+
NSUInteger::ENCODING,
216+
NSUInteger::ENCODING,
217+
NSUInteger::ENCODING,
218+
],
219+
);
220+
}
221+
215222
impl NSOperatingSystemVersion {
216223
#[inline]
217224
pub fn new(majorVersion: NSUInteger, minorVersion: NSUInteger, patchVersion: NSUInteger) -> NSOperatingSystemVersion {
@@ -593,6 +600,8 @@ bitflags! {
593600
}
594601
}
595602

603+
impl_Encode!(NSEnumerationOptions, libc::c_ulonglong);
604+
596605
pub type NSComparator = *mut Block<(id, id), NSComparisonResult>;
597606

598607
#[repr(isize)]
@@ -603,6 +612,8 @@ pub enum NSComparisonResult {
603612
NSOrderedDescending = 1
604613
}
605614

615+
impl_Encode!(NSComparisonResult, isize);
616+
606617
pub trait NSString: Sized {
607618
unsafe fn alloc(_: Self) -> id {
608619
msg_send![class!(NSString), alloc]
@@ -669,6 +680,23 @@ struct NSFastEnumerationState {
669680
pub extra: [libc::c_ulong; 5]
670681
}
671682

683+
unsafe impl Encode for NSFastEnumerationState {
684+
const ENCODING: Encoding<'static> = Encoding::Struct(
685+
"?",
686+
&[
687+
libc::c_ulong::ENCODING,
688+
Encoding::Pointer(&Encoding::Object),
689+
Encoding::Pointer(&libc::c_ulong::ENCODING),
690+
Encoding::Array(5, &libc::c_ulong::ENCODING),
691+
],
692+
);
693+
}
694+
695+
unsafe impl Encode for &'_ NSFastEnumerationState {
696+
const ENCODING: Encoding<'static> =
697+
Encoding::Pointer(&NSFastEnumerationState::ENCODING);
698+
}
699+
672700
const NS_FAST_ENUM_BUF_SIZE: usize = 16;
673701

674702
pub struct NSFastIterator {
@@ -677,7 +705,7 @@ pub struct NSFastIterator {
677705
mut_val: Option<libc::c_ulong>,
678706
len: usize,
679707
idx: usize,
680-
object: id
708+
object: id,
681709
}
682710

683711
impl Iterator for NSFastIterator {
@@ -774,6 +802,8 @@ bitflags! {
774802
}
775803
}
776804

805+
impl_Encode!(NSURLBookmarkCreationOptions, NSUInteger);
806+
777807
pub type NSURLBookmarkFileCreationOptions = NSURLBookmarkCreationOptions;
778808

779809
bitflags! {
@@ -784,6 +814,7 @@ bitflags! {
784814
}
785815
}
786816

817+
impl_Encode!(NSURLBookmarkResolutionOptions, NSUInteger);
787818

788819
pub trait NSURL: Sized {
789820
unsafe fn alloc(_: Self) -> id;
@@ -1337,6 +1368,8 @@ bitflags! {
13371368
}
13381369
}
13391370

1371+
impl_Encode!(NSDataReadingOptions, libc::c_ulonglong);
1372+
13401373
bitflags! {
13411374
pub struct NSDataBase64EncodingOptions: libc::c_ulonglong {
13421375
const NSDataBase64Encoding64CharacterLineLength = 1 << 0;
@@ -1346,26 +1379,34 @@ bitflags! {
13461379
}
13471380
}
13481381

1382+
impl_Encode!(NSDataBase64EncodingOptions, libc::c_ulonglong);
1383+
13491384
bitflags! {
13501385
pub struct NSDataBase64DecodingOptions: libc::c_ulonglong {
13511386
const NSDataBase64DecodingIgnoreUnknownCharacters = 1 << 0;
13521387
}
13531388
}
13541389

1390+
impl_Encode!(NSDataBase64DecodingOptions, libc::c_ulonglong);
1391+
13551392
bitflags! {
13561393
pub struct NSDataWritingOptions: libc::c_ulonglong {
13571394
const NSDataWritingAtomic = 1 << 0;
13581395
const NSDataWritingWithoutOverwriting = 1 << 1;
13591396
}
13601397
}
13611398

1399+
impl_Encode!(NSDataWritingOptions, libc::c_ulonglong);
1400+
13621401
bitflags! {
13631402
pub struct NSDataSearchOptions: libc::c_ulonglong {
13641403
const NSDataSearchBackwards = 1 << 0;
13651404
const NSDataSearchAnchored = 1 << 1;
13661405
}
13671406
}
13681407

1408+
impl_Encode!(NSDataSearchOptions, libc::c_ulonglong);
1409+
13691410
pub trait NSUserDefaults {
13701411
unsafe fn standardUserDefaults() -> Self;
13711412

cocoa-foundation/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,20 @@ extern crate core_foundation;
1616
extern crate core_graphics_types;
1717
extern crate foreign_types;
1818
extern crate libc;
19+
pub extern crate objc_encode;
1920
#[macro_use]
2021
extern crate objc;
2122

23+
pub use objc_encode as __objc_encode;
24+
25+
#[macro_export]
26+
macro_rules! impl_Encode {
27+
($t:ty, $delegation:ty) => {
28+
unsafe impl $crate::__objc_encode::Encode for $t {
29+
const ENCODING: $crate::__objc_encode::Encoding<'static> = <$delegation>::ENCODING;
30+
}
31+
}
32+
}
33+
2234
pub mod base;
2335
pub mod foundation;

cocoa/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ cocoa-foundation = { path = "../cocoa-foundation", version = "0.1" }
1919
core-foundation = { path = "../core-foundation", version = "0.9" }
2020
core-graphics = { path = "../core-graphics", version = "0.22" }
2121
foreign-types = "0.3"
22-
objc = "0.2.3"
22+
# Current `master` of objc
23+
objc = { version = "=0.3.0-alpha.0", package = "objc2" }
24+
objc-encode = "1.1.0"

0 commit comments

Comments
 (0)