vin_macros/lib.rs
1mod actor;
2mod task;
3mod message;
4
5use actor::actor_impl;
6use task::task_impl;
7use message::message_impl;
8
9use proc_macro::TokenStream;
10use quote::quote;
11
12/// Generates the actor impls and forms necessary fields.
13///
14/// # `vin::handles()` proc macro
15/// Specifies which message the actor handles, then generates code to handle the message.
16///
17/// ## Arguments
18/// - message type
19/// - maximum messages to handle concurrently (optional)
20///
21/// # Example
22/// ```ignore
23/// #[vin::actor]
24/// #[vin::handles(MyMsg)]
25/// struct MyActor;
26///
27/// /* or */
28///
29/// #[vin::actor]
30/// #[vin::handles(MyMsg, max = 1024)]
31/// struct MyActor;
32/// ```
33#[proc_macro_attribute]
34pub fn actor(args: TokenStream, input: TokenStream) -> TokenStream {
35 actor_impl(args, input)
36}
37
38/// A noop macro attribute used to specify messages to handle. Check [`actor`] for more information.
39#[proc_macro_attribute]
40pub fn handles(_args: TokenStream, _input: TokenStream) -> TokenStream {
41 quote! {}.into()
42}
43
44/// Generates a [`vin`]-managed [`tokio`] task. It's a specialized actor with no handler that runs some
45/// piece of code until it's completed or [`vin`] has shutdown.
46///
47/// # Example
48/// ```no_run
49/// # use vin::*;
50/// # struct WebSocket;
51///
52/// # impl WebSocket {
53/// # async fn recv(&mut self) -> Vec<u8> {
54/// # Vec::new()
55/// # }
56/// # }
57///
58/// #[vin::task]
59/// struct WebsocketSession {
60/// ws: WebSocket,
61/// }
62///
63/// #[async_trait]
64/// impl TaskActor for WebsocketSession {
65/// async fn task(&self, ctx: Self::Context) -> anyhow::Result<()> {
66/// loop {
67/// let msg = self.ws.recv().await?;
68/// /* do something */
69/// }
70/// }
71/// }
72/// ```
73#[proc_macro_attribute]
74pub fn task(args: TokenStream, input: TokenStream) -> TokenStream {
75 task_impl(args, input)
76}
77
78/// Implements the [`Message`] trait for a type. Enables specifying the result type easily.
79///
80/// Shorthand for:
81/// ```ignore
82/// struct MyMsg;
83///
84/// impl Message for MyMsg {
85/// type Result = u32;
86/// }
87/// ```
88///
89/// # Example
90/// ```ignore
91/// #[vin::message] // No result type (result = ())
92/// struct MyMsg;
93/// ```
94/// or
95/// ```ignore
96/// #[vin::message(result = u32)]
97/// struct MyMsg;
98/// ```
99/// or
100/// ```ignore
101/// #[vin::message(result = u32, error = MyError)]
102/// struct MyMsg;
103/// ```
104#[proc_macro_attribute]
105pub fn message(args: TokenStream, input: TokenStream) -> TokenStream {
106 message_impl(args, input)
107}