azul_core/
macros.rs

1#![allow(unused_macros)]
2
3/// Implements functions for `CallbackInfo` and `Info`,
4/// to prevent duplicating the functions
5#[macro_export]
6macro_rules! impl_task_api {
7    () => {
8        /// Insert a timer into the list of active timers.
9        /// Replaces the existing timer if called with the same TimerId.
10        pub fn add_timer(&mut self, id: TimerId, timer: Timer) {
11            self.timers.insert(id, timer);
12        }
13
14        /// Returns if a timer with the given ID is currently running
15        pub fn has_timer(&self, timer_id: &TimerId) -> bool {
16            self.get_timer(timer_id).is_some()
17        }
18
19        /// Returns a reference to an existing timer (if the `TimerId` is valid)
20        pub fn get_timer(&self, timer_id: &TimerId) -> Option<&Timer> {
21            self.timers.get(&timer_id)
22        }
23
24        /// Deletes a timer and returns it (if the `TimerId` is valid)
25        pub fn delete_timer(&mut self, timer_id: &TimerId) -> Option<Timer> {
26            self.timers.remove(timer_id)
27        }
28
29        /// Adds a (thread-safe) `Task` to the app that runs on a different thread
30        pub fn add_task(&mut self, task: Task) {
31            self.tasks.push(task);
32        }
33    };
34}
35
36/// Implement the `From` trait for any type.
37/// Example usage:
38/// ```
39/// # use azul_core::impl_from;
40/// enum MyError<'a> {
41///     Bar(BarError<'a>)
42///     Foo(FooError<'a>)
43/// }
44///
45/// impl_from!(BarError<'a>, Error::Bar);
46/// impl_from!(BarError<'a>, Error::Bar);
47/// ```
48#[macro_export]
49macro_rules! impl_from {
50    // From a type with a lifetime to a type which also has a lifetime
51    ($a:ident < $c:lifetime > , $b:ident:: $enum_type:ident) => {
52        impl<$c> From<$a<$c>> for $b<$c> {
53            fn from(e: $a<$c>) -> Self {
54                $b::$enum_type(e)
55            }
56        }
57    };
58
59    // From a type without a lifetime to a type which also does not have a lifetime
60    ($a:ident, $b:ident:: $enum_type:ident) => {
61        impl From<$a> for $b {
62            fn from(e: $a) -> Self {
63                $b::$enum_type(e)
64            }
65        }
66    };
67}
68
69/// Implement `Display` for an enum.
70///
71/// Example usage:
72/// ```
73/// # use azul_core::impl_display;
74/// enum Foo<'a> {
75///     Bar(&'a str),
76///     Baz(i32),
77/// }
78///
79/// impl_display! { Foo<'a>, {
80///     Bar(s) => s,
81///     Baz(i) => format!("{}", i)
82/// }}
83/// ```
84#[macro_export]
85macro_rules! impl_display {
86    // For a type with a lifetime
87    ($enum:ident<$lt:lifetime>, {$($variant:pat => $fmt_string:expr),+$(,)* }) => {
88
89        impl<$lt> ::core::fmt::Display for $enum<$lt> {
90            fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
91                use self::$enum::*;
92                match &self {
93                    $(
94                        $variant => write!(f, "{}", $fmt_string),
95                    )+
96                }
97            }
98        }
99
100    };
101
102    // For a type without a lifetime
103    ($enum:ident, {$($variant:pat => $fmt_string:expr),+$(,)* }) => {
104
105        impl ::core::fmt::Display for $enum {
106            fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
107                use self::$enum::*;
108                match &self {
109                    $(
110                        $variant => write!(f, "{}", $fmt_string),
111                    )+
112                }
113            }
114        }
115
116    };
117}