Skip to main content

qubit_value/multi_values/
multi_values_constructor_arg.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//! Internal dispatch implementations for `MultiValues::new<S>()` arguments.
12
13use std::collections::HashMap;
14use std::time::Duration;
15
16use bigdecimal::BigDecimal;
17use chrono::{
18    DateTime,
19    NaiveDate,
20    NaiveDateTime,
21    NaiveTime,
22    Utc,
23};
24use num_bigint::BigInt;
25use url::Url;
26
27use super::multi_values::MultiValues;
28use super::multi_values_constructor::MultiValuesConstructor;
29
30/// Collects borrowed string values into owned strings.
31#[inline]
32fn collect_strings<'a, I>(values: I) -> Vec<String>
33where
34    I: IntoIterator<Item = &'a str>,
35{
36    let mut result = Vec::new();
37    for value in values {
38        result.push(value.to_string());
39    }
40    result
41}
42
43/// Private marker trait used to prevent downstream implementations.
44trait MultiValuesConstructorArgSealed {}
45
46/// Internal dispatch trait for `MultiValues::new<S>()` arguments.
47#[allow(private_bounds)]
48pub trait MultiValuesConstructorArg<'a>: MultiValuesConstructorArgSealed {
49    /// Builds a `MultiValues` instance from this argument.
50    fn into_multi_values(self) -> MultiValues;
51}
52
53macro_rules! impl_multi_values_constructor_arg {
54    ($type:ty) => {
55        impl MultiValuesConstructorArgSealed for Vec<$type> {}
56
57        impl<'a> MultiValuesConstructorArg<'a> for Vec<$type> {
58            #[inline]
59            fn into_multi_values(self) -> MultiValues {
60                <MultiValues as MultiValuesConstructor<$type>>::from_vec(self)
61            }
62        }
63
64        impl<'a> MultiValuesConstructorArgSealed for &'a [$type] {}
65
66        impl<'a> MultiValuesConstructorArg<'a> for &'a [$type]
67        where
68            $type: Clone,
69        {
70            #[inline]
71            fn into_multi_values(self) -> MultiValues {
72                <MultiValues as MultiValuesConstructor<$type>>::from_vec(self.to_vec())
73            }
74        }
75
76        impl<'a> MultiValuesConstructorArgSealed for &'a Vec<$type> {}
77
78        impl<'a> MultiValuesConstructorArg<'a> for &'a Vec<$type>
79        where
80            $type: Clone,
81        {
82            #[inline]
83            fn into_multi_values(self) -> MultiValues {
84                <MultiValues as MultiValuesConstructor<$type>>::from_vec(self.clone())
85            }
86        }
87
88        impl<const N: usize> MultiValuesConstructorArgSealed for [$type; N] {}
89
90        impl<'a, const N: usize> MultiValuesConstructorArg<'a> for [$type; N] {
91            #[inline]
92            fn into_multi_values(self) -> MultiValues {
93                <MultiValues as MultiValuesConstructor<$type>>::from_vec(Vec::from(self))
94            }
95        }
96
97        impl<'a, const N: usize> MultiValuesConstructorArgSealed for &'a [$type; N] {}
98
99        impl<'a, const N: usize> MultiValuesConstructorArg<'a> for &'a [$type; N]
100        where
101            $type: Clone,
102        {
103            #[inline]
104            fn into_multi_values(self) -> MultiValues {
105                <MultiValues as MultiValuesConstructor<$type>>::from_vec(self.to_vec())
106            }
107        }
108    };
109}
110
111impl_multi_values_constructor_arg!(bool);
112impl_multi_values_constructor_arg!(char);
113impl_multi_values_constructor_arg!(i8);
114impl_multi_values_constructor_arg!(i16);
115impl_multi_values_constructor_arg!(i32);
116impl_multi_values_constructor_arg!(i64);
117impl_multi_values_constructor_arg!(i128);
118impl_multi_values_constructor_arg!(u8);
119impl_multi_values_constructor_arg!(u16);
120impl_multi_values_constructor_arg!(u32);
121impl_multi_values_constructor_arg!(u64);
122impl_multi_values_constructor_arg!(u128);
123impl_multi_values_constructor_arg!(isize);
124impl_multi_values_constructor_arg!(usize);
125impl_multi_values_constructor_arg!(f32);
126impl_multi_values_constructor_arg!(f64);
127impl_multi_values_constructor_arg!(String);
128impl_multi_values_constructor_arg!(NaiveDate);
129impl_multi_values_constructor_arg!(NaiveTime);
130impl_multi_values_constructor_arg!(NaiveDateTime);
131impl_multi_values_constructor_arg!(DateTime<Utc>);
132impl_multi_values_constructor_arg!(BigInt);
133impl_multi_values_constructor_arg!(BigDecimal);
134impl_multi_values_constructor_arg!(Duration);
135impl_multi_values_constructor_arg!(Url);
136impl_multi_values_constructor_arg!(HashMap<String, String>);
137impl_multi_values_constructor_arg!(serde_json::Value);
138
139impl MultiValuesConstructorArgSealed for Vec<&str> {}
140
141impl MultiValuesConstructorArg<'_> for Vec<&str> {
142    #[inline]
143    fn into_multi_values(self) -> MultiValues {
144        MultiValues::String(collect_strings(self))
145    }
146}
147
148impl MultiValuesConstructorArgSealed for &[&str] {}
149
150impl MultiValuesConstructorArg<'_> for &[&str] {
151    #[inline]
152    fn into_multi_values(self) -> MultiValues {
153        MultiValues::String(collect_strings(self.iter().copied()))
154    }
155}
156
157impl MultiValuesConstructorArgSealed for &Vec<&str> {}
158
159impl MultiValuesConstructorArg<'_> for &Vec<&str> {
160    #[inline]
161    fn into_multi_values(self) -> MultiValues {
162        MultiValues::String(collect_strings(self.iter().copied()))
163    }
164}
165
166impl<const N: usize> MultiValuesConstructorArgSealed for [&str; N] {}
167
168impl<const N: usize> MultiValuesConstructorArg<'_> for [&str; N] {
169    #[inline]
170    fn into_multi_values(self) -> MultiValues {
171        MultiValues::String(collect_strings(self))
172    }
173}
174
175impl<const N: usize> MultiValuesConstructorArgSealed for &[&str; N] {}
176
177impl<const N: usize> MultiValuesConstructorArg<'_> for &[&str; N] {
178    #[inline]
179    fn into_multi_values(self) -> MultiValues {
180        MultiValues::String(collect_strings(self.iter().copied()))
181    }
182}