2
0
Fork 1
mirror of https://github.com/async-rs/async-std.git synced 2025-04-03 06:56:41 +00:00
async-std/src/task/builder.rs
2019-10-01 18:08:24 +09:00

32 lines
755 B
Rust

use super::pool;
use super::JoinHandle;
use crate::future::Future;
use crate::io;
/// Task builder that configures the settings of a new task.
#[derive(Debug, Default)]
pub struct Builder {
pub(crate) name: Option<String>,
}
impl Builder {
/// Creates a new builder.
pub fn new() -> Builder {
Builder { name: None }
}
/// Configures the name of the task.
pub fn name(mut self, name: String) -> Builder {
self.name = Some(name);
self
}
/// Spawns a task with the configured settings.
pub fn spawn<F, T>(self, future: F) -> io::Result<JoinHandle<T>>
where
F: Future<Output = T> + Send + 'static,
T: Send + 'static,
{
Ok(pool::get().spawn(future, self))
}
}