Skip to content

Commit ae70554

Browse files
committed
Implement encode for optional function pointers
1 parent 886544b commit ae70554

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
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/src/runtime.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ impl fmt::Debug for Object {
595595
mod tests {
596596
use alloc::string::ToString;
597597

598-
use super::{Bool, Class, Ivar, Method, Object, Protocol, Sel};
598+
use super::{Bool, Class, Imp, Ivar, Method, Object, Protocol, Sel};
599599
use crate::test_utils;
600600
use crate::Encode;
601601

@@ -701,6 +701,8 @@ mod tests {
701701
assert_eq!(<*mut Object>::ENCODING.to_string(), "@");
702702
assert_eq!(<&Class>::ENCODING.to_string(), "#");
703703
assert_eq!(Sel::ENCODING.to_string(), ":");
704+
assert_eq!(Imp::ENCODING.to_string(), "^?");
705+
assert_eq!(<Option<Imp>>::ENCODING.to_string(), "^?");
704706
}
705707

706708
#[test]

0 commit comments

Comments
 (0)