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§
Required Associated Types§
Sourcetype Client: Client<Self::Error>
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.
Sourcetype ServerRequest: Req<Self::Error> + Send
type ServerRequest: Req<Self::Error> + Send
The type of the HTTP request when received by the server function on the server side.
Sourcetype ServerResponse: Res<Self::Error> + Send
type ServerResponse: Res<Self::Error> + Send
The type of the HTTP response returned by the server function on the server side.
Sourcetype Output: IntoRes<Self::OutputEncoding, Self::ServerResponse, Self::Error> + FromRes<Self::OutputEncoding, <Self::Client as Client<Self::Error>>::Response, 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
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.
Sourcetype InputEncoding: Encoding
type InputEncoding: Encoding
The Encoding
used in the request for arguments into the server function.
Sourcetype OutputEncoding: Encoding
type OutputEncoding: Encoding
The Encoding
used in the response for the result of the server function.
Sourcetype Error: FromStr + Display
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§
Provided Methods§
Sourcefn url() -> &'static str
fn url() -> &'static str
Returns Self::PATH
.
Sourcefn middlewares() -> Vec<Arc<dyn Layer<Self::ServerRequest, Self::ServerResponse>>>
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.