Skip to content

Commit ae3bcc2

Browse files
committed
[hyperactor] alias! -> behavior!
Pull Request resolved: #1458 The name `alias!` has bothered me for a long time. It does not create an alias, but rather it defines a collection of messages represented by a single newtype -- Erlang calls this concept a "behavior", so we adopt it here. Differential Revision: [D84091583](https://our.internmc.facebook.com/intern/diff/D84091583/) **NOTE FOR REVIEWERS**: This PR has internal Meta-specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D84091583/)! ghstack-source-id: 314654044
1 parent d761e0a commit ae3bcc2

File tree

13 files changed

+56
-52
lines changed

13 files changed

+56
-52
lines changed

docs/source/books/hyperactor-book/src/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@
4747
- [`#[derive(Named)]`](macros/named.md)
4848
- [`#[export]`](macros/export.md)
4949
- [`#[forward]`](macros/forward.md)
50-
- [`#[alias]`](macros/alias.md)
50+
- [`#[behavior]`](macros/behavior.md)
5151
- [Appendix](appendix/index.md)
5252
- [Typed Message Lifecycle](appendix/lifecycle.md)

docs/source/books/hyperactor-book/src/macros/alias.md renamed to docs/source/books/hyperactor-book/src/macros/behavior.md

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
# `#[alias]`
1+
# `#[behavior]`
22

3-
The `#[alias]` macro defines a façade actor type that exposes only a selected set of messages.
4-
This allows you to hand out **stable or restricted APIs** without tying clients to the full concrete actor type.
3+
The `#[behavior]` macro defines a type which represents an actor that handles a set of messages.
4+
Behaviors allows you to define and hand out **stable or restricted APIs** without tying clients
5+
to the full concrete actor type.
56

6-
### Defining an alias
7+
### Defining a behavior
78

8-
An alias groups together one or more message enums or structs:
9+
A behavior groups together one or more message enums or structs:
910

1011
```rust
1112
#[derive(Handler)]
@@ -28,23 +29,23 @@ struct GetItemCount<C> {
2829
reply: OncePortRef<C>,
2930
}
3031

31-
// Define an alias actor `ShoppingApi` that exposes exactly these messages.
32-
hyperactor::alias!(
32+
// Define a behavior `ShoppingApi` that exposes exactly these messages.
33+
hyperactor::behavior!(
3334
ShoppingApi,
3435
ShoppingList,
3536
ClearList,
3637
GetItemCount<usize>,
3738
);
3839
```
3940

40-
The alias can include:
41+
The behavior can include any type of message:
4142
- Enums (e.g. `ShoppingList`)
4243
- Struct messages (e.g. `ClearList`, `GetItemCount<usize>`)
4344
- Generic messages, with concrete type parameters bound at the alias site.
4445

45-
### Using an alias
46+
### Using a behavior
4647

47-
After spawning the real actor, re-type its id as the alias:
48+
After spawning the real actor, re-type its id as the behavior:
4849

4950
```rust
5051
let mut proc = Proc::local();
@@ -69,14 +70,14 @@ println!("items containing 'dairy': {n}");
6970
shopping_api.clear_list(&client, "end of session".into()).await?;
7071
```
7172

72-
> **Note:** `alias!` does *not* rename methods. It authorizes those calls on
73+
> **Note:** `behavior!` does *not* rename methods. It authorizes those calls on
7374
> `ActorRef<ShoppingApi>` if and only if the message type was included.
7475
7576
> **Note:** `attest` is a low-level escape hatch. It asserts that a raw
76-
> `ActorId` is valid for the alias type. This example uses it only because
77+
> `ActorId` is valid for the behavior. This example uses it only because
7778
> we just spawned the actor and know the id is safe.
7879
> In general, prefer higher-level APIs (such as `Proc` utilities) for
79-
> constructing alias references, and use `attest` sparingly.
80+
> constructing behavior references, and use `attest` sparingly.
8081
8182
### Generated code (excerpt)
8283

@@ -108,12 +109,12 @@ impl hyperactor::actor::RemoteHandles<GetItemCount<usize>> for ShoppingApi {}
108109

109110
### Capability slicing
110111

111-
If a message type is not listed in the alias, trying to call it will fail at compile time:
112+
If a message type is not listed in the behavior, trying to call it will fail at compile time:
112113

