Add inspect
Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
parent
f3f816acd9
commit
66971a4f26
2 changed files with 71 additions and 2 deletions
|
|
@ -41,6 +41,10 @@ impl InternalMessage {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn as_ref<M: Any>(&self) -> Option<&M> {
|
||||||
|
self.value.downcast_ref()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn type_name(&self) -> &'static str {
|
pub fn type_name(&self) -> &'static str {
|
||||||
self.name
|
self.name
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,12 +12,17 @@ use tytix_core::anyhow;
|
||||||
pub trait AddressExt<MB> {
|
pub trait AddressExt<MB> {
|
||||||
fn map_message<F, M, R, U>(self, f: F) -> MappedBeforeAddress<Self, F, M, R>
|
fn map_message<F, M, R, U>(self, f: F) -> MappedBeforeAddress<Self, F, M, R>
|
||||||
where
|
where
|
||||||
MB: MessageBundle,
|
|
||||||
F: Fn(M) -> U + 'static,
|
F: Fn(M) -> U + 'static,
|
||||||
U: Future<Output = R>,
|
U: Future<Output = R>,
|
||||||
M: Message,
|
M: Message,
|
||||||
R: Message + IsContainedInBundle<MB>,
|
R: Message + IsContainedInBundle<MB>,
|
||||||
Self: Sized;
|
Self: Sized;
|
||||||
|
|
||||||
|
fn inspect<F, M, U>(self, f: F) -> Inspect<Self, F, U, M>
|
||||||
|
where
|
||||||
|
F: Fn(&M) -> U,
|
||||||
|
M: Message + IsContainedInBundle<MB>,
|
||||||
|
Self: Sized;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<MB: MessageBundle, A: Address<MB>> AddressExt<MB> for A {
|
impl<MB: MessageBundle, A: Address<MB>> AddressExt<MB> for A {
|
||||||
|
|
@ -42,6 +47,48 @@ impl<MB: MessageBundle, A: Address<MB>> AddressExt<MB> for A {
|
||||||
_pd: PhantomData,
|
_pd: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn inspect<F, M, U>(self, f: F) -> Inspect<Self, F, U, M>
|
||||||
|
where
|
||||||
|
F: Fn(&M) -> U,
|
||||||
|
Self: Sized,
|
||||||
|
M: Message + IsContainedInBundle<MB>,
|
||||||
|
{
|
||||||
|
const {
|
||||||
|
let true = <M as IsContainedInBundle<MB>>::IS_CONTAINED else {
|
||||||
|
panic!("Message is not contained in MessageBundle",);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
Inspect {
|
||||||
|
address: self,
|
||||||
|
func: f,
|
||||||
|
_pd: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Inspect<A, F, U, M> {
|
||||||
|
address: A,
|
||||||
|
func: F,
|
||||||
|
_pd: PhantomData<fn(&M) -> U>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A, F, U, M> InternalMessageHandler for Inspect<A, F, U, M>
|
||||||
|
where
|
||||||
|
A: InternalMessageHandler,
|
||||||
|
M: Message,
|
||||||
|
F: Fn(&M) -> U,
|
||||||
|
U: Future<Output = ()>,
|
||||||
|
{
|
||||||
|
type HandledMessages = A::HandledMessages;
|
||||||
|
async fn handle_message(&mut self, msg: InternalMessage) -> anyhow::Result<InternalMessage> {
|
||||||
|
if let Some(msg) = msg.as_ref() {
|
||||||
|
(self.func)(msg).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.address.handle_message(msg).await
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct MappedBeforeAddress<A, F, M, R> {
|
pub struct MappedBeforeAddress<A, F, M, R> {
|
||||||
|
|
@ -117,7 +164,7 @@ mod tests {
|
||||||
struct SimpleAddress;
|
struct SimpleAddress;
|
||||||
|
|
||||||
impl InternalMessageHandler for SimpleAddress {
|
impl InternalMessageHandler for SimpleAddress {
|
||||||
type HandledMessages = (Foo,);
|
type HandledMessages = (Foo, Bar);
|
||||||
|
|
||||||
fn handle_message(
|
fn handle_message(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
|
@ -142,4 +189,22 @@ mod tests {
|
||||||
|
|
||||||
MSG.get().expect("The message was mapped!");
|
MSG.get().expect("The message was mapped!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[apply(test!)]
|
||||||
|
async fn check_inspect() {
|
||||||
|
static MSG: OnceLock<bool> = OnceLock::new();
|
||||||
|
|
||||||
|
let mut sa = SimpleAddress.inspect(|_b: &Bar| {
|
||||||
|
let _ = MSG.set(true);
|
||||||
|
async {}
|
||||||
|
});
|
||||||
|
|
||||||
|
sa.send(Foo).await.unwrap();
|
||||||
|
|
||||||
|
assert!(MSG.get().is_none());
|
||||||
|
|
||||||
|
sa.send(Bar).await.unwrap();
|
||||||
|
|
||||||
|
MSG.get().expect("The message was inspected!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue