Skip to main content

simconnect_sdk/domain/
notification.rs

1use crate::{
2    Airport, ClientEvent, SimConnectError, SimConnectObjectExt, SystemEvent, Waypoint, NDB, VOR,
3};
4
5/// Notification received from SimConnect.
6#[derive(Debug)]
7#[non_exhaustive]
8pub enum Notification {
9    /// SimConnect open
10    Open,
11    /// SimConnect client event
12    ClientEvent(ClientEvent),
13    /// SimConnect system event
14    SystemEvent(SystemEvent),
15    /// SimConnect object
16    Object(Object),
17    /// A list of [crate::Airport].
18    AirportList(Vec<Airport>),
19    /// A list of [crate::Waypoint].
20    WaypointList(Vec<Waypoint>),
21    /// A list of [crate::NDB].
22    NdbList(Vec<NDB>),
23    /// A list of [crate::VOR].
24    VorList(Vec<VOR>),
25    /// SimConnect quit
26    Quit,
27}
28
29/// Notification data object.
30#[derive(Debug)]
31pub struct Object {
32    pub(crate) type_name: String,
33    pub(crate) data_addr: *const u32,
34}
35
36impl Object {
37    /// Try and transmute this SimConnect object as a `T` struct.
38    ///
39    /// # Errors
40    /// - [`crate::SimConnectError::ObjectMismatch`] -- The type of this SimConnect object is different from `T`.
41    pub fn try_transmute<T: SimConnectObjectExt, I>(&self) -> Result<I, SimConnectError> {
42        let type_name: String = std::any::type_name::<T>().into();
43
44        if self.type_name == type_name {
45            let data: I = unsafe { std::ptr::read_unaligned(self.data_addr as *const I) };
46            Ok(data)
47        } else {
48            Err(SimConnectError::ObjectMismatch {
49                actual: self.type_name.clone(),
50                expected: type_name,
51            })
52        }
53    }
54}