hyperactor/
cap.rs

1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9//! Capabilities used in various public APIs.
10
11/// CanSend is a capabilty to confers the right of the holder to send
12/// messages to actors. CanSend is sealed and may only be implemented
13/// and accessed by this crate.
14pub trait CanSend: sealed::CanSend {}
15impl<T: sealed::CanSend> CanSend for T {}
16
17/// CanOpenPort is a capability that confers the ability of hte holder to
18/// open local ports, which can then be used to receive messages.
19pub trait CanOpenPort: sealed::CanOpenPort {}
20impl<T: sealed::CanOpenPort> CanOpenPort for T {}
21
22/// CanOpenPort is a capability that confers the ability of the holder to
23/// split ports.
24pub trait CanSplitPort: sealed::CanSplitPort {}
25impl<T: sealed::CanSplitPort> CanSplitPort for T {}
26
27/// CanSpawn is a capability that confers the ability to spawn a child
28/// actor.
29pub trait CanSpawn: sealed::CanSpawn {}
30impl<T: sealed::CanSpawn> CanSpawn for T {}
31
32/// CanResolveActorRef is a capability that confers the ability to resolve
33/// an ActorRef to a local ActorHandle if the actor is available locally.
34pub trait CanResolveActorRef: sealed::CanResolveActorRef {}
35impl<T: sealed::CanResolveActorRef> CanResolveActorRef for T {}
36
37pub(crate) mod sealed {
38    use async_trait::async_trait;
39
40    use crate::ActorId;
41    use crate::ActorRef;
42    use crate::PortId;
43    use crate::accum::ReducerSpec;
44    use crate::actor::Actor;
45    use crate::actor::ActorHandle;
46    use crate::actor::RemoteActor;
47    use crate::attrs::Attrs;
48    use crate::data::Serialized;
49    use crate::mailbox::Mailbox;
50
51    pub trait CanSend: Send + Sync {
52        fn post(&self, dest: PortId, headers: Attrs, data: Serialized);
53        fn actor_id(&self) -> &ActorId;
54    }
55
56    pub trait CanOpenPort: Send + Sync {
57        fn mailbox(&self) -> &Mailbox;
58    }
59
60    pub trait CanSplitPort: Send + Sync {
61        fn split(
62            &self,
63            port_id: PortId,
64            reducer_spec: Option<ReducerSpec>,
65        ) -> anyhow::Result<PortId>;
66    }
67
68    #[async_trait]
69    pub trait CanSpawn: Send + Sync {
70        async fn spawn<A: Actor>(&self, params: A::Params) -> anyhow::Result<ActorHandle<A>>;
71    }
72
73    pub trait CanResolveActorRef: Send + Sync {
74        fn resolve_actor_ref<A: RemoteActor + Actor>(
75            &self,
76            actor_ref: &ActorRef<A>,
77        ) -> Option<ActorHandle<A>>;
78    }
79}