Trait ServerFn

pub trait ServerFn: Sized + Send {
    type Client: Client<Self::Error, Self::InputStreamError, Self::OutputStreamError>;
    type Server: Server<Self::Error, Self::InputStreamError, Self::OutputStreamError>;
    type Protocol: Protocol<Self, Self::Output, Self::Client, Self::Server, Self::Error, Self::InputStreamError, Self::OutputStreamError>;
    type Output: Send;
    type Error: FromServerFnError + Send + Sync;
    type InputStreamError: FromServerFnError + Send + Sync;
    type OutputStreamError: FromServerFnError + Send + Sync;

    const PATH: &'static str;

    // Required method
    fn run_body(
        self,
    ) -> impl Future<Output = Result<Self::Output, Self::Error>> + Send;

    // Provided methods
    fn url() -> &'static str { ... }
    fn middlewares(    ) -> Vec<Arc<dyn Layer<<Self::Server as Server<Self::Error, Self::InputStreamError, Self::OutputStreamError>>::Request, <Self::Server as Server<Self::Error, Self::InputStreamError, Self::OutputStreamError>>::Response>>> { ... }
}
Expand description

Defines a function that runs only on the server, but can be called from the server or the client.

The type for which ServerFn is implemented is actually the type of the arguments to the function, while the function body itself is implemented in run_body.

This means that Self here is usually a struct, in which each field is an argument to the function. In other words,

ⓘ
#[server]
pub async fn my_function(foo: String, bar: usize) -> Result<usize, ServerFnError> {
    Ok(foo.len() + bar)
}

should expand to

ⓘ
#[derive(Serialize, Deserialize)]
pub struct MyFunction {
    foo: String,
    bar: usize
}

impl ServerFn for MyFunction {
    async fn run_body() -> Result<usize, ServerFnError> {
        Ok(foo.len() + bar)
    }

    // etc.
}

Required Associated Constants§

const PATH: &'static str

A unique path for the server function’s API endpoint, relative to the host, including its prefix.

Required Associated Types§

type Client: Client<Self::Error, Self::InputStreamError, Self::OutputStreamError>

The type of the HTTP client that will send the request from the client side.

For example, this might be gloo-net in the browser, or reqwest for a desktop app.

type Server: Server<Self::Error, Self::InputStreamError, Self::OutputStreamError>

The type of the HTTP server that will send the response from the server side.

For example, this might be axum or actix-web.

type Protocol: Protocol<Self, Self::Output, Self::Client, Self::Server, Self::Error, Self::InputStreamError, Self::OutputStreamError>

The protocol the server function uses to communicate with the client.

type Output: Send

The return type of the server function.

This needs to be converted into ServerResponse on the server side, and converted from ClientResponse when received by the client.

type Error: FromServerFnError + Send + Sync

The type of error in the server function return. Typically ServerFnError, but allowed to be any type that implements FromServerFnError.

type InputStreamError: FromServerFnError + Send + Sync

The type of error in the server function for stream items sent from the client to the server. Typically ServerFnError, but allowed to be any type that implements FromServerFnError.

type OutputStreamError: FromServerFnError + Send + Sync

The type of error in the server function for stream items sent from the server to the client. Typically ServerFnError, but allowed to be any type that implements FromServerFnError.

Required Methods§

fn run_body( self, ) -> impl Future<Output = Result<Self::Output, Self::Error>> + Send

The body of the server function. This will only run on the server.

Provided Methods§

fn url() -> &'static str

Returns Self::PATH.

fn middlewares() -> Vec<Arc<dyn Layer<<Self::Server as Server<Self::Error, Self::InputStreamError, Self::OutputStreamError>>::Request, <Self::Server as Server<Self::Error, Self::InputStreamError, Self::OutputStreamError>>::Response>>>

Middleware that should be applied to this server function.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl ServerFn for GetUsers

Source§

impl ServerFn for Login

Source§

impl ServerFn for LoginStateFn

Source§

impl ServerFn for Logout

Source§

impl ServerFn for SetAdmin

Implementors§