-
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
153 additions
and
8 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,50 @@ | ||
/// TODO | ||
#[doc(alias = "@protocol")] | ||
#[macro_export] | ||
macro_rules! extern_protocol { | ||
( | ||
$(#[$m:meta])* | ||
$v:vis struct $name:ident; | ||
|
||
unsafe impl ProtocolType for $for:ty { | ||
$(const NAME: &'static str = $name_const:literal;)? | ||
} | ||
) => { | ||
$crate::__inner_extern_class!( | ||
@__inner | ||
|
||
$(#[$m])* | ||
$v struct ($name) {} | ||
|
||
unsafe impl () for $for { | ||
INHERITS = [$crate::runtime::Object]; | ||
} | ||
); | ||
|
||
// SAFETY: TODO | ||
unsafe impl ProtocolType for $for { | ||
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 as ProtocolType>::NAME, | ||
) | ||
}) | ||
} | ||
} | ||
|
||
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,65 @@ | ||
use core::ptr::NonNull; | ||
|
||
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 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; | ||
} | ||
|
||
/// TODO | ||
/// | ||
/// # Safety | ||
/// | ||
/// TODO | ||
pub unsafe trait ProtocolImpl<P: ProtocolType> { | ||
/// Get an immutable reference to a type representing the protocol. | ||
fn as_protocol(&self) -> &P { | ||
let ptr: NonNull<Self> = NonNull::from(self); | ||
let ptr: NonNull<P> = ptr.cast(); | ||
// SAFETY: TODO | ||
unsafe { ptr.as_ref() } | ||
} | ||
|
||
/// Get a mutable reference to a type representing the protocol. | ||
fn as_protocol_mut(&mut self) -> &mut P { | ||
let ptr: NonNull<Self> = NonNull::from(self); | ||
let mut ptr: NonNull<P> = ptr.cast(); | ||
// SAFETY: TODO | ||
unsafe { ptr.as_mut() } | ||
} | ||
} | ||
|
||
// SAFETY: Trivial | ||
unsafe impl<P: ProtocolType> ProtocolImpl<P> for P { | ||
fn as_protocol(&self) -> &P { | ||
self | ||
} | ||
|
||
fn as_protocol_mut(&mut self) -> &mut P { | ||
self | ||
} | ||
} |
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