Skip to content

Commit 1d14ac2

Browse files
committed
Mention + test that #[rpc(config)] can be used with functions
1 parent dd21dfd commit 1d14ac2

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

godot-macros/src/lib.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -693,8 +693,8 @@ pub fn derive_godot_class(input: TokenStream) -> TokenStream {
693693
/// | Transfer mode | [`TransferMode`] | **`unreliable`**, `unreliable_ordered`, `reliable` |
694694
/// | Channel | `u32` | any |
695695
///
696-
/// You can also use `#[rpc(config = value)]`, with `value` being a constant of type [`RpcConfig`] in scope. This can be useful to reuse
697-
/// configurations across multiple RPCs.
696+
/// You can also use `#[rpc(config = value)]`, with `value` being an expression of type [`RpcConfig`] in scope, for example a `const` or the
697+
/// call to a function. This can be useful to reuse configurations across multiple RPCs.
698698
///
699699
/// `#[rpc]` implies `#[func]`. You can use both attributes together, if you need to configure other `#[func]`-specific keys.
700700
///
@@ -719,14 +719,26 @@ pub fn derive_godot_class(input: TokenStream) -> TokenStream {
719719
/// fn explicit(&mut self) {}
720720
///
721721
/// #[rpc(config = MY_RPC_CONFIG)]
722-
/// fn external_config(&mut self) {}
722+
/// fn external_config_const(&mut self) {}
723+
///
724+
/// #[rpc(config = my_rpc_provider())]
725+
/// fn external_config_fn(&mut self) {}
723726
/// }
724727
///
725728
/// const MY_RPC_CONFIG: RpcConfig = RpcConfig {
729+
/// rpc_mode: RpcMode::AUTHORITY,
726730
/// transfer_mode: TransferMode::UNRELIABLE_ORDERED,
731+
/// call_local: false,
727732
/// channel: 2,
728-
/// ..Default::default()
729733
/// };
734+
///
735+
/// fn my_rpc_provider() -> RpcConfig {
736+
/// RpcConfig {
737+
/// transfer_mode: TransferMode::UNRELIABLE_ORDERED,
738+
/// channel: 2,
739+
/// ..Default::default() // only possible in fn, not in const.
740+
/// }
741+
/// }
730742
/// ```
731743
///
732744
// Note: for some reason, the intra-doc links don't work here, despite dev-dependency on godot.

itest/rust/src/register_tests/rpc_test.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ const CACHED_CFG: RpcConfig = RpcConfig {
2525
channel: 1,
2626
};
2727

28+
fn provide_cfg() -> RpcConfig {
29+
RpcConfig {
30+
transfer_mode: TransferMode::RELIABLE,
31+
channel: 1,
32+
..Default::default()
33+
}
34+
}
35+
2836
#[godot_api]
2937
impl RpcTest {
3038
#[rpc]
@@ -66,7 +74,10 @@ impl RpcTest {
6674
pub fn args_func_gd_self(_this: Gd<Self>) {}
6775

6876
#[rpc(config = CACHED_CFG)]
69-
pub fn arg_config(&mut self) {}
77+
pub fn arg_config_const(&mut self) {}
78+
79+
#[rpc(config = provide_cfg())]
80+
pub fn arg_config_fn(&mut self) {}
7081
}
7182

7283
// ----------------------------------------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)