Skip to main content

qubit_executor/task/
task_handle_future.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026 Haixing Hu.
4 *
5 *    SPDX-License-Identifier: Apache-2.0
6 *
7 *    Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10use std::{
11    future::Future,
12    pin::Pin,
13    task::{
14        Context,
15        Poll,
16    },
17};
18
19use oneshot::AsyncReceiver;
20
21use super::{
22    TaskExecutionError,
23    TaskResult,
24};
25
26/// Future returned when awaiting a task handle by value.
27pub struct TaskHandleFuture<R, E> {
28    /// Async receiver created from the task result receiver.
29    receiver: AsyncReceiver<TaskResult<R, E>>,
30}
31
32impl<R, E> TaskHandleFuture<R, E> {
33    /// Creates a task-handle future from an async result receiver.
34    ///
35    /// # Parameters
36    ///
37    /// * `receiver` - Async receiver that resolves to the final task result.
38    ///
39    /// # Returns
40    ///
41    /// A future resolving to the final task result.
42    #[inline]
43    pub(crate) const fn new(receiver: AsyncReceiver<TaskResult<R, E>>) -> Self {
44        Self { receiver }
45    }
46}
47
48impl<R, E> Future for TaskHandleFuture<R, E> {
49    type Output = TaskResult<R, E>;
50
51    /// Polls the result receiver for the final task result.
52    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
53        let this = self.get_mut();
54        Pin::new(&mut this.receiver)
55            .poll(cx)
56            .map(|result| result.unwrap_or(Err(TaskExecutionError::Dropped)))
57    }
58}