File: rs_sys_impls.rs

package info (click to toggle)
rust-wayland-backend 0.3.11-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 776 kB
  • sloc: ansic: 28; makefile: 2
file content (172 lines) | stat: -rw-r--r-- 4,745 bytes parent folder | download | duplicates (3)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#![allow(clippy::test_attr_in_doctest)]

//! Tests to ensure the rust and sys types implement the same traits.

/// A macro used to assert a type defined in both the rust and sys implementations of wayland-backend
/// implement the same traits.
///
/// There are four patterns which may be matched using this macro.
///
/// For example, assume you want to make sure both the rust and sys versions of `ObjectId` implement [`Debug`].
/// The following pattern would achieve that check.
///
/// ```no_run
/// #[test]
/// fn test() {
///     assert_impl!(server::ObjectId: std::fmt::Debug);
/// }
/// ```
///
/// Multiple traits may be tested by separating each trait with a comma.
///
/// ```no_run
/// #[test]
/// fn test() {
///     assert_impl!(server::ObjectId: std::fmt::Debug, Send, Sync);
/// }
/// ```
///
/// For the client side, simply change the path before the name of the type.
///
/// ```no_run
/// #[test]
/// fn test() {
///     assert_impl!(client::ObjectId: std::fmt::Debug)
/// }
/// ```
///
/// Traits may be tested through prefixing the contents of the macro with `dyn`.
///
/// ```ignore
/// #[test]
/// fn test() {
///     assert_impl!(dyn server::SomeTraitWithNoGeneric: std::fmt::Debug)
/// }
/// ```
///
/// Finally, generics may also be tested by simply adding the generics as expected in a normal type. Do note
/// you will need to monomorphize the type with something, such as, `()`, the unit type.
///
/// ```no_run
/// #[test]
/// fn test() {
///     assert_impl!(server::Backend<()>: Send, Sync); // No trait
///     assert_impl!(dyn server::ObjectData<()>: std::fmt::Debug); // Trait
/// }
/// ```
macro_rules! assert_impl {
    // No type parameters with dyn
    (
        dyn $side: ident::$ty: ident: $($trait_: path),+
    ) => {{
        #[allow(dead_code)]
        fn assert_impl<T: $($trait_ +)* ?Sized>() {}

        assert_impl::<dyn wayland_backend::rs::$side::$ty>();
        #[cfg(feature = "server_system")]
        assert_impl::<dyn wayland_backend::sys::$side::$ty>();
    }};

    // Type parameters with dyn
    (
        dyn $side: ident::$ty: ident<$($types: ty),*>: $($trait_: path),+
    ) => {{
        #[allow(dead_code)]
        fn assert_impl<T: $($trait_ +)* ?Sized>() {}

        assert_impl::<dyn wayland_backend::rs::$side::$ty <$($types),*>>();
        #[cfg(feature = "server_system")]
        assert_impl::<dyn wayland_backend::sys::$side::$ty <$($types),*>>();
    }};

    // No type parameters
    (
        $side: ident::$ty: ident: $($trait_: path),+
    ) => {{
        #[allow(dead_code)]
        fn assert_impl<T: $($trait_ +)* ?Sized>() {}

        assert_impl::<wayland_backend::rs::$side::$ty>();
        #[cfg(feature = "server_system")]
        assert_impl::<wayland_backend::sys::$side::$ty>();
    }};

    // Type parameters
    (
        $side: ident::$ty: ident<$($types: ty),*>: $($trait_: path),+
    ) => {{
        #[allow(dead_code)]
        fn assert_impl<T: $($trait_ +)* ?Sized>() {}

        assert_impl::<wayland_backend::rs::$side::$ty <$($types),*>>();
        #[cfg(feature = "server_system")]
        assert_impl::<wayland_backend::sys::$side::$ty <$($types),*>>();
    }};
}

mod server {
    #[test]
    fn test_impls() {
        // ObjectData
        assert_impl!(
            dyn server::ObjectData<()>: std::fmt::Debug, downcast_rs::DowncastSync
        );

        // GlobalHandler
        assert_impl!(
            dyn server::GlobalHandler<()>: std::fmt::Debug, downcast_rs::DowncastSync
        );

        // ClientData
        assert_impl!(
            dyn server::ClientData: std::fmt::Debug, downcast_rs::DowncastSync
        );

        // ObjectId
        assert_impl!(
            server::ObjectId: std::fmt::Debug,
            std::fmt::Display,
            Send,
            Sync,
            PartialEq,
            Eq,
            Clone
        );

        // ClientId
        assert_impl!(server::ClientId: std::fmt::Debug, Clone, Send, Sync, PartialEq, Eq);

        // GlobalId
        assert_impl!(server::GlobalId: std::fmt::Debug, Clone, Send, Sync, PartialEq, Eq);

        // Handle
        assert_impl!(server::Handle: std::fmt::Debug);

        // Backend
        assert_impl!(server::Backend<()>: Send, Sync);
    }
}

mod client {
    #[test]
    fn test_impls() {
        // ObjectData
        assert_impl!(
            dyn client::ObjectData: std::fmt::Debug, downcast_rs::DowncastSync
        );

        // ObjectId
        assert_impl!(
            client::ObjectId: std::fmt::Debug,
            std::fmt::Display,
            Clone,
            Send,
            Sync,
            PartialEq,
            Eq
        );

        // Backend
        assert_impl!(client::Backend: Send, Sync, std::fmt::Debug);
    }
}