113114
```rust
114-
// If ClearList were omitted from the alias:
115+
// If ClearList were omitted from the behavior:
115116
shopping_api.clear_list(&client, "...").await?;
116117
// ^ error: the trait bound `ShoppingApi: RemoteHandles<ClearList>` is not satisfied
117118
```
118119

119-
This makes `alias!` a useful tool for **compile-time capability control**.
120+
This makes `behavior!` a useful tool for **compile-time capability control**.

hyperactor/example/derive.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ impl GetItemCountHandler<usize> for ShoppingListActor {
129129
}
130130
}
131131

132-
// Define an alias actor `ShoppingApi`. Clients can use
132+
// Define a behavior `ShoppingApi`. Clients can use
133133
// `ActorRef<ShoppingApi>` instead of referencing the concrete
134134
// `ShoppingListActor` directly.
135-
hyperactor::alias!(ShoppingApi, ShoppingList, ClearList, GetItemCount<usize>,);
135+
hyperactor::behavior!(ShoppingApi, ShoppingList, ClearList, GetItemCount<usize>,);
136136

137137
#[tokio::main]
138138
async fn main() -> Result<(), anyhow::Error> {

hyperactor/src/actor.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ impl<A: Actor> Clone for ActorHandle<A> {
684684
/// remote references across process boundaries.
685685
///
686686
/// It is not limited to concrete [`Actor`] implementations. For
687-
/// example, façade types generated by [`alias!`] implement
687+
/// example, façade types generated by [`behavior!`] implement
688688
/// `Referable` so that you can hand out restricted or stable APIs
689689
/// while still using the same remote messaging machinery.
690690
///
@@ -695,7 +695,7 @@ impl<A: Actor> Clone for ActorHandle<A> {
695695
/// boundaries.
696696
///
697697
/// In contrast, [`RemotableActor`] is the trait that marks *actors*
698-
/// that can actually be **spawned remotely**. An alias type may be a
698+
/// that can actually be **spawned remotely**. A behavior may be a
699699
/// `Referable` but is never a `RemotableActor`.
700700
pub trait Referable: Named + Send + Sync {}
701701

@@ -1028,15 +1028,15 @@ mod tests {
10281028
}
10291029

10301030
#[tokio::test]
1031-
async fn test_ref_alias() {
1031+
async fn test_ref_behavior() {
10321032
let test = MultiValuesTest::new().await;
10331033

10341034
test.send(123u64);
10351035
test.send("foo".to_string());
10361036

1037-
hyperactor::alias!(MyActorAlias, u64, String);
1037+
hyperactor::behavior!(MyActorBehavior, u64, String);
10381038

1039-
let myref: ActorRef<MyActorAlias> = test.handle.bind();
1039+
let myref: ActorRef<MyActorBehavior> = test.handle.bind();
10401040
myref.port().send(&test.client, "biz".to_string()).unwrap();
10411041
myref.port().send(&test.client, 999u64).unwrap();
10421042

hyperactor/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub use hyperactor_macros::RefClient;
127127
#[doc(inline)]
128128
pub use hyperactor_macros::Unbind;
129129
#[doc(inline)]
130-
pub use hyperactor_macros::alias;
130+
pub use hyperactor_macros::behavior;
131131
#[doc(inline)]
132132
pub use hyperactor_macros::export;
133133
#[doc(inline)]

hyperactor_macros/src/lib.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,49 +1603,52 @@ pub fn export(attr: TokenStream, item: TokenStream) -> TokenStream {
16031603
TokenStream::from(expanded)
16041604
}
16051605

1606-
/// Represents the full input to [`fn alias`].
1607-
struct AliasInput {
1608-
alias: Ident,
1606+
/// Represents the full input to [`fn behavior`].
1607+
struct BehaviorInput {
1608+
behavior: Ident,
16091609
handlers: Vec<HandlerSpec>,
16101610
}
16111611

1612-
impl syn::parse::Parse for AliasInput {
1612+
impl syn::parse::Parse for BehaviorInput {
16131613
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
1614-
let alias: Ident = input.parse()?;
1614+
let behavior: Ident = input.parse()?;
16151615
let _: Token![,] = input.parse()?;
16161616
let raw_handlers = input.parse_terminated(HandlerSpec::parse, Token![,])?;
16171617
let handlers = raw_handlers.into_iter().collect();
1618-
Ok(AliasInput { alias, handlers })
1618+
Ok(BehaviorInput { behavior, handlers })
16191619
}
16201620
}
16211621

1622-
/// Create a [`Referable`] handling a specific set of message types.
1623-
/// This is used to create an [`ActorRef`] without having to depend on the
1622+
/// Create a [`Referable`] definition, handling a specific set of message types.
1623+
/// Behaviors are used to create an [`ActorRef`] without having to depend on the
16241624
/// actor's implementation. If the message type need to be cast, add `castable`
1625-
/// flag to those types. e.g. the following example creats an alias with 5
1625+
/// flag to those types. e.g. the following example creates a behavior with 5
16261626
/// message types, and 4 of which need to be cast.
16271627
///
16281628
/// ```
1629-
/// hyperactor::alias!(
1630-
/// TestActorAlias,
1629+
/// hyperactor::behavior!(
1630+
/// TestActorBehavior,
16311631
/// TestMessage { castable = true },
16321632
/// () {castable = true },
16331633
/// MyGeneric<()> {castable = true },
16341634
/// u64,
16351635
/// );
16361636
/// ```
16371637
#[proc_macro]
1638-
pub fn alias(input: TokenStream) -> TokenStream {
1639-
let AliasInput { alias, handlers } = parse_macro_input!(input as AliasInput);
1638+
pub fn behavior(input: TokenStream) -> TokenStream {
1639+
let BehaviorInput {
1640+
behavior: behavior,
1641+
handlers,
1642+
} = parse_macro_input!(input as BehaviorInput);
16401643
let tys = HandlerSpec::add_indexed(handlers);
16411644

16421645
let expanded = quote! {
1643-
#[doc = "The generated alias struct."]
1646+
#[doc = "The generated behavior struct."]
16441647
#[derive(Debug, hyperactor::Named, serde::Serialize, serde::Deserialize)]
1645-
pub struct #alias;
1646-
impl hyperactor::actor::Referable for #alias {}
1648+
pub struct #behavior;
1649+
impl hyperactor::actor::Referable for #behavior {}
16471650

1648-
impl<A> hyperactor::actor::Binds<A> for #alias
1651+
impl<A> hyperactor::actor::Binds<A> for #behavior
16491652
where
16501653
A: hyperactor::Actor #(+ hyperactor::Handler<#tys>)* {
16511654
fn bind(ports: &hyperactor::proc::Ports<A>) {
@@ -1656,7 +1659,7 @@ pub fn alias(input: TokenStream) -> TokenStream {
16561659
}
16571660

16581661
#(
1659-
impl hyperactor::actor::RemoteHandles<#tys> for #alias {}
1662+
impl hyperactor::actor::RemoteHandles<#tys> for #behavior {}
16601663
)*
16611664
};
16621665

hyperactor_macros/tests/export.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl Handler<u64> for TestActor {
8585
}
8686
}
8787

88-
hyperactor::alias!(
88+
hyperactor::behavior!(
8989
TestActorAlias,
9090
TestMessage { cast = true },
9191
() { cast = true },

hyperactor_mesh/src/bootstrap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ async fn exit_if_missed_heartbeat(bootstrap_index: usize, bootstrap_addr: Channe
179179
}
180180

181181
#[macro_export]
182-
macro_rules! ok {
182+
macro_rules! ok {re
183183
($expr:expr $(,)?) => {
184184
match $expr {
185185
Ok(value) => value,

hyperactor_multiprocess/src/supervision.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,6 @@ impl ProcSupervisionState {
133133
}
134134
}
135135

136-
hyperactor::alias!(ProcSupervisor, ProcSupervisionMessage); // For proc supervisor to implement (e.g. system actor)
137-
hyperactor::alias!(WorldSupervisor, WorldSupervisionMessage); // For world supervisor to implement (e.g. system actor)
138-
hyperactor::alias!(SupervisionClient, WorldSupervisionState); // For the end receiver of supervision events to implement (e.g. client)
136+
hyperactor::behavior!(ProcSupervisor, ProcSupervisionMessage); // For proc supervisor to implement (e.g. system actor)
137+
hyperactor::behavior!(WorldSupervisor, WorldSupervisionMessage); // For world supervisor to implement (e.g. system actor)
138+
hyperactor::behavior!(SupervisionClient, WorldSupervisionState); // For the end receiver of supervision events to implement (e.g. client)

monarch_messages/src/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,4 @@ pub enum ClientMessage {
9595
},
9696
}
9797

98-
hyperactor::alias!(ClientActor, ClientMessage);
98+
hyperactor::behavior!(ClientActor, ClientMessage);

0 commit comments

Comments
 (0)