Skip to content

Commit c50b646

Browse files
committed
Replaced the EncodePtr trait with a macro.
1 parent 51139b9 commit c50b646

File tree

4 files changed

+53
-45
lines changed

4 files changed

+53
-45
lines changed

core/block.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ use std::ptr;
5252
use libc::{c_int, c_ulong};
5353

5454
use runtime::{Class, Object};
55-
use {EncodePtr, Id, Message};
55+
use {Id, Message};
5656

5757
#[link(name = "Foundation", kind = "framework")]
5858
extern {
@@ -121,9 +121,7 @@ impl<A: BlockArguments, R> Block<A, R> where A: BlockArguments {
121121

122122
unsafe impl<A, R> Message for Block<A, R> { }
123123

124-
impl<A, R> EncodePtr for Block<A, R> {
125-
fn ptr_code() -> &'static str { "@?" }
126-
}
124+
encode_message_impl!("@?", Block, A, R);
127125

128126
/// Types that may be converted into a `ConcreteBlock`.
129127
pub trait IntoConcreteBlock<A> where A: BlockArguments {

core/encode.rs

+2-40
Original file line numberDiff line numberDiff line change
@@ -96,47 +96,9 @@ impl Encode for Sel {
9696
fn code() -> &'static str { ":" }
9797
}
9898

99-
/// Types that pointers to have an Objective-C type encoding.
100-
///
101-
/// This trait is largely for convenience, so that it can be implemnted once
102-
/// to implement encode for the 6 pointer types: immutable/mutable references,
103-
/// optional immutable/mutable references, and const/mutable pointers.
104-
pub trait EncodePtr : PhantomFn<Self> {
105-
/// Returns the encoding for pointers to Self.
106-
fn ptr_code() -> &'static str;
107-
}
99+
encode_message_impl!("@", Object);
108100

109-
impl<'a, T> Encode for &'a T where T: EncodePtr {
110-
fn code() -> &'static str { T::ptr_code() }
111-
}
112-
113-
impl<'a, T> Encode for &'a mut T where T: EncodePtr {
114-
fn code() -> &'static str { T::ptr_code() }
115-
}
116-
117-
impl<'a, T> Encode for Option<&'a T> where T: EncodePtr {
118-
fn code() -> &'static str { T::ptr_code() }
119-
}
120-
121-
impl<'a, T> Encode for Option<&'a mut T> where T: EncodePtr {
122-
fn code() -> &'static str { T::ptr_code() }
123-
}
124-
125-
impl<T> Encode for *const T where T: EncodePtr {
126-
fn code() -> &'static str { T::ptr_code() }
127-
}
128-
129-
impl<T> Encode for *mut T where T: EncodePtr {
130-
fn code() -> &'static str { T::ptr_code() }
131-
}
132-
133-
impl EncodePtr for Object {
134-
fn ptr_code() -> &'static str { "@" }
135-
}
136-
137-
impl EncodePtr for Class {
138-
fn ptr_code() -> &'static str { "#" }
139-
}
101+
encode_message_impl!("#", Class);
140102

141103
/// Returns the Objective-C type encoding for a type.
142104
pub fn encode<T>() -> &'static str where T: Encode {

core/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extern crate malloc_buf;
1313
extern crate objc_test_utils;
1414

1515
pub use id::{Id, Owned, Ownership, Shared, ShareId};
16-
pub use encode::{encode, Encode, EncodePtr};
16+
pub use encode::{encode, Encode};
1717
pub use message::{send_message, send_super_message, Message, MessageArguments, ToMessage};
1818
pub use weak::WeakId;
1919

core/macros.rs

+48
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,51 @@ macro_rules! msg_send {
7171
$crate::send_message(&$obj, sel, ($($arg,)*))
7272
});
7373
}
74+
75+
/// Implements the `Encode` trait for a `Message` type.
76+
/// Specifically, this will implement `Encode` for reference, pointers, and
77+
/// `Option` references of the given type.
78+
///
79+
/// The first argument should be a static string that is the type encoding
80+
/// to use in the implementation. The second argument is the ident of the name
81+
/// of the type to implement `Encode` for, and any further arguments are
82+
/// used as type parameters for the type.
83+
///
84+
/// # Example
85+
/// ``` ignore
86+
/// impl Message for Object { }
87+
/// encode_message_impl!("@", Object)
88+
///
89+
/// impl<T> Message for Array<T> { }
90+
/// encode_message_impl!("@", Array, T)
91+
/// ```
92+
macro_rules! encode_message_impl {
93+
($code:expr, $name:ident) => (
94+
encode_message_impl!($code, $name,);
95+
);
96+
($code:expr, $name:ident, $($t:ident),*) => (
97+
impl<'a $(, $t)*> $crate::Encode for &'a $name<$($t),*> {
98+
fn code() -> &'static str { $code }
99+
}
100+
101+
impl<'a $(, $t)*> $crate::Encode for &'a mut $name<$($t),*> {
102+
fn code() -> &'static str { $code }
103+
}
104+
105+
impl<'a $(, $t)*> $crate::Encode for Option<&'a $name<$($t),*>> {
106+
fn code() -> &'static str { $code }
107+
}
108+
109+
impl<'a $(, $t)*> $crate::Encode for Option<&'a mut $name<$($t),*>> {
110+
fn code() -> &'static str { $code }
111+
}
112+
113+
impl<$($t),*> $crate::Encode for *const $name<$($t),*> {
114+
fn code() -> &'static str { $code }
115+
}
116+
117+
impl<$($t),*> $crate::Encode for *mut $name<$($t),*> {
118+
fn code() -> &'static str { $code }
119+
}
120+
);
121+
}

0 commit comments

Comments
 (0)