Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace objc with objc2? #513

Closed
wants to merge 14 commits into from
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
- name: Install toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: 1.56.1
toolchain: '1.60'
override: true
- name: Build
run: cargo build --verbose
Expand Down
6 changes: 3 additions & 3 deletions cocoa-foundation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ license = "MIT OR Apache-2.0"
default-target = "x86_64-apple-darwin"

[dependencies]
block = "0.1"
block2 = "0.2.0"
bitflags = "1.0"
libc = "0.2"
core-foundation = { path = "../core-foundation", version = "0.9" }
core-graphics-types = { path = "../core-graphics-types", version = "0.1" }
objc = "0.2.3"
core-graphics-types = { path = "../core-graphics-types", version = "0.1", features = ["objc2"] }
objc2 = "0.4.0"
6 changes: 4 additions & 2 deletions cocoa-foundation/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use objc::runtime;
use objc2::runtime;

pub use objc::runtime::{BOOL, NO, YES};
pub type BOOL = runtime::BOOL;
pub const NO: BOOL = runtime::NO;
pub const YES: BOOL = runtime::YES;

pub type Class = *mut runtime::Class;
#[allow(non_camel_case_types)]
Expand Down
100 changes: 68 additions & 32 deletions cocoa-foundation/src/foundation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
#![allow(non_upper_case_globals)]

use base::{id, nil, BOOL, NO, SEL};
use block::Block;
use block2::Block;
use libc;
use objc2::encode::{Encode, Encoding, RefEncode};
use std::os::raw::c_void;
use std::ptr;

Expand All @@ -35,7 +36,7 @@ mod macos {
use base::id;
use core_graphics_types::base::CGFloat;
use core_graphics_types::geometry::CGRect;
use objc;
use objc2::encode::{Encode, Encoding};
use std::mem;

#[repr(C)]
Expand All @@ -52,15 +53,9 @@ mod macos {
}
}

unsafe impl objc::Encode for NSPoint {
fn encode() -> objc::Encoding {
let encoding = format!(
"{{CGPoint={}{}}}",
CGFloat::encode().as_str(),
CGFloat::encode().as_str()
);
unsafe { objc::Encoding::from_str(&encoding) }
}
unsafe impl Encode for NSPoint {
const ENCODING: Encoding =
Encoding::Struct("CGPoint", &[CGFloat::ENCODING, CGFloat::ENCODING]);
}

#[repr(C)]
Expand All @@ -80,15 +75,9 @@ mod macos {
}
}

unsafe impl objc::Encode for NSSize {
fn encode() -> objc::Encoding {
let encoding = format!(
"{{CGSize={}{}}}",
CGFloat::encode().as_str(),
CGFloat::encode().as_str()
);
unsafe { objc::Encoding::from_str(&encoding) }
}
unsafe impl Encode for NSSize {
const ENCODING: Encoding =
Encoding::Struct("CGSize", &[CGFloat::ENCODING, CGFloat::ENCODING]);
}

#[repr(C)]
Expand Down Expand Up @@ -118,15 +107,9 @@ mod macos {
}
}

