Skip to main content

qubit_value/multi_values/
multi_values_constructor.rs

1// =============================================================================
2//    Copyright (c) 2025 - 2026 Haixing Hu.
3//
4//    SPDX-License-Identifier: Apache-2.0
5//
6//    Licensed under the Apache License, Version 2.0.
7// =============================================================================
8
9//! `From<T>` implementations for supported `MultiValues` input forms.
10
11use super::multi_values::MultiValues;
12
13/// Collects borrowed string values into owned strings.
14///
15/// # Type Parameters
16///
17/// * `I` - Iterator source yielding borrowed UTF-8 strings.
18///
19/// # Parameters
20///
21/// * `values` - Borrowed strings to copy into the returned collection.
22///
23/// # Returns
24///
25/// Owned strings in the source iteration order.
26#[inline]
27fn collect_strings<'a, I>(values: I) -> Vec<String>
28where
29    I: IntoIterator<Item = &'a str>,
30{
31    values.into_iter().map(str::to_owned).collect()
32}
33
34/// Implements owned collection conversions from the shared value table.
35macro_rules! impl_multi_values_from_table {
36    (
37        ;
38        $(
39            (
40                [$($cfg:meta),*],
41                $variant:ident,
42                $type:ty,
43                $data_type:expr,
44                $materialization:ident,
45                $json_class:ident,
46                $number_projection:ident,
47                $value_doc:literal,
48                $multi_doc:literal
49                $(, $_wire:tt)*
50            )
51        ),+ $(,)?
52    ) => {
53        $(
54            $(#[$cfg])*
55            impl From<$type> for MultiValues {
56                #[inline]
57                fn from(value: $type) -> Self {
58                    MultiValues::$variant(vec![value])
59                }
60            }
61
62            $(#[$cfg])*
63            impl From<Vec<$type>> for MultiValues {
64                #[inline]
65                fn from(values: Vec<$type>) -> Self {
66                    MultiValues::$variant(values)
67                }
68            }
69
70            $(#[$cfg])*
71            impl From<&[$type]> for MultiValues {
72                #[inline]
73                fn from(values: &[$type]) -> Self {
74                    MultiValues::$variant(values.to_vec())
75                }
76            }
77
78            $(#[$cfg])*
79            impl From<&Vec<$type>> for MultiValues {
80                #[inline]
81                fn from(values: &Vec<$type>) -> Self {
82                    MultiValues::$variant(values.clone())
83                }
84            }
85
86            $(#[$cfg])*
87            impl<const N: usize> From<[$type; N]> for MultiValues {
88                #[inline]
89                fn from(values: [$type; N]) -> Self {
90                    MultiValues::$variant(Vec::from(values))
91                }
92            }
93
94            $(#[$cfg])*
95            impl<const N: usize> From<&[$type; N]> for MultiValues {
96                #[inline]
97                fn from(values: &[$type; N]) -> Self {
98                    MultiValues::$variant(values.to_vec())
99                }
100            }
101        )+
102    };
103}
104
105for_each_value_type!(impl_multi_values_from_table);
106
107impl From<&str> for MultiValues {
108    #[inline]
109    fn from(value: &str) -> Self {
110        MultiValues::String(vec![value.to_string()])
111    }
112}
113
114impl<'a> From<Vec<&'a str>> for MultiValues {
115    #[inline]
116    fn from(values: Vec<&'a str>) -> Self {
117        MultiValues::String(collect_strings(values))
118    }
119}
120
121impl<'a, 'b> From<&'a [&'b str]> for MultiValues {
122    #[inline]
123    fn from(values: &'a [&'b str]) -> Self {
124        MultiValues::String(collect_strings(values.iter().copied()))
125    }
126}
127
128impl<'a, 'b> From<&'a Vec<&'b str>> for MultiValues {
129    #[inline]
130    fn from(values: &'a Vec<&'b str>) -> Self {
131        MultiValues::String(collect_strings(values.iter().copied()))
132    }
133}
134
135impl<'a, const N: usize> From<[&'a str; N]> for MultiValues {
136    #[inline]
137    fn from(values: [&'a str; N]) -> Self {
138        MultiValues::String(collect_strings(values))
139    }
140}
141
142impl<'a, 'b, const N: usize> From<&'a [&'b str; N]> for MultiValues {
143    #[inline]
144    fn from(values: &'a [&'b str; N]) -> Self {
145        MultiValues::String(collect_strings(values.iter().copied()))
146    }
147}