ServerFn

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 Document

Sourceยง

const PATH: &'static str

Sourceยง

type Client = BrowserClient

Sourceยง

type Server = AxumServerFnBackend

Sourceยง

type Protocol = Http<GetUrl, Post<JsonEncoding>>

Sourceยง

type Output = <Result<(DocumentUri, Box<[Css]>, Box<str>), BackendError<ServerFnErrorErr>> as ServerFnMustReturnResult>::Ok

Sourceยง

type Error = <Result<(DocumentUri, Box<[Css]>, Box<str>), BackendError<ServerFnErrorErr>> as ServerFnMustReturnResult>::Err

Sourceยง

type InputStreamError = <Result<(DocumentUri, Box<[Css]>, Box<str>), BackendError<ServerFnErrorErr>> as ServerFnMustReturnResult>::Err

Sourceยง

type OutputStreamError = <Result<(DocumentUri, Box<[Css]>, Box<str>), BackendError<ServerFnErrorErr>> as ServerFnMustReturnResult>::Err

Sourceยง

fn middlewares() -> Vec<Arc<dyn Layer<<<Document as ServerFn>::Server as Server<<Document as ServerFn>::Error>>::Request, <<Document as ServerFn>::Server as Server<<Document as ServerFn>::Error>>::Response>>>

Sourceยง

fn run_body( self, ) -> impl Future<Output = Result<(DocumentUri, Box<[Css]>, Box<str>), BackendError<ServerFnErrorErr>>> + Send

Sourceยง

impl ServerFn for DocumentOf

Sourceยง

impl ServerFn for Fragment

Sourceยง

const PATH: &'static str

Sourceยง

type Client = BrowserClient

Sourceยง

type Server = AxumServerFnBackend

Sourceยง

type Protocol = Http<GetUrl, Post<JsonEncoding>>

Sourceยง

type Output = <Result<(Uri, Box<[Css]>, Box<str>), BackendError<ServerFnErrorErr>> as ServerFnMustReturnResult>::Ok

Sourceยง

type Error = <Result<(Uri, Box<[Css]>, Box<str>), BackendError<ServerFnErrorErr>> as ServerFnMustReturnResult>::Err

Sourceยง

type InputStreamError = <Result<(Uri, Box<[Css]>, Box<str>), BackendError<ServerFnErrorErr>> as ServerFnMustReturnResult>::Err

Sourceยง

type OutputStreamError = <Result<(Uri, Box<[Css]>, Box<str>), BackendError<ServerFnErrorErr>> as ServerFnMustReturnResult>::Err

Sourceยง

fn middlewares() -> Vec<Arc<dyn Layer<<<Fragment as ServerFn>::Server as Server<<Fragment as ServerFn>::Error>>::Request, <<Fragment as ServerFn>::Server as Server<<Fragment as ServerFn>::Error>>::Response>>>

Sourceยง

fn run_body( self, ) -> impl Future<Output = Result<(Uri, Box<[Css]>, Box<str>), BackendError<ServerFnErrorErr>>> + Send

Sourceยง

impl ServerFn for GetDocument

Sourceยง

const PATH: &'static str

Sourceยง

type Client = BrowserClient

Sourceยง

type Server = AxumServerFnBackend

Sourceยง

type Protocol = Http<GetUrl, Post<JsonEncoding>>

Sourceยง

type Output = <Result<Document, BackendError<ServerFnErrorErr>> as ServerFnMustReturnResult>::Ok

Sourceยง

type Error = <Result<Document, BackendError<ServerFnErrorErr>> as ServerFnMustReturnResult>::Err

Sourceยง

type InputStreamError = <Result<Document, BackendError<ServerFnErrorErr>> as ServerFnMustReturnResult>::Err

Sourceยง

type OutputStreamError = <Result<Document, BackendError<ServerFnErrorErr>> as ServerFnMustReturnResult>::Err

Sourceยง

fn middlewares() -> Vec<Arc<dyn Layer<<<GetDocument as ServerFn>::Server as Server<<GetDocument as ServerFn>::Error>>::Request, <<GetDocument as ServerFn>::Server as Server<<GetDocument as ServerFn>::Error>>::Response>>>

Sourceยง

fn run_body( self, ) -> impl Future<Output = Result<Document, BackendError<ServerFnErrorErr>>> + Send

Sourceยง

impl ServerFn for GetModule

