Skip to content

Commit f114bce

Browse files
committed
simplify
1 parent 6d23483 commit f114bce

8 files changed

Lines changed: 343 additions & 216 deletions

File tree

actor/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ At minimum, you provide:
7575
- `type Args`: startup payload (`()` when unused)
7676
- `type Snapshot`: cheap clone used for concurrent read-only handlers
7777
- `snapshot(...)`
78-
- `on_readonly(...)`
78+
- `on_read_only(...)`
7979
- `on_read_write(...)`
8080

8181
### Minimal actor example
@@ -108,7 +108,7 @@ impl<E: Spawner> Actor<E> for Counter {
108108
self.total
109109
}
110110
111-
async fn on_readonly(
111+
async fn on_read_only(
112112
_context: E,
113113
snapshot: Self::Snapshot,
114114
message: CounterMailboxReadOnlyMessage,
@@ -153,7 +153,7 @@ runner.start(|context| async move {
153153
- read-only ingress runs concurrently on snapshots
154154
- read-write ingress runs serially on actor state
155155
- read-write handling is fenced behind read-only work dispatched before the write arrived
156-
- returning `Err` from `on_readonly` or `on_read_write` is fatal but `on_shutdown` is still called after draining in-flight reads
156+
- returning `Err` from `on_read_only` or `on_read_write` is fatal but `on_shutdown` is still called after draining in-flight reads
157157

158158
`subscribe` detail:
159159

@@ -200,7 +200,7 @@ impl<E: Spawner + Clock> Actor<E> for ExternalActor {
200200
self.value
201201
}
202202

203-
async fn on_readonly(
203+
async fn on_read_only(
204204
_context: E,
205205
snapshot: Self::Snapshot,
206206
message: ExternalMailboxReadOnlyMessage,

actor/macros/src/ingress/mod.rs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -678,12 +678,14 @@ pub(crate) fn expand(input: TokenStream) -> TokenStream {
678678
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
679679
let names = GenericParamNames::from_generics(&generics);
680680

681-
let readonly_variants = items
681+
let readonly_variants: Vec<_> = items
682682
.iter()
683-
.filter_map(|item| emit_readonly_variant(item, &actor));
684-
let read_write_variants = items
683+
.filter_map(|item| emit_readonly_variant(item, &actor))
684+
.collect();
685+
let read_write_variants: Vec<_> = items
685686
.iter()
686-
.filter_map(|item| emit_read_write_variant(item, &actor));
687+
.filter_map(|item| emit_read_write_variant(item, &actor))
688+
.collect();
687689
let wrappers = items.iter().map(|item| {
688690
let ctx = WrapperEmitCtx {
689691
actor: &actor,
@@ -707,13 +709,51 @@ pub(crate) fn expand(input: TokenStream) -> TokenStream {
707709
MailboxKind::Unbounded => quote!(#actor::mailbox::UnboundedMailbox<#ingress #ty_generics>),
708710
};
709711

712+
let phantom_variant = if generics.params.is_empty() {
713+
quote!()
714+
} else {
715+
let phantom_args: Vec<_> = generics
716+
.params
717+
.iter()
718+
.map(|p| match p {
719+
GenericParam::Type(tp) => {
720+
let ident = &tp.ident;
721+
quote!(#ident)
722+
}
723+
GenericParam::Lifetime(lp) => {
724+
let lt = &lp.lifetime;
725+
quote!(&#lt ())
726+
}
727+
GenericParam::Const(_) => quote!(()),
728+
})
729+
.collect();
730+
quote! {
731+
#[doc(hidden)]
732+
_Phantom(::core::marker::PhantomData<(#(#phantom_args),*)>),
733+
}
734+
};
735+
736+
let empty = quote!();
737+
let readonly_phantom = if readonly_variants.is_empty() {
738+
&phantom_variant
739+
} else {
740+
&empty
741+
};
742+
let read_write_phantom = if read_write_variants.is_empty() {
743+
&phantom_variant
744+
} else {
745+
&empty
746+
};
747+
710748
quote! {
711749
pub enum #readonly_ingress #generics #where_clause {
712750
#(#readonly_variants)*
751+
#readonly_phantom
713752
}
714753

715754
pub enum #read_write_ingress #generics #where_clause {
716755
#(#read_write_variants)*
756+
#read_write_phantom
717757
}
718758

719759
pub enum #ingress #generics #where_clause {

actor/src/lib.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ stability_scope!(ALPHA {
3030
/// 1. `on_startup` once (receives [`Actor::Args`] data).
3131
/// 2. Per iteration: `preprocess`, then race lanes against
3232
/// `on_external` for one event. If a message is received (`Some`),
33-
/// dispatch to `on_readonly` (concurrent) or `on_read_write` (serial),
33+
/// dispatch to `on_read_only` (concurrent) or `on_read_write` (serial),
3434
/// then `postprocess`. If the source
3535
/// yields `None` (lane closed or `on_external` exhaustion), skip
3636
/// directly to `on_shutdown`.
3737
/// 3. `on_shutdown` once, on graceful exit (runtime stop, lane closure,
3838
/// or `on_external` returning `None`).
3939
///
40-
/// Returning `Err` from `on_readonly` or `on_read_write` is fatal: the error is logged,
40+
/// Returning `Err` from `on_read_only` or `on_read_write` is fatal: the error is logged,
4141
/// remaining in-flight reads are drained, and then `on_shutdown` is called
4242
/// before the loop exits.
4343
pub trait Actor<E>: Send + 'static {
@@ -54,7 +54,7 @@ stability_scope!(ALPHA {
5454
/// `<MailboxName>Message`.
5555
type Ingress: IntoIngressEnvelope;
5656

57-
/// Fatal error type returned by [`Actor::on_readonly`] and [`Actor::on_read_write`].
57+
/// Fatal error type returned by [`Actor::on_read_only`] and [`Actor::on_read_write`].
5858
///
5959
/// Returning `Err` from read/write handlers logs the error and stops the
6060
/// actor.
@@ -89,7 +89,7 @@ stability_scope!(ALPHA {
8989
///
9090
/// Called on: runtime shutdown signal, lane closure,
9191
/// [`Actor::on_external`] returning `None`, or after a fatal handler
92-
/// error from [`Actor::on_readonly`] or [`Actor::on_read_write`].
92+
/// error from [`Actor::on_read_only`] or [`Actor::on_read_write`].
9393
fn on_shutdown(
9494
&mut self,
9595
_context: &mut E,
@@ -114,7 +114,7 @@ stability_scope!(ALPHA {
114114
/// Create a snapshot for handling read-only ingress concurrently.
115115
///
116116
/// The service loop captures this snapshot when a read-only message is
117-
/// admitted, then executes [`Actor::on_readonly`] in a spawned task.
117+
/// admitted, then executes [`Actor::on_read_only`] in a spawned task.
118118
///
119119
/// This must be cheap to create. Prefer `Arc`-backed structures or
120120
/// `Copy` types. Avoid deep cloning large data structures, as
@@ -131,11 +131,13 @@ stability_scope!(ALPHA {
131131
/// Returning `Err` is fatal: the service loop logs the error, drains
132132
/// remaining in-flight reads, and then calls [`Actor::on_shutdown`]
133133
/// before exiting.
134-
fn on_readonly(
135-
context: E,
136-
snapshot: Self::Snapshot,
137-
message: <Self::Ingress as IntoIngressEnvelope>::ReadOnlyIngress,
138-
) -> impl Future<Output = Result<(), Self::Error>> + Send;
134+
fn on_read_only(
135+
_context: E,
136+
_snapshot: Self::Snapshot,
137+
_message: <Self::Ingress as IntoIngressEnvelope>::ReadOnlyIngress,
138+
) -> impl Future<Output = Result<(), Self::Error>> + Send {
139+
futures::future::pending()
140+
}
139141

140142
/// Handle one ingress message that may mutate actor state.
141143
///
@@ -148,10 +150,12 @@ stability_scope!(ALPHA {
148150
/// before exiting.
149151
fn on_read_write(
150152
&mut self,
151-
context: &mut E,
152-
args: &mut Self::Args,
153-
message: <Self::Ingress as IntoIngressEnvelope>::ReadWriteIngress,
154-
) -> impl Future<Output = Result<(), Self::Error>> + Send;
153+
_context: &mut E,
154+
_args: &mut Self::Args,
155+
_message: <Self::Ingress as IntoIngressEnvelope>::ReadWriteIngress,
156+
) -> impl Future<Output = Result<(), Self::Error>> + Send {
157+
futures::future::pending()
158+
}
155159

156160
/// Poll external per-iteration sources and map them to ingress.
157161
///

actor/src/service/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ where
6363
/// # Behavioral Semantics
6464
///
6565
/// - At most one event is dispatched per iteration.
66-
/// - Returning `Err` from [`Actor::on_readonly`] or [`Actor::on_read_write`]
66+
/// - Returning `Err` from [`Actor::on_read_only`] or [`Actor::on_read_write`]
6767
/// is fatal: the error is logged, remaining in-flight reads are drained,
6868
/// and then [`Actor::on_shutdown`] is called before the loop exits.
6969
/// - A lane closing (`None`) or [`Actor::on_external`] returning `None`

0 commit comments

Comments
 (0)