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
macro_rules! impl_event_helpers {
($raw_type:ty) => {
/// Returns a shared reference to the underlying raw, C-FFI compatible event struct.
#[inline]
pub const fn as_raw(&self) -> &$raw_type {
&self.inner
}
/// Returns an exclusive, mutable reference to the raw, C-FFI compatible event struct.
///
/// # Safety
///
/// This method allows mutating the event header's fields, most notably `type_`, `size` and
/// `space_id`.
///
/// Modifying those fields may trigger Undefined Behavior, as it can make other readers of this
/// event treat it as a type or size that it is actually not.
#[inline]
pub const unsafe fn as_raw_mut(&mut self) -> &mut $raw_type {
&mut self.inner
}
/// Creates a new event of this type from a reference to a raw, C-FFI compatible event
/// struct.
///
/// # Panics
///
/// This method will panic if the given event struct's header doesn't actually match
/// the expected event type.
#[inline]
pub const fn from_raw(raw: &$raw_type) -> Self {
crate::events::ensure_event_matches::<Self>(&raw.header);
Self { inner: *raw }
}
/// Creates a reference to an event of this type from a reference to a raw,
/// C-FFI compatible event struct.
///
/// # Panics
///
/// This method will panic if the given event struct's header doesn't actually match
/// the expected event type.
#[inline]
pub const fn from_raw_ref(raw: &$raw_type) -> &Self {
crate::events::ensure_event_matches::<Self>(&raw.header);
// SAFETY: This type is #[repr(C)]-compatible with $raw_type
unsafe { &*(raw as *const $raw_type as *const Self) }
}
/// Creates a mutable reference to a note event of this type from a reference to a raw,
/// C-FFI compatible event struct.
///
/// # Panics
///
/// This method will panic if the given event struct's header doesn't actually match
/// the expected note event type.
#[inline]
pub const fn from_raw_mut(raw: &mut $raw_type) -> &mut Self {
crate::events::ensure_event_matches::<Self>(&raw.header);
// SAFETY: This type is #[repr(C)]-compatible with $raw_type
unsafe { &mut *(raw as *mut $raw_type as *mut Self) }
}
};
}
pub(crate) use impl_event_helpers;