Skip to main content

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/// * `clear_name(&mut self)` - Clears the current name
33///
34/// # Author
35///
36/// Haixing Hu
37macro_rules! impl_common_name_methods {
38    ($type_desc:literal) => {
39        #[doc = concat!("Gets the name of this ", $type_desc, ".")]
40        ///
41        /// # Returns
42        ///
43        /// Returns `Some(&str)` if a name was set, `None` otherwise.
44        #[inline]
45        pub fn name(&self) -> Option<&str> {
46            self.name.as_deref()
47        }
48
49        #[doc = concat!("Sets the name of this ", $type_desc, ".")]
50        ///
51        /// # Parameters
52        ///
53        #[doc = concat!("* `name` - The name to set for this ", $type_desc)]
54        #[inline]
55        pub fn set_name(&mut self, name: &str) {
56            if self.name.as_deref() != Some(name) {
57                self.name = Some(name.to_owned());
58            }
59        }
60
61        #[doc = concat!("Clears the name of this ", $type_desc, ".")]
62        #[inline]
63        pub fn clear_name(&mut self) {
64            self.name = None;
65        }
66    };
67}
68
69pub(crate) use impl_common_name_methods;