pub trait IntoJobResult {
// Required method
fn into_job_result(self) -> Option<JobResult>;
}
Expand description
Trait for generating JobResults.
Types that implement IntoJobResult
can be returned from Job
s.
§Optional return value
The into_job_result()
method returns Option<JobResult>
, as certain values can be considered
“void”, meaning that they don’t produce a result. See Void
.
§Results
A special case exists for Result
, allowing you to return any Error
from a Job
, so
long as it implements Send
and Sync
.
use blueprint_sdk::Router;
use blueprint_sdk::job::JobWithoutContextExt;
use blueprint_sdk::{IntoJobResult, JobResult};
#[derive(Debug)]
enum MyError {
SomethingWentWrong,
SomethingElseWentWrong,
}
impl core::fmt::Display for MyError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
MyError::SomethingWentWrong => write!(f, "something went wrong"),
MyError::SomethingElseWentWrong => write!(f, "something else went wrong"),
}
}
}
impl core::error::Error for MyError {}
const JOB_ID: u32 = 0;
// `Result<impl IntoJobResult, MyError>` can now be returned from jobs
let app = Router::new().route(JOB_ID, job);
async fn job() -> Result<(), MyError> {
Err(MyError::SomethingWentWrong)
}
§Implementing IntoJobResult
You generally shouldn’t have to implement IntoJobResult
manually, as blueprint_sdk
provides implementations for many common types.
However, it might be necessary if you have a custom body type you want to return from a Job
:
use blueprint_sdk::job::JobWithoutContextExt;
use blueprint_sdk::{IntoJobResult, JobResult, Router};
use bytes::Bytes;
use std::{
convert::Infallible,
pin::Pin,
task::{Context, Poll},
};
struct SendHello;
impl SendHello {
fn hello_bytes(self) -> Bytes {
Bytes::from("hello")
}
}
// Now we can implement `IntoJobResult` directly for `MyBody`
impl IntoJobResult for SendHello {
fn into_job_result(self) -> Option<JobResult> {
Some(JobResult::new(self.hello_bytes()))
}
}
const JOB_ID: u32 = 0;
// `MyBody` can now be returned from jobs.
let app = Router::new().route(JOB_ID, || async { SendHello });
Required Methods§
Sourcefn into_job_result(self) -> Option<JobResult>
fn into_job_result(self) -> Option<JobResult>
Create a JobResult.