unsafe impl objc::Encode for NSRect {
fn encode() -> objc::Encoding {
let encoding = format!(
"{{CGRect={}{}}}",
NSPoint::encode().as_str(),
NSSize::encode().as_str()
);
unsafe { objc::Encoding::from_str(&encoding) }
}
unsafe impl Encode for NSRect {
const ENCODING: Encoding =
Encoding::Struct("CGRect", &[NSPoint::ENCODING, NSSize::ENCODING]);
}

// Same as CGRectEdge
Expand All @@ -138,6 +121,8 @@ mod macos {
NSRectMaxYEdge,
}

impl_Encode!(NSRectEdge, u32);

#[link(name = "Foundation", kind = "framework")]
extern "C" {
fn NSInsetRect(rect: NSRect, x: CGFloat, y: CGFloat) -> NSRect;
Expand Down Expand Up @@ -166,6 +151,11 @@ pub struct NSRange {
pub length: NSUInteger,
}

unsafe impl Encode for NSRange {
const ENCODING: Encoding =
Encoding::Struct("_NSRange", &[NSUInteger::ENCODING, NSUInteger::ENCODING]);
}

impl NSRange {
#[inline]
pub fn new(location: NSUInteger, length: NSUInteger) -> NSRange {
Expand Down Expand Up @@ -208,6 +198,17 @@ pub struct NSOperatingSystemVersion {
pub patchVersion: NSUInteger,
}

unsafe impl Encode for NSOperatingSystemVersion {
const ENCODING: Encoding = Encoding::Struct(
"NSOperatingSystemVersion",
&[
NSUInteger::ENCODING,
NSUInteger::ENCODING,
NSUInteger::ENCODING,
],
);
}

impl NSOperatingSystemVersion {
#[inline]
pub fn new(
Expand Down Expand Up @@ -248,7 +249,8 @@ impl NSProcessInfo for id {
}

unsafe fn isOperatingSystemAtLeastVersion(self, version: NSOperatingSystemVersion) -> bool {
msg_send![self, isOperatingSystemAtLeastVersion: version]
let res: BOOL = msg_send![self, isOperatingSystemAtLeastVersion: version];
res != NO
}
}

Expand Down Expand Up @@ -628,6 +630,8 @@ bitflags! {
}
}

impl_Encode!(NSEnumerationOptions, libc::c_ulonglong);

pub type NSComparator = *mut Block<(id, id), NSComparisonResult>;

#[repr(isize)]
Expand All @@ -638,6 +642,8 @@ pub enum NSComparisonResult {
NSOrderedDescending = 1,
}

impl_Encode!(NSComparisonResult, isize);

pub trait NSString: Sized {
unsafe fn alloc(_: Self) -> id {
msg_send![class!(NSString), alloc]
Expand All @@ -664,9 +670,9 @@ impl NSString for id {

unsafe fn init_str(self, string: &str) -> id {
return msg_send![self,
initWithBytes:string.as_ptr()
initWithBytes:string.as_ptr() as *const c_void
length:string.len()
encoding:UTF8_ENCODING as id];
encoding:UTF8_ENCODING];
}

unsafe fn len(self) -> usize {
Expand Down Expand Up @@ -702,6 +708,22 @@ struct NSFastEnumerationState {
pub extra: [libc::c_ulong; 5],
}

unsafe impl Encode for NSFastEnumerationState {
const ENCODING: Encoding = Encoding::Struct(
"?",
&[
libc::c_ulong::ENCODING,
Encoding::Pointer(&Encoding::Object),
Encoding::Pointer(&libc::c_ulong::ENCODING),
Encoding::Array(5, &libc::c_ulong::ENCODING),
],
);
}

unsafe impl RefEncode for NSFastEnumerationState {
const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
}

const NS_FAST_ENUM_BUF_SIZE: usize = 16;

pub struct NSFastIterator {
Expand Down Expand Up @@ -810,6 +832,8 @@ bitflags! {
}
}

impl_Encode!(NSURLBookmarkCreationOptions, NSUInteger);

pub type NSURLBookmarkFileCreationOptions = NSURLBookmarkCreationOptions;

bitflags! {
Expand All @@ -820,6 +844,8 @@ bitflags! {
}
}

impl_Encode!(NSURLBookmarkResolutionOptions, NSUInteger);

pub trait NSURL: Sized {
unsafe fn alloc(_: Self) -> id;

Expand Down Expand Up @@ -1606,6 +1632,8 @@ bitflags! {
}
}

impl_Encode!(NSDataReadingOptions, libc::c_ulonglong);

bitflags! {
pub struct NSDataBase64EncodingOptions: libc::c_ulonglong {
const NSDataBase64Encoding64CharacterLineLength = 1 << 0;
Expand All @@ -1615,26 +1643,34 @@ bitflags! {
}
}

impl_Encode!(NSDataBase64EncodingOptions, libc::c_ulonglong);

bitflags! {
pub struct NSDataBase64DecodingOptions: libc::c_ulonglong {
const NSDataBase64DecodingIgnoreUnknownCharacters = 1 << 0;
}
}

impl_Encode!(NSDataBase64DecodingOptions, libc::c_ulonglong);

bitflags! {
pub struct NSDataWritingOptions: libc::c_ulonglong {
const NSDataWritingAtomic = 1 << 0;
const NSDataWritingWithoutOverwriting = 1 << 1;
}
}

impl_Encode!(NSDataWritingOptions, libc::c_ulonglong);

bitflags! {
pub struct NSDataSearchOptions: libc::c_ulonglong {
const NSDataSearchBackwards = 1 << 0;
const NSDataSearchAnchored = 1 << 1;
}
}

impl_Encode!(NSDataSearchOptions, libc::c_ulonglong);

pub trait NSUserDefaults {
unsafe fn standardUserDefaults() -> Self;

Expand Down
14 changes: 12 additions & 2 deletions cocoa-foundation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,25 @@
// except according to those terms.

#![allow(non_snake_case)]
#![allow(deprecated)] // TODO(madsmtm): Remove this

extern crate block;
extern crate block2;
#[macro_use]
extern crate bitflags;
extern crate core_foundation;
extern crate core_graphics_types;
extern crate libc;
#[macro_use]
extern crate objc;
pub extern crate objc2;

#[macro_export]
macro_rules! impl_Encode {
($t:ty, $delegation:ty) => {
unsafe impl $crate::objc2::encode::Encode for $t {
const ENCODING: $crate::objc2::encode::Encoding = <$delegation>::ENCODING;
}
};
}

pub mod base;
pub mod foundation;
12 changes: 7 additions & 5 deletions cocoa-foundation/tests/foundation.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#[macro_use]
extern crate objc;
extern crate block;
extern crate objc2;
extern crate block2;
extern crate cocoa_foundation;

#[cfg(test)]
Expand Down Expand Up @@ -108,7 +108,7 @@ mod foundation {
}

mod nsdictionary {
use block::ConcreteBlock;
use block2::ConcreteBlock;
use cocoa_foundation::base::{id, nil};
use cocoa_foundation::foundation::{
NSArray, NSComparisonResult, NSDictionary, NSFastEnumeration, NSString,
Expand Down Expand Up @@ -164,16 +164,18 @@ mod foundation {
}

// First test cocoa sorting...
let mut comparator =
let comparator =
ConcreteBlock::new(|s0: id, s1: id| match compare_function(&s0, &s1) {
Ordering::Less => NSComparisonResult::NSOrderedAscending,
Ordering::Equal => NSComparisonResult::NSOrderedSame,
Ordering::Greater => NSComparisonResult::NSOrderedDescending,
});
let comparator_ptr: *const _ = &*comparator;
let comparator_ptr = comparator_ptr as *mut _;

let associated_iter = keys.iter().zip(objects.iter());
for (k_id, (k, v)) in dict
.keysSortedByValueUsingComparator_(&mut *comparator)
.keysSortedByValueUsingComparator_(comparator_ptr)
.iter()
.zip(associated_iter)
{
Expand Down
4 changes: 2 additions & 2 deletions cocoa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ license = "MIT OR Apache-2.0"
default-target = "x86_64-apple-darwin"

[dependencies]
block = "0.1"
block2 = "0.2.0"
bitflags = "1.0"
libc = "0.2"
cocoa-foundation = { path = "../cocoa-foundation", version = "0.1" }
core-foundation = { path = "../core-foundation", version = "0.9" }
core-graphics = { path = "../core-graphics", version = "0.23" }
foreign-types = "0.5"
objc = "0.2.3"
objc2 = "0.4.0"
Loading