-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathencode.rs
67 lines (57 loc) · 1.87 KB
/
encode.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use crate::runtime::{Class, Object, Sel};
use crate::{Encode, Encoding};
unsafe impl Encode for Sel {
const ENCODING: Encoding<'static> = Encoding::Sel;
}
unsafe impl<'a> Encode for &'a Object {
const ENCODING: Encoding<'static> = Encoding::Object;
}
unsafe impl<'a> Encode for &'a mut Object {
const ENCODING: Encoding<'static> = Encoding::Object;
}
unsafe impl<'a> Encode for &'a Class {
const ENCODING: Encoding<'static> = Encoding::Class;
}
unsafe impl<'a> Encode for &'a mut Class {
const ENCODING: Encoding<'static> = Encoding::Class;
}
/// Types that represent a group of arguments, where each has an Objective-C
/// type encoding.
pub trait EncodeArguments {
/// The type as which the encodings for Self will be returned.
const ENCODINGS: &'static [Encoding<'static>];
}
macro_rules! encode_args_impl {
($($t:ident),*) => (
impl<$($t: Encode),*> EncodeArguments for ($($t,)*) {
const ENCODINGS: &'static [Encoding<'static>] = &[
$($t::ENCODING),*
];
}
);
}
encode_args_impl!();
encode_args_impl!(A);
encode_args_impl!(A, B);
encode_args_impl!(A, B, C);
encode_args_impl!(A, B, C, D);
encode_args_impl!(A, B, C, D, E);
encode_args_impl!(A, B, C, D, E, F);
encode_args_impl!(A, B, C, D, E, F, G);
encode_args_impl!(A, B, C, D, E, F, G, H);
encode_args_impl!(A, B, C, D, E, F, G, H, I);
encode_args_impl!(A, B, C, D, E, F, G, H, I, J);
encode_args_impl!(A, B, C, D, E, F, G, H, I, J, K);
encode_args_impl!(A, B, C, D, E, F, G, H, I, J, K, L);
#[cfg(test)]
mod tests {
use crate::runtime::{Class, Object, Sel};
use objc_encode::Encode;
#[test]
fn test_encode() {
assert!(<&Object>::ENCODING.to_string() == "@");
assert!(<*mut Object>::ENCODING.to_string() == "@");
assert!(<&Class>::ENCODING.to_string() == "#");
assert!(Sel::ENCODING.to_string() == ":");
}
}