tqwewe / kameo

Fault-tolerant Async Actors Built on Tokio
https://docs.page/tqwewe/kameo
Apache License 2.0
633 stars 16 forks source link

[FEATURE] - make try methods generic over mailbox. #81

Closed blueforesticarus closed 1 week ago

blueforesticarus commented 1 week ago

The following is not possible because try_send_sync is only defined for specific mailboxes. (same for all other send variants as near I can tell).

struct TriggerTask<T, A: kameo::Actor> {
    trigger: AtomicBool,
    task: T,
    actor_ref: ActorRef<A>,
}

impl<T, A> TriggerTask<T, A>
where
    T: Clone + Send + Sync,
    A: kameo::Actor + kameo::message::Message<T>,
{
    pub fn trigger_task(&mut self) {
        if !self
            .trigger
            .swap(true, std::sync::atomic::Ordering::Relaxed)
        {
            use kameo::request::TryMessageSendSync;
            self.actor_ref
                .tell(self.task.clone())
                .try_send_sync()
                .unwrap();
        }
    }
}
tqwewe commented 1 week ago

This code would actually compile on the main branch of kameo. I've done some updates to make some of the requests more flexbile. See https://github.com/tqwewe/kameo/pull/71. I need to publish a new version soon with some of the improvements present in main branch.

However, not all can be made generic. For example, you cannot call .send_sync() on a bounded mailbox with a tell request, since sending the message needs to await mailbox capacity.

If you haven't already seen this in the book, there's a nice table showing an overview of what request methods are available for each mailbox: https://docs.page/tqwewe/kameo/core-concepts/requests#supported-requests