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

WIP: Allow bool as the return type from msg_send! #126

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions objc2-encode/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,13 @@ unsafe impl Encode for () {
const ENCODING: Encoding<'static> = Encoding::Void;
}

/// Using this directly is heavily discouraged, since the type of BOOL differs
/// across platforms.
///
/// Use `objc2::runtime::Bool::ENCODING` instead.
unsafe impl Encode for bool {
const ENCODING: Encoding<'static> = Encoding::Bool;
}
// /// Using this directly is heavily discouraged, since the type of BOOL differs
// /// across platforms.
// ///
// /// Use `objc2::runtime::Bool::ENCODING` instead.
// unsafe impl Encode for bool {
// const ENCODING: Encoding<'static> = Encoding::Bool;
// }

macro_rules! encode_impls_size {
($($t:ty => ($t16:ty, $t32:ty, $t64:ty),)*) => ($(
Expand Down Expand Up @@ -219,7 +219,7 @@ macro_rules! pointer_refencode_impl {
)*);
}

pointer_refencode_impl!(bool, i16, i32, i64, isize, u16, u32, u64, usize, f32, f64);
pointer_refencode_impl!(i16, i32, i64, isize, u16, u32, u64, usize, f32, f64);

/// Pointers to [`i8`] use the special [`Encoding::String`] encoding.
unsafe impl RefEncode for i8 {
Expand Down
22 changes: 22 additions & 0 deletions objc2-encode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,25 @@ mod parse;

pub use self::encode::{Encode, EncodeArguments, RefEncode};
pub use self::encoding::Encoding;

/// TODO
pub trait ReturnType {
/// TODO
type From: Encode;
/// TODO
fn from_return_type(from: Self::From) -> Self;
}

impl<T: Encode> ReturnType for T {
type From = T;
fn from_return_type(from: Self::From) -> Self {
from
}
}

impl ReturnType for bool {
type From = i8; // BOOL - Varies between runtimes
fn from_return_type(from: Self::From) -> Self {
from != 0
}
}
12 changes: 12 additions & 0 deletions objc2/examples/into_bool.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use objc2::runtime::Object;
use objc2::{class, msg_send};

fn main() {
// Get a class
let cls = class!(NSObject);

let obj: *mut Object = unsafe { msg_send![cls, new] };

let x = unsafe { msg_send![obj, isEqual: obj] };
if x {}
}
2 changes: 1 addition & 1 deletion objc2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ extern "C" {}

pub use objc_sys as ffi;

pub use objc2_encode::{Encode, EncodeArguments, Encoding, RefEncode};
pub use objc2_encode::{Encode, EncodeArguments, Encoding, RefEncode, ReturnType};

pub use crate::message::{Message, MessageArguments, MessageError, MessageReceiver};

Expand Down
8 changes: 4 additions & 4 deletions objc2/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ macro_rules! msg_send {
let result;
match $crate::MessageReceiver::send_super_message(&$obj, $superclass, sel, ()) {
Err(s) => panic!("{}", s),
Ok(r) => result = r,
Ok(r) => result = $crate::ReturnType::from_return_type(r),
}
result
});
Expand All @@ -149,7 +149,7 @@ macro_rules! msg_send {
let result;
match $crate::MessageReceiver::send_super_message(&$obj, $superclass, sel, ($($arg,)+)) {
Err(s) => panic!("{}", s),
Ok(r) => result = r,
Ok(r) => result = $crate::ReturnType::from_return_type(r),
}
result
});
Expand All @@ -158,7 +158,7 @@ macro_rules! msg_send {
let result;
match $crate::MessageReceiver::send_message(&$obj, sel, ()) {
Err(s) => panic!("{}", s),
Ok(r) => result = r,
Ok(r) => result = $crate::ReturnType::from_return_type(r),
}
result
});
Expand All @@ -167,7 +167,7 @@ macro_rules! msg_send {
let result;
match $crate::MessageReceiver::send_message(&$obj, sel, ($($arg,)+)) {
Err(s) => panic!("{}", s),
Ok(r) => result = r,
Ok(r) => result = $crate::ReturnType::from_return_type(r),
}
result
});
Expand Down