-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add extern_protocol! macro and ProtocolType trait
- Loading branch information
Showing
8 changed files
with
137 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/// TODO | ||
#[doc(alias = "@protocol")] | ||
#[macro_export] | ||
macro_rules! extern_protocol { | ||
( | ||
$(#[$m:meta])* | ||
$v:vis struct $name:ident; | ||
|
||
unsafe impl ProtocolType for $for:ty { | ||
$(#[inherits($($inheritance_rest:ty),+)])? | ||
type Super = $superprotocol:ty; | ||
|
||
$(const NAME: &'static str = $name_const:literal;)? | ||
} | ||
) => { | ||
$crate::__inner_extern_class!( | ||
@__inner | ||
|
||
$(#[$m])* | ||
$v struct ($name) {} | ||
|
||
unsafe impl () for $for { | ||
INHERITS = [$superprotocol, $($($inheritance_rest,)+)? $crate::runtime::Object]; | ||
} | ||
); | ||
|
||
unsafe impl ProtocolType for $for { | ||
type Super = $superprotocol; | ||
const NAME: &'static str = $crate::__select_name!($name; $($name_const)?); | ||
|
||
#[inline] | ||
fn protocol() -> &'static $crate::runtime::Protocol { | ||
$crate::runtime::Protocol::get(<Self as ProtocolType>::NAME).unwrap_or_else(|| { | ||
$crate::__macro_helpers::panic!("could not find protocol {}", Self::NAME) | ||
}) | ||
} | ||
|
||
#[inline] | ||
fn as_super(&self) -> &Self::Super { | ||
&self.__inner | ||
} | ||
|
||
#[inline] | ||
fn as_super_mut(&mut self) -> &mut Self::Super { | ||
&mut self.__inner | ||
} | ||
} | ||
|
||
const _: () = { | ||
if $crate::__macro_helpers::size_of::<$name>() != 0 { | ||
panic!(concat!( | ||
"the struct ", | ||
stringify!($name), | ||
" is not zero-sized!", | ||
)) | ||
} | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
use crate::runtime::Protocol; | ||
use crate::Message; | ||
|
||
/// Marks types that represent specific protocols. | ||
/// | ||
/// TODO. | ||
/// | ||
/// | ||
/// # Safety | ||
/// | ||
/// Same as [`ClassType`], TODO. | ||
/// | ||
/// [`ClassType`]: crate::ClassType | ||
pub unsafe trait ProtocolType: Message { | ||
/// The protocol that this protocol inherits. | ||
type Super: Message; | ||
|
||
/// The name of the Objective-C protocol that this type represents. | ||
const NAME: &'static str; | ||
|
||
/// Get a reference to the Objective-C protocol that this type represents. | ||
/// | ||
/// May register the protocol with the runtime if it wasn't already. | ||
/// | ||
/// | ||
/// # Panics | ||
/// | ||
/// This may panic if something went wrong with getting or declaring the | ||
/// protocol, e.g. if the program is not properly linked to the framework | ||
/// that defines the protocol. | ||
fn protocol() -> &'static Protocol; | ||
|
||
/// Get an immutable reference to the superprotocol. | ||
fn as_super(&self) -> &Self::Super; | ||
|
||
/// Get a mutable reference to the superprotocol. | ||
fn as_super_mut(&mut self) -> &mut Self::Super; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters