aboutsummaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
authorhozan23 <hozan23@proton.me>2024-03-21 12:28:47 +0100
committerhozan23 <hozan23@proton.me>2024-03-21 12:28:47 +0100
commit5baf14594331b1b0b60d655240eb398bcce61f7c (patch)
tree1911a2af6e0684c35a143095ee31223f1f88abd5 /core/src
parent379dca552ca91d22ee007b42f93803ad3dc2b274 (diff)
core: minor modification to TaskGroup api
Diffstat (limited to 'core/src')
-rw-r--r--core/src/async_util/task_group.rs26
1 files changed, 16 insertions, 10 deletions
diff --git a/core/src/async_util/task_group.rs b/core/src/async_util/task_group.rs
index 0fb4855..3de2735 100644
--- a/core/src/async_util/task_group.rs
+++ b/core/src/async_util/task_group.rs
@@ -17,7 +17,7 @@ use super::{executor::global_executor, select, CondWait, Either, Executor};
/// async {
///
/// let ex = Arc::new(smol::Executor::new());
-/// let group = TaskGroup::new(ex);
+/// let group = TaskGroup::with_executor(ex);
///
/// group.spawn(smol::Timer::never(), |_| async {});
///
@@ -34,10 +34,10 @@ pub struct TaskGroup<'a> {
}
impl TaskGroup<'static> {
- /// Creates a new task group without providing an executor
+ /// Creates a new TaskGroup without providing an executor
///
/// This will Spawn a task onto a global executor (single-threaded by default).
- pub fn new_without_executor() -> Self {
+ pub fn new() -> Self {
Self {
tasks: Mutex::new(Vec::new()),
stop_signal: Arc::new(CondWait::new()),
@@ -47,8 +47,8 @@ impl TaskGroup<'static> {
}
impl<'a> TaskGroup<'a> {
- /// Creates a new task group
- pub fn new(executor: Executor<'a>) -> Self {
+ /// Creates a new TaskGroup by providing an executor
+ pub fn with_executor(executor: Executor<'a>) -> Self {
Self {
tasks: Mutex::new(Vec::new()),
stop_signal: Arc::new(CondWait::new()),
@@ -75,7 +75,7 @@ impl<'a> TaskGroup<'a> {
self.tasks.lock().unwrap().push(task);
}
- /// Checks if the task group is empty.
+ /// Checks if the TaskGroup is empty.
pub fn is_empty(&self) -> bool {
self.tasks.lock().unwrap().is_empty()
}
@@ -100,6 +100,12 @@ impl<'a> TaskGroup<'a> {
}
}
+impl Default for TaskGroup<'static> {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
/// The result of a spawned task.
#[derive(Debug)]
pub enum TaskResult<T> {
@@ -172,10 +178,10 @@ mod tests {
use std::{future, sync::Arc};
#[test]
- fn test_task_group() {
+ fn test_task_group_with_executor() {
let ex = Arc::new(smol::Executor::new());
smol::block_on(ex.clone().run(async move {
- let group = Arc::new(TaskGroup::new(ex));
+ let group = Arc::new(TaskGroup::with_executor(ex));
group.spawn(future::ready(0), |res| async move {
assert!(matches!(res, TaskResult::Completed(0)));
@@ -204,9 +210,9 @@ mod tests {
}
#[test]
- fn test_task_group_without_executor() {
+ fn test_task_group() {
smol::block_on(async {
- let group = Arc::new(TaskGroup::new_without_executor());
+ let group = Arc::new(TaskGroup::new());
group.spawn(future::ready(0), |res| async move {
assert!(matches!(res, TaskResult::Completed(0)));