qubit_executor/hook/task_id.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::sync::atomic::{
11 AtomicU64,
12 Ordering,
13};
14
15/// Unique identifier assigned to an accepted task.
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
17pub struct TaskId(u64);
18
19impl TaskId {
20 /// Creates a task id from its raw numeric value.
21 ///
22 /// # Parameters
23 ///
24 /// * `value` - Raw task id value.
25 ///
26 /// # Returns
27 ///
28 /// A task id wrapping `value`.
29 #[inline]
30 pub const fn new(value: u64) -> Self {
31 Self(value)
32 }
33
34 /// Returns the raw numeric task id value.
35 ///
36 /// # Returns
37 ///
38 /// The raw task id value.
39 #[inline]
40 pub const fn get(self) -> u64 {
41 self.0
42 }
43}
44
45static NEXT_TASK_ID: AtomicU64 = AtomicU64::new(1);
46
47/// Allocates a fresh task id.
48///
49/// # Returns
50///
51/// A task id unique within this process until the counter wraps.
52#[inline]
53pub(crate) fn next_task_id() -> TaskId {
54 TaskId(NEXT_TASK_ID.fetch_add(1, Ordering::Relaxed))
55}