Sourceยง

impl ServerFn for GetQuiz

Sourceยง

const PATH: &'static str

Sourceยง

type Client = BrowserClient

Sourceยง

type Server = AxumServerFnBackend

Sourceยง

type Protocol = Http<GetUrl, Post<JsonEncoding>>

Sourceยง

type Output = <Result<Quiz, ServerFnError<String>> as ServerFnMustReturnResult>::Ok

Sourceยง

type Error = <Result<Quiz, ServerFnError<String>> as ServerFnMustReturnResult>::Err

Sourceยง

type InputStreamError = <Result<Quiz, ServerFnError<String>> as ServerFnMustReturnResult>::Err

Sourceยง

type OutputStreamError = <Result<Quiz, ServerFnError<String>> as ServerFnMustReturnResult>::Err

Sourceยง

fn middlewares() -> Vec<Arc<dyn Layer<<<GetQuiz as ServerFn>::Server as Server<<GetQuiz as ServerFn>::Error>>::Request, <<GetQuiz as ServerFn>::Server as Server<<GetQuiz as ServerFn>::Error>>::Response>>>

Sourceยง

fn run_body( self, ) -> impl Future<Output = Result<Quiz, ServerFnError<String>>> + Send

Sourceยง

impl ServerFn for Grade

Sourceยง

impl ServerFn for GradeEnc

Sourceยง

impl ServerFn for Los

Sourceยง

const PATH: &'static str

Sourceยง

type Client = BrowserClient

Sourceยง

type Server = AxumServerFnBackend

Sourceยง

type Protocol = Http<GetUrl, Post<JsonEncoding>>

Sourceยง

type Output = <Result<Vec<(DocumentElementUri, ParagraphOrProblemKind)>, BackendError<ServerFnErrorErr>> as ServerFnMustReturnResult>::Ok

Sourceยง

type Error = <Result<Vec<(DocumentElementUri, ParagraphOrProblemKind)>, BackendError<ServerFnErrorErr>> as ServerFnMustReturnResult>::Err

Sourceยง

type InputStreamError = <Result<Vec<(DocumentElementUri, ParagraphOrProblemKind)>, BackendError<ServerFnErrorErr>> as ServerFnMustReturnResult>::Err

Sourceยง

type OutputStreamError = <Result<Vec<(DocumentElementUri, ParagraphOrProblemKind)>, BackendError<ServerFnErrorErr>> as ServerFnMustReturnResult>::Err

Sourceยง

fn middlewares() -> Vec<Arc<dyn Layer<<<Los as ServerFn>::Server as Server<<Los as ServerFn>::Error>>::Request, <<Los as ServerFn>::Server as Server<<Los as ServerFn>::Error>>::Response>>>

Sourceยง

fn run_body( self, ) -> impl Future<Output = Result<Vec<(DocumentElementUri, ParagraphOrProblemKind)>, BackendError<ServerFnErrorErr>>> + Send

Sourceยง

impl ServerFn for Notations

Sourceยง

const PATH: &'static str

Sourceยง

type Client = BrowserClient

Sourceยง

type Server = AxumServerFnBackend

Sourceยง

type Protocol = Http<GetUrl, Post<JsonEncoding>>

Sourceยง

type Output = <Result<Vec<(DocumentElementUri, Notation)>, BackendError<ServerFnErrorErr>> as ServerFnMustReturnResult>::Ok

Sourceยง

type Error = <Result<Vec<(DocumentElementUri, Notation)>, BackendError<ServerFnErrorErr>> as ServerFnMustReturnResult>::Err

Sourceยง

type InputStreamError = <Result<Vec<(DocumentElementUri, Notation)>, BackendError<ServerFnErrorErr>> as ServerFnMustReturnResult>::Err

Sourceยง

type OutputStreamError = <Result<Vec<(DocumentElementUri, Notation)>, BackendError<ServerFnErrorErr>> as ServerFnMustReturnResult>::Err

Sourceยง

fn middlewares() -> Vec<Arc<dyn Layer<<<Notations as ServerFn>::Server as Server<<Notations as ServerFn>::Error>>::Request, <<Notations as ServerFn>::Server as Server<<Notations as ServerFn>::Error>>::Response>>>

Sourceยง

fn run_body( self, ) -> impl Future<Output = Result<Vec<(DocumentElementUri, Notation)>, BackendError<ServerFnErrorErr>>> + Send

