Skip to content

Commit 38bd899

Browse files
committed
Implement Encode for most types
Using new objc-encode crate
1 parent 3b9868a commit 38bd899

File tree

20 files changed

+288
-36
lines changed

20 files changed

+288
-36
lines changed

cocoa-foundation/Cargo.toml

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

cocoa-foundation/src/foundation.rs

Lines changed: 63 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use base::{id, nil, BOOL, NO, SEL};
1313
use block::Block;
1414
use libc;
15+
use objc_encode::{Encode, Encoding};
1516
use std::os::raw::c_void;
1617
use std::ptr;
1718

@@ -35,7 +36,7 @@ mod macos {
3536
use base::id;
3637
use core_graphics_types::base::CGFloat;
3738
use core_graphics_types::geometry::CGRect;
38-
use objc;
39+
use objc_encode::{Encode, Encoding};
3940
use std::mem;
4041

4142
#[repr(C)]
@@ -52,15 +53,9 @@ mod macos {
5253
}
5354
}
5455

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

6661
#[repr(C)]
@@ -80,15 +75,9 @@ mod macos {
8075
}
8176
}
8277

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

9483
#[repr(C)]
@@ -118,15 +107,9 @@ mod macos {
118107
}
119108
}
120109

121-
unsafe impl objc::Encode for NSRect {
122-
fn encode() -> objc::Encoding {
123-
let encoding = format!(
124-
"{{CGRect={}{}}}",
125-
NSPoint::encode().as_str(),
126-
NSSize::encode().as_str()
127-
);
128-
unsafe { objc::Encoding::from_str(&encoding) }
129-
}
110+
unsafe impl Encode for NSRect {
111+
const ENCODING: Encoding<'static> =
112+
Encoding::Struct("CGRect", &[NSPoint::ENCODING, NSSize::ENCODING]);
130113
}
131114

132115
// Same as CGRectEdge
@@ -138,6 +121,8 @@ mod macos {
138121
NSRectMaxYEdge,
139122
}
140123

124+
impl_Encode!(NSRectEdge, u32);
125+
141126
#[link(name = "Foundation", kind = "framework")]
142127
extern "C" {
143128
fn NSInsetRect(rect: NSRect, x: CGFloat, y: CGFloat) -> NSRect;
@@ -166,6 +151,11 @@ pub struct NSRange {
166151
pub length: NSUInteger,
167152
}
168153

154+
unsafe impl Encode for NSRange {
155+
const ENCODING: Encoding<'static> =
156+
Encoding::Struct("_NSRange", &[NSUInteger::ENCODING, NSUInteger::ENCODING]);
157+
}
158+
169159
impl NSRange {
170160
#[inline]
171161
pub fn new(location: NSUInteger, length: NSUInteger) -> NSRange {
@@ -208,6 +198,17 @@ pub struct NSOperatingSystemVersion {
208198
pub patchVersion: NSUInteger,
209199
}
210200

201+
unsafe impl Encode for NSOperatingSystemVersion {
202+
const ENCODING: Encoding<'static> = Encoding::Struct(
203+
"NSOperatingSystemVersion",
204+
&[
205+
NSUInteger::ENCODING,
206+
NSUInteger::ENCODING,
207+
NSUInteger::ENCODING,
208+
],
209+
);
210+
}
211+
211212
impl NSOperatingSystemVersion {
212213
#[inline]
213214
pub fn new(
@@ -629,6 +630,8 @@ bitflags! {
629630
}
630631
}
631632

633+
impl_Encode!(NSEnumerationOptions, libc::c_ulonglong);
634+
632635
pub type NSComparator = *mut Block<(id, id), NSComparisonResult>;
633636

634637
#[repr(isize)]
@@ -639,6 +642,8 @@ pub enum NSComparisonResult {
639642
NSOrderedDescending = 1,
640643
}
641644

645+
impl_Encode!(NSComparisonResult, isize);
646+
642647
pub trait NSString: Sized {
643648
unsafe fn alloc(_: Self) -> id {
644649
msg_send![class!(NSString), alloc]
@@ -703,6 +708,22 @@ struct NSFastEnumerationState {
703708
pub extra: [libc::c_ulong; 5],
704709
}
705710

711+
unsafe impl Encode for NSFastEnumerationState {
712+
const ENCODING: Encoding<'static> = Encoding::Struct(
713+
"?",
714+
&[
715+
libc::c_ulong::ENCODING,
716+
Encoding::Pointer(&Encoding::Object),
717+
Encoding::Pointer(&libc::c_ulong::ENCODING),
718+
Encoding::Array(5, &libc::c_ulong::ENCODING),
719+
],
720+
);
721+
}
722+
723+
unsafe impl Encode for &'_ NSFastEnumerationState {
724+
const ENCODING: Encoding<'static> = Encoding::Pointer(&NSFastEnumerationState::ENCODING);
725+
}
726+
706727
const NS_FAST_ENUM_BUF_SIZE: usize = 16;
707728

708729
pub struct NSFastIterator {
@@ -811,6 +832,8 @@ bitflags! {
811832
}
812833
}
813834

835+
impl_Encode!(NSURLBookmarkCreationOptions, NSUInteger);
836+
814837
pub type NSURLBookmarkFileCreationOptions = NSURLBookmarkCreationOptions;
815838

816839
bitflags! {
@@ -821,6 +844,8 @@ bitflags! {
821844
}
822845
}
823846

847+
impl_Encode!(NSURLBookmarkResolutionOptions, NSUInteger);
848+
824849
pub trait NSURL: Sized {
825850
unsafe fn alloc(_: Self) -> id;
826851

@@ -1607,6 +1632,8 @@ bitflags! {
16071632
}
16081633
}
16091634

1635+
impl_Encode!(NSDataReadingOptions, libc::c_ulonglong);
1636+
16101637
bitflags! {
16111638
pub struct NSDataBase64EncodingOptions: libc::c_ulonglong {
16121639
const NSDataBase64Encoding64CharacterLineLength = 1 << 0;
@@ -1616,26 +1643,34 @@ bitflags! {
16161643
}
16171644
}
16181645

1646+
impl_Encode!(NSDataBase64EncodingOptions, libc::c_ulonglong);
1647+
16191648
bitflags! {
16201649
pub struct NSDataBase64DecodingOptions: libc::c_ulonglong {
16211650
const NSDataBase64DecodingIgnoreUnknownCharacters = 1 << 0;
16221651
}
16231652
}
16241653

1654+
impl_Encode!(NSDataBase64DecodingOptions, libc::c_ulonglong);
1655+
16251656
bitflags! {
16261657
pub struct NSDataWritingOptions: libc::c_ulonglong {
16271658
const NSDataWritingAtomic = 1 << 0;
16281659
const NSDataWritingWithoutOverwriting = 1 << 1;
16291660
}
16301661
}
16311662

1663+
impl_Encode!(NSDataWritingOptions, libc::c_ulonglong);
1664+
16321665
bitflags! {
16331666
pub struct NSDataSearchOptions: libc::c_ulonglong {
16341667
const NSDataSearchBackwards = 1 << 0;
16351668
const NSDataSearchAnchored = 1 << 1;
16361669
}
16371670
}
16381671

1672+
impl_Encode!(NSDataSearchOptions, libc::c_ulonglong);
1673+
16391674
pub trait NSUserDefaults {
16401675
unsafe fn standardUserDefaults() -> Self;
16411676

cocoa-foundation/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,20 @@ extern crate bitflags;
1515
extern crate core_foundation;
1616
extern crate core_graphics_types;
1717
extern crate libc;
18+
pub extern crate objc_encode;
1819
#[macro_use]
1920
extern crate objc;
2021

22+
pub use objc_encode as __objc_encode;
23+
24+
#[macro_export]
25+
macro_rules! impl_Encode {
26+
($t:ty, $delegation:ty) => {
27+
unsafe impl $crate::__objc_encode::Encode for $t {
28+
const ENCODING: $crate::__objc_encode::Encoding<'static> = <$delegation>::ENCODING;
29+
}
30+
};
31+
}
32+
2133
pub mod base;
2234
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.23" }
2121
foreign-types = "0.5"
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)