Skip to main content

zyn_core/
ident.rs

1//! Internal identifier generation for template expansion.
2//!
3//! [`Iter`] produces sequential hygienic identifiers (`__zyn_ts_0`, `__zyn_ts_1`,
4//! ...) used by the template expander to name temporaries in expanded code. Not
5//! intended for direct use by proc macro authors.
6//!
7//! # Example
8//!
9//! ```ignore
10//! let mut iter = zyn_core::ident::Iter::new();
11//! assert_eq!(iter.next().unwrap().to_string(), "__zyn_ts_0");
12//! assert_eq!(iter.next().unwrap().to_string(), "__zyn_ts_1");
13//! ```
14
15use proc_macro2::Ident;
16use proc_macro2::Span;
17
18/// An iterator that yields unique internal identifiers for template expansion.
19pub struct Iter {
20    counter: usize,
21}
22
23impl Iter {
24    pub fn new() -> Self {
25        Self { counter: 0 }
26    }
27}
28
29impl Default for Iter {
30    fn default() -> Self {
31        Self::new()
32    }
33}
34
35impl Iterator for Iter {
36    type Item = Ident;
37
38    fn next(&mut self) -> Option<Self::Item> {
39        let id = Ident::new(&format!("__zyn_ts_{}", self.counter), Span::call_site());
40
41        self.counter += 1;
42        Some(id)
43    }
44}