Sourceยง

impl ServerFn for SlidesView

Sourceยง

const PATH: &'static str

Sourceยง

type Client = BrowserClient

Sourceยง

type Server = AxumServerFnBackend

Sourceยง

type Protocol = Http<GetUrl, Post<JsonEncoding>>

Sourceยง

type Output = <Result<(Box<[Css]>, Box<[SlideElement]>), ServerFnError<String>> as ServerFnMustReturnResult>::Ok

Sourceยง

type Error = <Result<(Box<[Css]>, Box<[SlideElement]>), ServerFnError<String>> as ServerFnMustReturnResult>::Err

Sourceยง

type InputStreamError = <Result<(Box<[Css]>, Box<[SlideElement]>), ServerFnError<String>> as ServerFnMustReturnResult>::Err

Sourceยง

type OutputStreamError = <Result<(Box<[Css]>, Box<[SlideElement]>), ServerFnError<String>> as ServerFnMustReturnResult>::Err

Sourceยง

fn middlewares() -> Vec<Arc<dyn Layer<<<SlidesView as ServerFn>::Server as Server<<SlidesView as ServerFn>::Error>>::Request, <<SlidesView as ServerFn>::Server as Server<<SlidesView as ServerFn>::Error>>::Response>>>

Sourceยง

fn run_body( self, ) -> impl Future<Output = Result<(Box<[Css]>, Box<[SlideElement]>), ServerFnError<String>>> + Send

Sourceยง

impl ServerFn for Solution

Sourceยง

impl ServerFn for Title

Sourceยง

const PATH: &'static str

Sourceยง

type Client = BrowserClient

Sourceยง

type Server = AxumServerFnBackend

Sourceยง

type Protocol = Http<GetUrl, Post<JsonEncoding>>

Sourceยง

type Output = <Result<(Box<[Css]>, Box<str>), ServerFnError<String>> as ServerFnMustReturnResult>::Ok

Sourceยง

type Error = <Result<(Box<[Css]>, Box<str>), ServerFnError<String>> as ServerFnMustReturnResult>::Err

Sourceยง

type InputStreamError = <Result<(Box<[Css]>, Box<str>), ServerFnError<String>> as ServerFnMustReturnResult>::Err

Sourceยง

type OutputStreamError = <Result<(Box<[Css]>, Box<str>), ServerFnError<String>> as ServerFnMustReturnResult>::Err

Sourceยง

fn middlewares() -> Vec<Arc<dyn Layer<<<Title as ServerFn>::Server as Server<<Title as ServerFn>::Error>>::Request, <<Title as ServerFn>::Server as Server<<Title as ServerFn>::Error>>::Response>>>

Sourceยง

fn run_body( self, ) -> impl Future<Output = Result<(Box<[Css]>, Box<str>), ServerFnError<String>>> + Send

Sourceยง

impl ServerFn for Toc

Sourceยง

const PATH: &'static str

Sourceยง

type Client = BrowserClient

Sourceยง

type Server = AxumServerFnBackend

Sourceยง

type Protocol = Http<GetUrl, Post<JsonEncoding>>

Sourceยง

type Output = <Result<(Box<[Css]>, Box<[TocElem]>), BackendError<ServerFnErrorErr>> as ServerFnMustReturnResult>::Ok

Sourceยง

type Error = <Result<(Box<[Css]>, Box<[TocElem]>), BackendError<ServerFnErrorErr>> as ServerFnMustReturnResult>::Err

Sourceยง

type InputStreamError = <Result<(Box<[Css]>, Box<[TocElem]>), BackendError<ServerFnErrorErr>> as ServerFnMustReturnResult>::Err

Sourceยง

type OutputStreamError = <Result<(Box<[Css]>, Box<[TocElem]>), BackendError<ServerFnErrorErr>> as ServerFnMustReturnResult>::Err

Sourceยง

fn middlewares() -> Vec<Arc<dyn Layer<<<Toc as ServerFn>::Server as Server<<Toc as ServerFn>::Error>>::Request, <<Toc as ServerFn>::Server as Server<<Toc as ServerFn>::Error>>::Response>>>

Sourceยง

fn run_body( self, ) -> impl Future<Output = Result<(Box<[Css]>, Box<[TocElem]>), BackendError<ServerFnErrorErr>>> + Send

Sourceยง

impl ServerFn for Uris

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ยง