Attribute Macro remoc::rtc::remote

source ·
#[remote]
Available on crate feature rtc only.
Expand description

Denotes a trait as remotely callable and generate a client and servers for it.

See module-level documentation for details and examples.

This generates the client and server structs for the trait. If the trait is called Trait the client will be called TraitClient and the name of the servers will start with TraitServer.

§Requirements

Each trait method must be async and have return type Result<T, E> where T and E are remote sendable and E must implemented From<CallError>. All arguments must also be remote sendable. Of course, you can use all remote types from Remoc in your arguments and return type, for example remote channels and remote objects.

This uses async_trait, so you must apply the async_trait attribute on all implementation of the trait.

Since the generated code relies on Tokio macros, you must add a dependency to Tokio in your Cargo.toml.

§Generics and lifetimes

The trait may be generic with constraints on the generic arguments. You will probably need to constrain them on RemoteSend. Method definitions within the remote trait may use generic arguments from the trait definition, but must not introduce generic arguments in the method definition.

Lifetimes are not allowed on remote traits and their methods.

§Default implementations of methods

Default implementations of methods may be provided. However, this requires specifying Send and Sync as supertraits of the remote trait.

§Attributes

If the clone argument is specified (by invoking the attribute as #[remoc::rtc::remote(clone)]), the generated TraitClient will even be clonable when the trait contains methods taking the receiver by mutable reference (&mut self). In this case the client can invoke more than one mutable method simultaneously; however, the execution on the server will be serialized through locking.

If the #[no_cancel] attribute is applied on a trait method, it will run to completion, even if the client cancels the request by dropping the future.

All serde field attributes #[serde(...)] are allowed on the arguments of the functions. They will be transferred to the respective field of the request struct that will be send to the server when the method is called by the client. This can be used to customize serialization and provide defaults for forward and backward compatibility.