Skip to content

Commit

Permalink
Implement Encode for most types
Browse files Browse the repository at this point in the history
Using new objc-encode crate
  • Loading branch information
madsmtm committed Jul 20, 2022
1 parent 94081c8 commit 94478ae
Show file tree
Hide file tree
Showing 19 changed files with 278 additions and 27 deletions.
4 changes: 3 additions & 1 deletion cocoa-foundation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ libc = "0.2"
core-foundation = { path = "../core-foundation", version = "0.9" }
core-graphics-types = { path = "../core-graphics-types", version = "0.1" }
foreign-types = "0.3"
objc = "0.2.3"
# Current `master` of objc
objc = { version = "=0.3.0-alpha.0", package = "objc2" }
objc-encode = "1.1.0"
87 changes: 64 additions & 23 deletions cocoa-foundation/src/foundation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::os::raw::c_void;
use base::{id, BOOL, NO, SEL, nil};
use block::Block;
use libc;
use objc_encode::{Encode, Encoding};


#[cfg(target_pointer_width = "32")]
Expand All @@ -37,7 +38,7 @@ mod macos {
use base::id;
use core_graphics_types::base::CGFloat;
use core_graphics_types::geometry::CGRect;
use objc;
use objc_encode::{Encode, Encoding};

#[repr(C)]
#[derive(Copy, Clone)]
Expand All @@ -56,13 +57,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<'static> =
Encoding::Struct("CGPoint", &[CGFloat::ENCODING, CGFloat::ENCODING]);
}

#[repr(C)]
Expand All @@ -82,13 +79,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<'static> =
Encoding::Struct("CGSize", &[CGFloat::ENCODING, CGFloat::ENCODING]);
}

#[repr(C)]
Expand Down Expand Up @@ -122,13 +115,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<'static> =
Encoding::Struct("CGRect", &[NSPoint::ENCODING, NSSize::ENCODING]);
}

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

impl_Encode!(NSRectEdge, u32);

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

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

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

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

impl NSOperatingSystemVersion {
#[inline]
pub fn new(majorVersion: NSUInteger, minorVersion: NSUInteger, patchVersion: NSUInteger) -> NSOperatingSystemVersion {
Expand Down Expand Up @@ -593,6 +600,8 @@ bitflags! {
}
}

impl_Encode!(NSEnumerationOptions, libc::c_ulonglong);

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

#[repr(isize)]
Expand All @@ -603,6 +612,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 Down Expand Up @@ -669,6 +680,23 @@ struct NSFastEnumerationState {
pub extra: [libc::c_ulong; 5]
}

unsafe impl Encode for NSFastEnumerationState {
const ENCODING: Encoding<'static> = 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 Encode for &'_ NSFastEnumerationState {
const ENCODING: Encoding<'static> =
Encoding::Pointer(&NSFastEnumerationState::ENCODING);
}

const NS_FAST_ENUM_BUF_SIZE: usize = 16;

pub struct NSFastIterator {
Expand All @@ -677,7 +705,7 @@ pub struct NSFastIterator {
mut_val: Option<libc::c_ulong>,
len: usize,
idx: usize,
object: id
object: id,
}

impl Iterator for NSFastIterator {
Expand Down Expand Up @@ -774,6 +802,8 @@ bitflags! {
}
}

impl_Encode!(NSURLBookmarkCreationOptions, NSUInteger);

pub type NSURLBookmarkFileCreationOptions = NSURLBookmarkCreationOptions;

bitflags! {
Expand All @@ -784,6 +814,7 @@ bitflags! {
}
}

impl_Encode!(NSURLBookmarkResolutionOptions, NSUInteger);

pub trait NSURL: Sized {
unsafe fn alloc(_: Self) -> id;
Expand Down Expand Up @@ -1337,6 +1368,8 @@ bitflags! {
}
}

impl_Encode!(NSDataReadingOptions, libc::c_ulonglong);

bitflags! {
pub struct NSDataBase64EncodingOptions: libc::c_ulonglong {
const NSDataBase64Encoding64CharacterLineLength = 1 << 0;
Expand All @@ -1346,26 +1379,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
12 changes: 12 additions & 0 deletions cocoa-foundation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,20 @@ extern crate core_foundation;
extern crate core_graphics_types;
extern crate foreign_types;
extern crate libc;
pub extern crate objc_encode;
#[macro_use]
extern crate objc;

pub use objc_encode as __objc_encode;

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

pub mod base;
pub mod foundation;
4 changes: 3 additions & 1 deletion cocoa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ cocoa-foundation = { path = "../cocoa-foundation", version = "0.1" }
core-foundation = { path = "../core-foundation", version = "0.9" }
core-graphics = { path = "../core-graphics", version = "0.22" }
foreign-types = "0.3"
objc = "0.2.3"
# Current `master` of objc
objc = { version = "=0.3.0-alpha.0", package = "objc2" }
objc-encode = "1.1.0"
Loading

0 comments on commit 94478ae

Please sign in to comment.