diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2018-10-01 17:47:11 +0200 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2018-10-01 18:04:08 +0200 |
| commit | bad36b9606f264478bdf5bf80406b8cff4a85d1c (patch) | |
| tree | 0368179dbf2613d67bbf118d7e56f86182cb9d49 | |
| parent | 1b57f87f736fee17c85029252fe976fc392be0e1 (diff) | |
| download | mullvadvpn-bad36b9606f264478bdf5bf80406b8cff4a85d1c.tar.xz mullvadvpn-bad36b9606f264478bdf5bf80406b8cff4a85d1c.zip | |
Generate TunnelStateWrapper with macro
| -rw-r--r-- | talpid-core/src/tunnel_state_machine/mod.rs | 67 |
1 files changed, 26 insertions, 41 deletions
diff --git a/talpid-core/src/tunnel_state_machine/mod.rs b/talpid-core/src/tunnel_state_machine/mod.rs index f1ab145137..87bc2b157a 100644 --- a/talpid-core/src/tunnel_state_machine/mod.rs +++ b/talpid-core/src/tunnel_state_machine/mod.rs @@ -7,7 +7,6 @@ mod connecting_state; mod disconnected_state; mod disconnecting_state; -use std::fmt::{Debug, Formatter, Result as FmtResult}; use std::path::{Path, PathBuf}; use std::sync::mpsc as sync_mpsc; use std::thread; @@ -281,16 +280,32 @@ trait TunnelState: Into<TunnelStateWrapper> + Sized { ) -> EventConsequence<Self>; } -/// Valid states of the tunnel. -/// -/// All implementations must implement `TunnelState` so that they can handle events and -/// commands in order to advance the state machine. -enum TunnelStateWrapper { - Disconnected(DisconnectedState), - Connecting(ConnectingState), - Connected(ConnectedState), - Disconnecting(DisconnectingState), - Blocked(BlockedState), +macro_rules! state_wrapper { + (enum $wrapper_name:ident { $($state_variant:ident($state_type:ident)),* $(,)* }) => { + /// Valid states of the tunnel. + /// + /// All implementations must implement `TunnelState` so that they can handle events and + /// commands in order to advance the state machine. + enum $wrapper_name { + $($state_variant($state_type),)* + } + + $(impl From<$state_type> for $wrapper_name { + fn from(state: $state_type) -> Self { + $wrapper_name::$state_variant(state) + } + })* + } +} + +state_wrapper! { + enum TunnelStateWrapper { + Disconnected(DisconnectedState), + Connecting(ConnectingState), + Connected(ConnectedState), + Disconnecting(DisconnectingState), + Blocked(BlockedState), + } } impl TunnelStateWrapper { @@ -330,33 +345,3 @@ impl TunnelStateWrapper { } } } - -macro_rules! impl_from_for_tunnel_state { - ($state_variant:ident($state_type:ident)) => { - impl From<$state_type> for TunnelStateWrapper { - fn from(state: $state_type) -> Self { - TunnelStateWrapper::$state_variant(state) - } - } - }; -} - -impl_from_for_tunnel_state!(Disconnected(DisconnectedState)); -impl_from_for_tunnel_state!(Connecting(ConnectingState)); -impl_from_for_tunnel_state!(Connected(ConnectedState)); -impl_from_for_tunnel_state!(Disconnecting(DisconnectingState)); -impl_from_for_tunnel_state!(Blocked(BlockedState)); - -impl Debug for TunnelStateWrapper { - fn fmt(&self, formatter: &mut Formatter) -> FmtResult { - use self::TunnelStateWrapper::*; - - match *self { - Disconnected(_) => write!(formatter, "TunnelStateWrapper::Disconnected(_)"), - Connecting(_) => write!(formatter, "TunnelStateWrapper::Connecting(_)"), - Connected(_) => write!(formatter, "TunnelStateWrapper::Connected(_)"), - Disconnecting(_) => write!(formatter, "TunnelStateWrapper::Disconnecting(_)"), - Blocked(_) => write!(formatter, "TunnelStateWrapper::Blocked(_)"), - } - } -} |
