Skip to content

Commit

Permalink
Added a "is" method to Msg, fixed the "msg!" macro to allow using "re…
Browse files Browse the repository at this point in the history
…turn" inside it and updated the "msg!" macro to allow to use it as a typed expression.
  • Loading branch information
r3v2d0g committed Dec 2, 2019
1 parent ca376bf commit be9a3e7
Showing 1 changed file with 41 additions and 34 deletions.
75 changes: 41 additions & 34 deletions bastion/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,15 @@ impl Msg {
}
}

#[doc(hidden)]
pub fn is<M: Message>(&self) -> bool {
match &self.0 {
MsgInner::Tell(msg) => msg.is::<M>(),
MsgInner::Ask { msg, .. } => msg.is::<M>(),
MsgInner::Broadcast(msg) => msg.is::<M>(),
}
}

#[doc(hidden)]
pub fn downcast<M: Message>(self) -> Result<M, Self> {
trace!("{:?}: Downcasting to {}.", self, type_name::<M>());
Expand Down Expand Up @@ -520,7 +529,7 @@ impl Future for Answer {
/// [`BastionContext::try_recv`]: context/struct.BastionContext.html#method.try_recv
macro_rules! msg {
($msg:expr, $($tokens:tt)+) => {
{ msg!(@internal $msg, (), (), (), $($tokens)+); }
msg!(@internal $msg, (), (), (), $($tokens)+)
};

(@internal
Expand Down Expand Up @@ -592,19 +601,21 @@ macro_rules! msg {
($($tvar:ident, $tty:ty, $thandle:expr,)*),
($($avar:ident, $aty:ty, $ahandle:expr,)*),
$var:ident: _ => $handle:expr;
) => {
) => { {
let mut $var = $msg;
let sender = $var.take_sender();
if $var.is_broadcast() {
if false {}
if false {
unreachable!();
}
$(
else if let Some($bvar) = $var.downcast_ref::<$bty>() {
let $bvar = &*$bvar;
{ $bhandle };
else if $var.is::<$bty>() {
let $bvar = &*$var.downcast_ref::<$bty>().unwrap();
{ $bhandle }
}
)*
else {
{ $handle };
{ $handle }
}
} else if sender.is_some() {
let sender = sender.unwrap();
Expand All @@ -614,35 +625,31 @@ macro_rules! msg {
};
}

loop {
$(
match $var.downcast::<$aty>() {
Ok($avar) => {
{ $ahandle };
break;
}
Err(msg_) => $var = msg_,
}
)*

{ $handle };
break;
if false {
unreachable!();
}
$(
else if $var.is::<$aty>() {
let $avar = $var.downcast::<$aty>().unwrap();
{ $ahandle }
}
)*
else {
{ $handle }
}
} else {
loop {
$(
match $var.downcast::<$tty>() {
Ok($tvar) => {
{ $thandle };
break;
}
Err(msg_) => $var = msg_,
}
)*

{ $handle };
break;
if false {
unreachable!();
}
$(
else if $var.is::<$tty>() {
let $tvar = $var.downcast::<$tty>().unwrap();
{ $thandle }
}
)*
else {
{ $handle }
}
}
};
} };
}

0 comments on commit be9a3e7

Please sign in to comment.