Skip to content

Commit 40031dd

Browse files
authored
Merge pull request #95 from madsmtm/few-more-encodes
Add a few more encoding impls
2 parents 886544b + 677ef61 commit 40031dd

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

objc2-encode/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1010
* `Encoding::equivalent_to`, `Encoding::equivalent_to_str` and
1111
`Encoding::equivalent_to_start_of_str` methods for more precise comparison
1212
semantics.
13+
* Added `Encode` and `RefEncode` implementations for `Option` function
14+
pointers.
1315

1416
### Changed
1517
* Discourage comparing `str` with `Encoding` using `PartialEq`. This trait

objc2-encode/src/encode.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,10 +408,16 @@ macro_rules! encode_fn_pointer_impl {
408408
unsafe impl<Ret: Encode, $($Arg: Encode),*> Encode for $FnTy {
409409
const ENCODING: Encoding<'static> = Encoding::Pointer(&Encoding::Unknown);
410410
}
411-
412411
unsafe impl<Ret: Encode, $($Arg: Encode),*> RefEncode for $FnTy {
413412
const ENCODING_REF: Encoding<'static> = Encoding::Pointer(&Self::ENCODING);
414413
}
414+
415+
unsafe impl<Ret: Encode, $($Arg: Encode),*> Encode for Option<$FnTy> {
416+
const ENCODING: Encoding<'static> = Encoding::Pointer(&Encoding::Unknown);
417+
}
418+
unsafe impl<Ret: Encode, $($Arg: Encode),*> RefEncode for Option<$FnTy> {
419+
const ENCODING_REF: Encoding<'static> = Encoding::Pointer(&Self::ENCODING);
420+
}
415421
};
416422
($($Arg: ident),+) => {
417423
// Normal functions
@@ -543,5 +549,9 @@ mod tests {
543549
<extern "C" fn(x: ()) -> ()>::ENCODING,
544550
Encoding::Pointer(&Encoding::Unknown)
545551
);
552+
assert_eq!(
553+
<Option<unsafe extern "C" fn()>>::ENCODING,
554+
Encoding::Pointer(&Encoding::Unknown)
555+
);
546556
}
547557
}

objc2/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1010
* Export `objc-sys` as `ffi` module.
1111
* Added common trait impls on `rc::Owned` and `rc::Shared` (useful in generic
1212
contexts).
13+
* Implement `RefEncode` for `runtime::Protocol`.
1314

1415
### Changed
1516
* Deprecated `runtime::BOOL`, `runtime::YES` and `runtime::NO`. Use the

objc2/src/runtime.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,11 @@ impl PartialEq for Protocol {
472472
}
473473
}
474474

475+
unsafe impl RefEncode for Protocol {
476+
// Protocol is an object internally
477+
const ENCODING_REF: Encoding<'static> = Encoding::Object;
478+
}
479+
475480
impl Eq for Protocol {}
476481

477482
impl fmt::Debug for Protocol {
@@ -595,7 +600,7 @@ impl fmt::Debug for Object {
595600
mod tests {
596601
use alloc::string::ToString;
597602

598-
use super::{Bool, Class, Ivar, Method, Object, Protocol, Sel};
603+
use super::{Bool, Class, Imp, Ivar, Method, Object, Protocol, Sel};
599604
use crate::test_utils;
600605
use crate::Encode;
601606

@@ -697,10 +702,16 @@ mod tests {
697702

698703
#[test]
699704
fn test_encode() {
700-
assert_eq!(<&Object>::ENCODING.to_string(), "@");
701-
assert_eq!(<*mut Object>::ENCODING.to_string(), "@");
702-
assert_eq!(<&Class>::ENCODING.to_string(), "#");
703-
assert_eq!(Sel::ENCODING.to_string(), ":");
705+
fn assert_enc<T: Encode>(expected: &str) {
706+
assert_eq!(&T::ENCODING.to_string(), expected);
707+
}
708+
assert_enc::<&Object>("@");
709+
assert_enc::<*mut Object>("@");
710+
assert_enc::<&Class>("#");
711+
assert_enc::<Sel>(":");
712+
assert_enc::<Imp>("^?");
713+
assert_enc::<Option<Imp>>("^?");
714+
assert_enc::<&Protocol>("@");
704715
}
705716

706717
#[test]

0 commit comments

Comments
 (0)