1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
45use std::sync::atomic::{AtomicUsize, Ordering};
67// Suffix used for unique names.
8static UNIQUE_NAME_SUFFIX: AtomicUsize = AtomicUsize::new(0);
910/// Generates a unique name that can be used in inspect nodes and properties that will be prefixed
11/// by the given `prefix`.
12pub fn unique_name(prefix: &str) -> String {
13let suffix = UNIQUE_NAME_SUFFIX.fetch_add(1, Ordering::Relaxed);
14format!("{}{}", prefix, suffix)
15}
1617#[cfg(test)]
18mod tests {
19use super::*;
20use crate::writer::Inspector;
21use diagnostics_assertions::assert_data_tree;
2223#[fuchsia::test]
24fn test_unique_name() {
25let inspector = Inspector::default();
2627let name_1 = unique_name("a");
28assert_eq!(name_1, "a0");
29 inspector.root().record_uint(name_1, 1);
3031let name_2 = unique_name("a");
32assert_eq!(name_2, "a1");
33 inspector.root().record_uint(name_2, 1);
3435assert_data_tree!(inspector, root: {
36 a0: 1u64,
37 a1: 1u64,
38 });
39 }
40}