async_mavlink/
types.rs

1use std::hash::{Hash, Hasher};
2use std::mem::{discriminant, Discriminant};
3
4use mavlink::Message;
5
6use thiserror::Error;
7
8// TODO find better names for all of this
9/// Error type
10#[derive(Error, Debug)]
11pub enum AsyncMavlinkError {
12    /// IO Error encountered when trying to communicate with the MAV
13    #[error("connection to MAV lost")]
14    ConnectionLost(#[from] std::io::Error),
15
16    /// The event loop does not take our call
17    #[error("unable to emit task to event loop")]
18    TaskEmit(#[from] futures::channel::mpsc::SendError),
19
20    /// The event loop canceled our send request
21    #[error("the event loop canceled our send ack channel")]
22    SendAck(#[from] futures::channel::oneshot::Canceled),
23}
24
25/// Representation of the type of a specific MavMessage
26pub struct MavMessageType<M: Message>(Discriminant<M>);
27impl<M: mavlink::Message> Eq for MavMessageType<M> {}
28impl<M: mavlink::Message> PartialEq for MavMessageType<M> {
29    fn eq(&self, rhs: &Self) -> bool {
30        self.0.eq(&rhs.0)
31    }
32}
33impl<M: mavlink::Message> Hash for MavMessageType<M> {
34    fn hash<H>(&self, hasher: &mut H)
35    where
36        H: Hasher,
37    {
38        self.0.hash(hasher)
39    }
40}
41
42impl<M: Message> MavMessageType<M> {
43    /// Returns the `MavMessageType` of a `MavMessage`
44    ///
45    /// # Arguments
46    ///
47    /// * `message` - The message whose type shall be represented
48    /// # Examples
49    ///
50    /// ```
51    /// use mavlink::common::MavMessage;
52    /// use async_mavlink::prelude::*;
53    ///
54    /// let message_type = MavMessageType::new(&MavMessage::PARAM_VALUE(Default::default()));
55    /// ```
56    pub fn new(message: &M) -> MavMessageType<M> {
57        #[allow(clippy::mem_discriminant_non_enum)]
58        Self(discriminant(message))
59    }
60}