qubit_function/macros/common_name_methods.rs
1/*******************************************************************************
2 *
3 * Copyright (c) 2025 - 2026.
4 * Haixing Hu, Qubit Co. Ltd.
5 *
6 * All rights reserved.
7 *
8 ******************************************************************************/
9
10//! # Common Name Methods Macro
11//!
12//! Generates common name management methods for function-like structs.
13//!
14//! # Author
15//!
16//! Haixing Hu
17
18/// Implements common name management methods for function-like structs.
19///
20/// This macro generates `name`, and `set_name` methods for structs that have
21/// an optional name field. These methods provide a standardized way to get
22/// and set names for debugging and logging purposes.
23///
24/// # Parameters
25///
26/// * `$type_desc:literal` - Description of the type (e.g., "consumer")
27///
28/// # Generated Methods
29///
30/// * `name(&self) -> Option<&str>` - Gets the current name if set
31/// * `set_name(&mut self, name: &str)` - Sets a new name for the instance
32///
33/// # Author
34///
35/// Haixing Hu
36macro_rules! impl_common_name_methods {
37 ($type_desc:literal) => {
38 #[doc = concat!("Gets the name of this ", $type_desc, ".")]
39 ///
40 /// # Returns
41 ///
42 /// Returns `Some(&str)` if a name was set, `None` otherwise.
43 #[inline]
44 pub fn name(&self) -> Option<&str> {
45 self.name.as_deref()
46 }
47
48 #[doc = concat!("Sets the name of this ", $type_desc, ".")]
49 ///
50 /// # Parameters
51 ///
52 #[doc = concat!("* `name` - The name to set for this ", $type_desc)]
53 #[inline]
54 pub fn set_name(&mut self, name: &str) {
55 self.name = Some(name.to_string());
56 }
57 };
58}
59
60pub(crate) use impl_common_name_methods;