Skip to content

Commit 8c5d3ff

Browse files
committed
Future proof MethodImplementation trait
1 parent aa118cb commit 8c5d3ff

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

objc2/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
6161
* **BREAKING**: `Class` no longer implements `Message` (but it can still be
6262
used as the receiver in `msg_send!`, so this is unlikely to break anything
6363
in practice).
64+
* **BREAKING**: Sealed the `MethodImplementation` trait, and made it's `imp`
65+
method privat.
6466

6567
### Fixed
6668
* Properly sealed the `MessageArguments` trait (it already had a hidden

objc2/src/declare.rs

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,37 @@ use core::ptr::NonNull;
4343
use std::ffi::CString;
4444

4545
use crate::runtime::{Bool, Class, Imp, Object, Protocol, Sel};
46-
use crate::{ffi, Encode, EncodeArguments, Encoding, Message};
46+
use crate::{ffi, Encode, EncodeArguments, Encoding, Message, RefEncode};
47+
48+
pub(crate) mod private {
49+
pub trait Sealed {}
50+
}
4751

4852
/// Types that can be used as the implementation of an Objective-C method.
49-
pub trait MethodImplementation {
53+
///
54+
/// This is a sealed trait that is implemented for a lot of `extern "C"`
55+
/// function pointer types.
56+
pub trait MethodImplementation: private::Sealed {
5057
/// The callee type of the method.
51-
type Callee: ?Sized;
58+
type Callee: RefEncode + ?Sized;
5259
/// The return type of the method.
5360
type Ret: Encode;
5461
/// The argument types of the method.
5562
type Args: EncodeArguments;
5663

57-
/// Returns self as an [`Imp`] of a method.
58-
fn imp(self) -> Imp;
64+
#[doc(hidden)]
65+
fn __imp(self) -> Imp;
5966
}
6067

6168
macro_rules! method_decl_impl {
62-
(-$s:ident, $r:ident, $f:ty, $($t:ident),*) => (
69+
(-$s:ident, $r:ident, $f:ty, $($t:ident),*) => {
70+
impl<$s, $r, $($t),*> private::Sealed for $f
71+
where
72+
$s: Message + ?Sized,
73+
$r: Encode,
74+
$($t: Encode,)*
75+
{}
76+
6377
impl<$s, $r, $($t),*> MethodImplementation for $f
6478
where
6579
$s: Message + ?Sized,
@@ -70,12 +84,18 @@ macro_rules! method_decl_impl {
7084
type Ret = $r;
7185
type Args = ($($t,)*);
7286

73-
fn imp(self) -> Imp {
87+
fn __imp(self) -> Imp {
7488
unsafe { mem::transmute(self) }
7589
}
7690
}
77-
);
78-
(@$s:ident, $r:ident, $f:ty, $($t:ident),*) => (
91+
};
92+
(@$s:ident, $r:ident, $f:ty, $($t:ident),*) => {
93+
impl<$r, $($t),*> private::Sealed for $f
94+
where
95+
$r: Encode,
96+
$($t: Encode,)*
97+
{}
98+
7999
impl<$r, $($t),*> MethodImplementation for $f
80100
where
81101
$r: Encode,
@@ -85,12 +105,12 @@ macro_rules! method_decl_impl {
85105
type Ret = $r;
86106
type Args = ($($t,)*);
87107

88-
fn imp(self) -> Imp {
108+
fn __imp(self) -> Imp {
89109
unsafe { mem::transmute(self) }
90110
}
91111
}
92-
);
93-
($($t:ident),*) => (
112+
};
113+
($($t:ident),*) => {
94114
method_decl_impl!(-T, R, extern "C" fn(&T, Sel $(, $t)*) -> R, $($t),*);
95115
method_decl_impl!(-T, R, extern "C" fn(&mut T, Sel $(, $t)*) -> R, $($t),*);
96116
method_decl_impl!(-T, R, unsafe extern "C" fn(*const T, Sel $(, $t)*) -> R, $($t),*);
@@ -101,7 +121,7 @@ macro_rules! method_decl_impl {
101121
method_decl_impl!(@Class, R, extern "C" fn(&Class, Sel $(, $t)*) -> R, $($t),*);
102122
method_decl_impl!(@Class, R, unsafe extern "C" fn(*const Class, Sel $(, $t)*) -> R, $($t),*);
103123
method_decl_impl!(@Class, R, unsafe extern "C" fn(&Class, Sel $(, $t)*) -> R, $($t),*);
104-
);
124+
};
105125
}
106126

107127
method_decl_impl!();
@@ -237,7 +257,7 @@ impl ClassBuilder {
237257
ffi::class_addMethod(
238258
self.as_ptr(),
239259
sel.as_ptr() as _,
240-
Some(func.imp()),
260+
Some(func.__imp()),
241261
types.as_ptr(),
242262
)
243263
});
@@ -275,7 +295,7 @@ impl ClassBuilder {
275295
ffi::class_addMethod(
276296
metaclass,
277297
sel.as_ptr() as _,
278-
Some(func.imp()),
298+
Some(func.__imp()),
279299
types.as_ptr(),
280300
)
281301
});

0 commit comments

Comments
 (0)