pub trait _:
    Send
    + FromReq<Self::InputEncoding, Self::ServerRequest, Self::Error>
    + IntoReq<Self::InputEncoding, <Self::Client as Client<Self::Error>>::Request, Self::Error> {
    type Client: Client<Self::Error>;
    type ServerRequest: Req<Self::Error> + Send;
    type ServerResponse: Res<Self::Error> + Send;
    type Output: IntoRes<Self::OutputEncoding, Self::ServerResponse, Self::Error> + FromRes<Self::OutputEncoding, <Self::Client as Client<Self::Error>>::Response, Self::Error> + Send;
    type InputEncoding: Encoding;
    type OutputEncoding: Encoding;
    type Error: FromStr + Display;

    const PATH: &'static str;

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

    // Provided methods
    fn url() -> &'static str { ... }
    fn middlewares(    ) -> Vec<Arc<dyn Layer<Self::ServerRequest, Self::ServerResponse>>> { ... }
}
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§

Source

const PATH: &'static str

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

Required Associated Types§

Source

type Client: Client<Self::Error>

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.

Source

type ServerRequest: Req<Self::Error> + Send

The type of the HTTP request when received by the server function on the server side.

Source

type ServerResponse: Res<Self::Error> + Send

The type of the HTTP response returned by the server function on the server side.

Source

type Output: IntoRes<Self::OutputEncoding, Self::ServerResponse, Self::Error> + FromRes<Self::OutputEncoding, <Self::Client as Client<Self::Error>>::Response, Self::Error> + 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.

Source

type InputEncoding: Encoding

The Encoding used in the request for arguments into the server function.

Source

type OutputEncoding: Encoding

The Encoding used in the response for the result of the server function.

Source

type Error: FromStr + Display

The type of the custom error on ServerFnError, if any. (If there is no custom error type, this can be NoCustomError by default.)

Required Methods§

Source

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

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

Provided Methods§

Source

fn url() -> &'static str

Returns Self::PATH.

Source

fn middlewares() -> Vec<Arc<dyn Layer<Self::ServerRequest, Self::ServerResponse>>>

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.

Implementors§