Skip to main content

rust_macios/foundation/
macros.rs

1/// A macro to create new `NSArray`s.
2#[macro_export]
3macro_rules! nsarray {
4    () => {
5        $crate::foundation::NSArray::new()
6    };
7    ($($x:expr),*) => {
8        $crate::foundation::NSArray::from(vec![$($x),*])
9    };
10}
11
12/// A macro to create new `NSDictionary`s.
13#[macro_export]
14macro_rules! nsdictionary {
15    (@single $($x:tt)*) => (());
16    (@count $($rest:expr),*) => (<[()]>::len(&[$(nsdictionary!(@single $rest)),*]));
17
18    ($($key:expr => $value:expr,)+) => { nsdictionary!($($key => $value),+) };
19    ($($key:expr => $value:expr),*) => {
20        {
21            let capacity = nsdictionary!(@count $($key),*);
22            let mut map = $crate::foundation::NSMutableDictionary::with_capacity(capacity as u64);
23            $(
24                let _ = map.insert($key, $value);
25            )*
26            $crate::foundation::NSDictionary::from(map)
27        }
28    };
29}
30
31/// Respond to problem situations in your interactions with APIs, and fine-tune your app for better debugging.
32#[macro_export]
33macro_rules! nslog {
34    ($format:expr) => {
35        use $crate::foundation::NSLog;
36        unsafe {
37            NSLog($format.into());
38        }
39    };
40    ($format:expr, $($arg:expr),+) => {
41        use $crate::foundation::NSLog;
42        unsafe {
43            NSLog($format.into(), $($arg),+);
44        }
45    };
46}