Simplify the API bounds

Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
Marcel Müller 2025-11-04 12:50:44 +01:00
parent 9fac3f08a1
commit 44e8280093

View file

@ -11,13 +11,9 @@ use tytix_core::anyhow;
pub type ReplyOf<M> = <M as Message>::Reply; pub type ReplyOf<M> = <M as Message>::Reply;
pub trait AddressExt<MB> { pub trait AddressExt<MB> {
fn map<M, R, F, U, RF, RU>(self, f: F, r: RF) -> MappedMessage<Self, M, F, U, RF, RU> fn map<M, F, U, RF, RU>(self, f: F, r: RF) -> MappedMessage<Self, M, F, U, RF, RU>
where where
M: Message, MappedMessage<Self, M, F, U, RF, RU>: InternalMessageHandler,
R: Message + IsContainedInBundle<MB>,
F: FnMut(M) -> U + 'static,
U: Future<Output = R>,
RF: FnMut(<R as Message>::Reply) -> RU + 'static,
Self: Sized; Self: Sized;
fn inspect<F, M, U>(self, f: F) -> Inspect<Self, F, U, M> fn inspect<F, M, U>(self, f: F) -> Inspect<Self, F, U, M>
@ -28,20 +24,11 @@ pub trait AddressExt<MB> {
} }
impl<MB: MessageBundle, A: InternalMessageHandler<HandledMessages = MB>> AddressExt<MB> for A { impl<MB: MessageBundle, A: InternalMessageHandler<HandledMessages = MB>> AddressExt<MB> for A {
fn map<M, R, F, U, RF, RU>(self, f: F, r: RF) -> MappedMessage<Self, M, F, U, RF, RU> fn map<M, F, U, RF, RU>(self, f: F, r: RF) -> MappedMessage<Self, M, F, U, RF, RU>
where where
M: Message, MappedMessage<Self, M, F, U, RF, RU>: InternalMessageHandler,
R: Message + IsContainedInBundle<MB>,
F: FnMut(M) -> U + 'static,
RF: FnMut(<R as Message>::Reply) -> RU + 'static,
Self: Sized, Self: Sized,
{ {
const {
let true = <R as IsContainedInBundle<MB>>::IS_CONTAINED else {
panic!("Message is not contained in MessageBundle",);
};
}
MappedMessage { MappedMessage {
address: self, address: self,
func: f, func: f,
@ -166,7 +153,7 @@ mod tests {
struct Foo; struct Foo;
impl Message for Foo { impl Message for Foo {
type Reply = (); type Reply = usize;
} }
struct Bar; struct Bar;
@ -180,13 +167,15 @@ mod tests {
impl InternalMessageHandler for SimpleAddress { impl InternalMessageHandler for SimpleAddress {
type HandledMessages = (Foo, Bar); type HandledMessages = (Foo, Bar);
fn handle_message( async fn handle_message(
&mut self, &mut self,
msg: tytix_core::InternalMessage, msg: tytix_core::InternalMessage,
) -> impl Future<Output = anyhow::Result<InternalMessage>> { ) -> anyhow::Result<InternalMessage> {
drop(msg); if let Ok(_foo) = msg.into_inner::<Foo>() {
Ok(InternalMessage::new(42usize))
async { Ok(InternalMessage::new(())) } } else {
Ok(InternalMessage::new(()))
}
} }
} }
@ -202,7 +191,7 @@ mod tests {
|_a| async {}, |_a| async {},
); );
sa.send(Bar).await.unwrap(); let () = sa.send(Bar).await.unwrap();
MSG.get().expect("The message was mapped!"); MSG.get().expect("The message was mapped!");
} }