about summary refs log tree commit diff
path: root/library/core/src/os/darwin/objc.rs
blob: df3aab867e83d0cd1c3fce9f2740ddc54dcd93d1 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//! Defines types and macros for Objective-C interoperability.

#![unstable(feature = "darwin_objc", issue = "145496")]
#![allow(nonstandard_style)]

use crate::fmt;

/// Equivalent to Objective-C’s `struct objc_class` type.
#[repr(u8)]
pub enum objc_class {
    #[unstable(
        feature = "objc_class_variant",
        reason = "temporary implementation detail",
        issue = "none"
    )]
    #[doc(hidden)]
    __variant1,
    #[unstable(
        feature = "objc_class_variant",
        reason = "temporary implementation detail",
        issue = "none"
    )]
    #[doc(hidden)]
    __variant2,
}

impl fmt::Debug for objc_class {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("objc_class").finish()
    }
}

/// Equivalent to Objective-C’s `struct objc_selector` type.
#[repr(u8)]
pub enum objc_selector {
    #[unstable(
        feature = "objc_selector_variant",
        reason = "temporary implementation detail",
        issue = "none"
    )]
    #[doc(hidden)]
    __variant1,
    #[unstable(
        feature = "objc_selector_variant",
        reason = "temporary implementation detail",
        issue = "none"
    )]
    #[doc(hidden)]
    __variant2,
}

impl fmt::Debug for objc_selector {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("objc_selector").finish()
    }
}

/// Equivalent to Objective-C’s `Class` type.
pub type Class = *mut objc_class;

/// Equivalent to Objective-C’s `SEL` type.
pub type SEL = *mut objc_selector;

/// Gets a reference to an Objective-C class.
///
/// This macro will yield an expression of type [`Class`] for the given class name string literal.
///
/// # Example
///
/// ```no_run
/// #![feature(darwin_objc)]
/// use core::os::darwin::objc;
///
/// let string_class = objc::class!("NSString");
/// ```
#[allow_internal_unstable(rustc_attrs)]
pub macro class($classname:expr) {{
    // Since static Objective-C class references actually end up with multiple definitions
    // across dylib boundaries, we only expose the value of the static and don't provide a way to
    // get the address of or a reference to the static.
    unsafe extern "C" {
        #[rustc_objc_class = $classname]
        safe static VAL: $crate::os::darwin::objc::Class;
    }
    VAL
}}

/// Gets a reference to an Objective-C selector.
///
/// This macro will yield an expression of type [`SEL`] for the given method name string literal.
///
/// It is similar to Objective-C’s `@selector` directive.
///
/// # Examples
///
/// ```no_run
/// #![feature(darwin_objc)]
/// use core::os::darwin::objc;
///
/// let alloc_sel = objc::selector!("alloc");
/// let init_sel = objc::selector!("initWithCString:encoding:");
/// ```
#[allow_internal_unstable(rustc_attrs)]
pub macro selector($methname:expr) {{
    // Since static Objective-C selector references actually end up with multiple definitions
    // across dylib boundaries, we only expose the value of the static and don't provide a way to
    // get the address of or a reference to the static.
    unsafe extern "C" {
        #[rustc_objc_selector = $methname]
        safe static VAL: $crate::os::darwin::objc::SEL;
    }
    VAL
}}