Skip to content

Commit

Permalink
Document + test #[rpc(config)] with functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Bromeon committed Nov 10, 2024
1 parent ffef18b commit 7b9ad4c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
20 changes: 16 additions & 4 deletions godot-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,8 +692,8 @@ pub fn derive_godot_class(input: TokenStream) -> TokenStream {
/// | Transfer mode | [`TransferMode`] | **`unreliable`**, `unreliable_ordered`, `reliable` |
/// | Channel | `u32` | any |
///
/// You can also use `#[rpc(config = value)]`, with `value` being a constant of type [`RpcConfig`] in scope. This can be useful to reuse
/// configurations across multiple RPCs.
/// You can also use `#[rpc(config = value)]`, with `value` being an expression of type [`RpcConfig`] in scope, for example a `const` or the
/// call to a function. This can be useful to reuse configurations across multiple RPCs.
///
/// `#[rpc]` implies `#[func]`. You can use both attributes together, if you need to configure other `#[func]`-specific keys.
///
Expand All @@ -716,14 +716,26 @@ pub fn derive_godot_class(input: TokenStream) -> TokenStream {
/// fn explicit(&mut self) {}
///
/// #[rpc(config = MY_RPC_CONFIG)]
/// fn external_config(&mut self) {}
/// fn external_config_const(&mut self) {}
///
/// #[rpc(config = my_rpc_provider())]
/// fn external_config_fn(&mut self) {}
/// }
///
/// const MY_RPC_CONFIG: RpcConfig = RpcConfig {
/// rpc_mode: RpcMode::AUTHORITY,
/// transfer_mode: TransferMode::UNRELIABLE_ORDERED,
/// call_local: false,
/// channel: 2,
/// ..Default::default()
/// };
///
/// fn my_rpc_provider() -> RpcConfig {
/// RpcConfig {
/// transfer_mode: TransferMode::UNRELIABLE_ORDERED,
/// channel: 2,
/// ..Default::default() // only possible in fn, not in const.
/// }
/// }
/// ```
///
// Note: for some reason, the intra-doc links don't work here, despite dev-dependency on godot.
Expand Down
13 changes: 12 additions & 1 deletion itest/rust/src/register_tests/rpc_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ const CACHED_CFG: RpcConfig = RpcConfig {
channel: 1,
};

fn provide_cfg() -> RpcConfig {
RpcConfig {
transfer_mode: TransferMode::RELIABLE,
channel: 1,
..Default::default()
}
}

#[godot_api]
impl RpcTest {
#[rpc]
Expand Down Expand Up @@ -66,7 +74,10 @@ impl RpcTest {
pub fn args_func_gd_self(_this: Gd<Self>) {}

#[rpc(config = CACHED_CFG)]
pub fn arg_config(&mut self) {}
pub fn arg_config_const(&mut self) {}

#[rpc(config = provide_cfg())]
pub fn arg_config_fn(&mut self) {}
}

// ----------------------------------------------------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit 7b9ad4c

Please sign in to comment.