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#[inline]
15fn collect_strings<'a, I>(values: I) -> Vec<String>
16where
17    I: IntoIterator<Item = &'a str>,
18{
19    values.into_iter().map(str::to_owned).collect()
20}
21
22macro_rules! impl_multi_values_from_table {
23    (
24        ;
25        $(
26            (
27                [$($cfg:meta),*],
28                $variant:ident,
29                $type:ty,
30                $data_type:expr,
31                $materialization:ident,
32                $json_class:ident,
33                $number_projection:ident,
34                $value_doc:literal,
35                $multi_doc:literal
36            )
37        ),+ $(,)?
38    ) => {
39        $(
40            $(#[$cfg])*
41            impl From<$type> for MultiValues {
42                #[inline]
43                fn from(value: $type) -> Self {
44                    MultiValues::$variant(vec![value])
45                }
46            }
47
48            $(#[$cfg])*
49            impl From<Vec<$type>> for MultiValues {
50                #[inline]
51                fn from(values: Vec<$type>) -> Self {
52                    MultiValues::$variant(values)
53                }
54            }
55
56            $(#[$cfg])*
57            impl From<&[$type]> for MultiValues {
58                #[inline]
59                fn from(values: &[$type]) -> Self {
60                    MultiValues::$variant(values.to_vec())
61                }
62            }
63
64            $(#[$cfg])*
65            impl From<&Vec<$type>> for MultiValues {
66                #[inline]
67                fn from(values: &Vec<$type>) -> Self {
68                    MultiValues::$variant(values.clone())
69                }
70            }
71
72            $(#[$cfg])*
73            impl<const N: usize> From<[$type; N]> for MultiValues {
74                #[inline]
75                fn from(values: [$type; N]) -> Self {
76                    MultiValues::$variant(Vec::from(values))
77                }
78            }
79
80            $(#[$cfg])*
81            impl<const N: usize> From<&[$type; N]> for MultiValues {
82                #[inline]
83                fn from(values: &[$type; N]) -> Self {
84                    MultiValues::$variant(values.to_vec())
85                }
86            }
87        )+
88    };
89}
90
91for_each_value_type!(impl_multi_values_from_table);
92
93impl From<&str> for MultiValues {
94    #[inline]
95    fn from(value: &str) -> Self {
96        MultiValues::String(vec![value.to_string()])
97    }
98}
99
100impl<'a> From<Vec<&'a str>> for MultiValues {
101    #[inline]
102    fn from(values: Vec<&'a str>) -> Self {
103        MultiValues::String(collect_strings(values))
104    }
105}
106
107impl<'a, 'b> From<&'a [&'b str]> for MultiValues {
108    #[inline]
109    fn from(values: &'a [&'b str]) -> Self {
110        MultiValues::String(collect_strings(values.iter().copied()))
111    }
112}
113
114impl<'a, 'b> From<&'a Vec<&'b str>> for MultiValues {
115    #[inline]
116    fn from(values: &'a Vec<&'b str>) -> Self {
117        MultiValues::String(collect_strings(values.iter().copied()))
118    }
119}
120
121impl<'a, const N: usize> From<[&'a str; N]> for MultiValues {
122    #[inline]
123    fn from(values: [&'a str; N]) -> Self {
124        MultiValues::String(collect_strings(values))
125    }
126}
127
128impl<'a, 'b, const N: usize> From<&'a [&'b str; N]> for MultiValues {
129    #[inline]
130    fn from(values: &'a [&'b str; N]) -> Self {
131        MultiValues::String(collect_strings(values.iter().copied()))
132    }
133}