Skip to content

Commit

Permalink
feat(named-task): expose name getter
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverNChalk committed Jan 3, 2025
1 parent 5bfa986 commit fc41901
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/tokio/named_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,24 @@ use tokio::task::JoinError;
/// This enables a parent task to await all children simultaneously but still
/// determine what child has exited for logging/diagnosis purposes.
#[derive(Debug)]
pub struct NamedTask<Ret = (), Id = String>
pub struct NamedTask<Ret = (), Name = String>
where
Id: Clone + Unpin,
Name: Clone + Unpin,
{
task: Pin<Box<tokio::task::JoinHandle<Ret>>>,
id: Id,
name: Name,
}

impl<R, I> NamedTask<R, I>
impl<Ret, Name> NamedTask<Ret, Name>
where
I: Clone + Unpin,
Name: Clone + Unpin,
{
pub fn new(task: tokio::task::JoinHandle<R>, id: I) -> Self {
NamedTask { task: Box::pin(task), id }
pub fn new(task: tokio::task::JoinHandle<Ret>, name: Name) -> Self {
NamedTask { task: Box::pin(task), name }
}

pub fn name(&self) -> &Name {
&self.name
}
}

Expand All @@ -35,6 +39,6 @@ where
mut self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Self::Output> {
self.task.as_mut().poll(cx).map(|v| (self.id.clone(), v))
self.task.as_mut().poll(cx).map(|v| (self.name.clone(), v))
}
}

0 comments on commit fc41901

Please sign in to comment.