Skip to main content

qubit_value/
into_value_default.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//! Default value adapters for value access APIs.
10
11/// Collects borrowed string values into owned strings.
12///
13/// # Type Parameters
14///
15/// * `I` - Iterator source yielding borrowed UTF-8 strings.
16///
17/// # Parameters
18///
19/// * `values` - Borrowed strings to copy into the returned collection.
20///
21/// # Returns
22///
23/// Owned strings in the source iteration order.
24#[must_use]
25#[inline]
26fn collect_strings<'a, I>(values: I) -> Vec<String>
27where
28    I: IntoIterator<Item = &'a str>,
29{
30    values.into_iter().map(str::to_owned).collect()
31}
32
33/// Converts ergonomic default arguments into the value type expected by a read
34/// API.
35///
36/// # Type Parameters
37///
38/// * `T` - Owned default type required by the read operation.
39///
40/// # Examples
41///
42/// ```
43/// use qubit_value::IntoValueDefault;
44///
45/// let owned: String = "fallback".into_value_default();
46/// assert_eq!(owned, "fallback");
47/// ```
48pub trait IntoValueDefault<T> {
49    /// Converts this argument into the default value.
50    ///
51    /// # Returns
52    ///
53    /// The owned default value expected by the read API.
54    #[must_use]
55    fn into_value_default(self) -> T;
56}
57
58impl<T> IntoValueDefault<T> for T {
59    /// Returns the owned value unchanged as the default.
60    ///
61    /// # Returns
62    ///
63    /// The input value, moved without cloning.
64    #[inline(always)]
65    fn into_value_default(self) -> T {
66        self
67    }
68}
69
70impl IntoValueDefault<String> for &str {
71    /// Copies the borrowed string into an owned default string.
72    ///
73    /// # Returns
74    ///
75    /// An owned string with the same contents as the input.
76    #[inline]
77    fn into_value_default(self) -> String {
78        self.to_string()
79    }
80}
81
82impl IntoValueDefault<String> for &String {
83    /// Clones the borrowed string into an owned default string.
84    ///
85    /// # Returns
86    ///
87    /// An owned string with the same contents as the input.
88    #[inline]
89    fn into_value_default(self) -> String {
90        self.clone()
91    }
92}
93
94impl<T> IntoValueDefault<Vec<T>> for &[T]
95where
96    T: Clone,
97{
98    /// Clones the borrowed slice elements into an owned default vector.
99    ///
100    /// # Returns
101    ///
102    /// An owned vector preserving the input element order.
103    #[inline]
104    fn into_value_default(self) -> Vec<T> {
105        self.to_vec()
106    }
107}
108
109impl<T> IntoValueDefault<Vec<T>> for &Vec<T>
110where
111    T: Clone,
112{
113    /// Clones the borrowed vector elements into an owned default vector.
114    ///
115    /// # Returns
116    ///
117    /// An owned vector preserving the input element order.
118    #[inline]
119    fn into_value_default(self) -> Vec<T> {
120        self.clone()
121    }
122}
123
124impl<T, const N: usize> IntoValueDefault<Vec<T>> for [T; N] {
125    /// Moves the array elements into an owned default vector.
126    ///
127    /// # Returns
128    ///
129    /// An owned vector preserving the input element order.
130    #[inline]
131    fn into_value_default(self) -> Vec<T> {
132        Vec::from(self)
133    }
134}
135
136impl<T, const N: usize> IntoValueDefault<Vec<T>> for &[T; N]
137where
138    T: Clone,
139{
140    /// Clones the borrowed array elements into an owned default vector.
141    ///
142    /// # Returns
143    ///
144    /// An owned vector preserving the input element order.
145    #[inline]
146    fn into_value_default(self) -> Vec<T> {
147        self.to_vec()
148    }
149}
150
151impl IntoValueDefault<Vec<String>> for Vec<&str> {
152    /// Copies each borrowed string into an owned default vector.
153    ///
154    /// # Returns
155    ///
156    /// An owned vector preserving the input element order.
157    #[inline]
158    fn into_value_default(self) -> Vec<String> {
159        collect_strings(self)
160    }
161}
162
163impl IntoValueDefault<Vec<String>> for &[&str] {
164    /// Copies each borrowed slice string into an owned default vector.
165    ///
166    /// # Returns
167    ///
168    /// An owned vector preserving the input element order.
169    #[inline]
170    fn into_value_default(self) -> Vec<String> {
171        collect_strings(self.iter().copied())
172    }
173}
174
175impl IntoValueDefault<Vec<String>> for &Vec<&str> {
176    /// Copies each borrowed vector string into an owned default vector.
177    ///
178    /// # Returns
179    ///
180    /// An owned vector preserving the input element order.
181    #[inline]
182    fn into_value_default(self) -> Vec<String> {
183        collect_strings(self.iter().copied())
184    }
185}
186
187impl<const N: usize> IntoValueDefault<Vec<String>> for [&str; N] {
188    /// Copies each borrowed array string into an owned default vector.
189    ///
190    /// # Returns
191    ///
192    /// An owned vector preserving the input element order.
193    #[inline]
194    fn into_value_default(self) -> Vec<String> {
195        collect_strings(self)
196    }
197}
198
199impl<const N: usize> IntoValueDefault<Vec<String>> for &[&str; N] {
200    /// Copies each borrowed array string into an owned default vector.
201    ///
202    /// # Returns
203    ///
204    /// An owned vector preserving the input element order.
205    #[inline]
206    fn into_value_default(self) -> Vec<String> {
207        collect_strings(self.iter().copied())
208    }
209}