Skip to main content

qubit_config/
config_names.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 ******************************************************************************/
10
11//! Ergonomic configuration key list argument adapters.
12
13/// Provides borrowed access to a candidate configuration key list.
14pub trait ConfigNames {
15    /// Invokes `operation` with this argument as a string slice list.
16    fn with_config_names<R>(self, operation: impl FnOnce(&[&str]) -> R) -> R;
17}
18
19impl ConfigNames for &[&str] {
20    #[inline]
21    fn with_config_names<R>(self, operation: impl FnOnce(&[&str]) -> R) -> R {
22        operation(self)
23    }
24}
25
26impl<const N: usize> ConfigNames for [&str; N] {
27    #[inline]
28    fn with_config_names<R>(self, operation: impl FnOnce(&[&str]) -> R) -> R {
29        operation(&self)
30    }
31}
32
33impl<const N: usize> ConfigNames for &[&str; N] {
34    #[inline]
35    fn with_config_names<R>(self, operation: impl FnOnce(&[&str]) -> R) -> R {
36        operation(self.as_slice())
37    }
38}
39
40impl ConfigNames for Vec<&str> {
41    #[inline]
42    fn with_config_names<R>(self, operation: impl FnOnce(&[&str]) -> R) -> R {
43        operation(self.as_slice())
44    }
45}
46
47impl ConfigNames for &Vec<&str> {
48    #[inline]
49    fn with_config_names<R>(self, operation: impl FnOnce(&[&str]) -> R) -> R {
50        operation(self.as_slice())
51    }
52}
53
54impl ConfigNames for &[String] {
55    #[inline]
56    fn with_config_names<R>(self, operation: impl FnOnce(&[&str]) -> R) -> R {
57        let names: Vec<&str> = self.iter().map(String::as_str).collect();
58        operation(names.as_slice())
59    }
60}
61
62impl<const N: usize> ConfigNames for [String; N] {
63    #[inline]
64    fn with_config_names<R>(self, operation: impl FnOnce(&[&str]) -> R) -> R {
65        let names: Vec<&str> = self.iter().map(String::as_str).collect();
66        operation(names.as_slice())
67    }
68}
69
70impl<const N: usize> ConfigNames for &[String; N] {
71    #[inline]
72    fn with_config_names<R>(self, operation: impl FnOnce(&[&str]) -> R) -> R {
73        let names: Vec<&str> = self.iter().map(String::as_str).collect();
74        operation(names.as_slice())
75    }
76}
77
78impl ConfigNames for Vec<String> {
79    #[inline]
80    fn with_config_names<R>(self, operation: impl FnOnce(&[&str]) -> R) -> R {
81        let names: Vec<&str> = self.iter().map(String::as_str).collect();
82        operation(names.as_slice())
83    }
84}
85
86impl ConfigNames for &Vec<String> {
87    #[inline]
88    fn with_config_names<R>(self, operation: impl FnOnce(&[&str]) -> R) -> R {
89        let names: Vec<&str> = self.iter().map(String::as_str).collect();
90        operation(names.as_slice())
91    }
92}