Skip to main content

qubit_value/value/
value_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 all supported `Value` input types.
10
11use super::value::Value;
12
13/// Implements owned scalar conversions from the shared value table.
14macro_rules! impl_value_from_table {
15    (
16        ;
17        $(
18            (
19                [$($cfg:meta),*],
20                $variant:ident,
21                $type:ty,
22                $data_type:expr,
23                $materialization:ident,
24                $json_class:ident,
25                $number_projection:ident,
26                $value_doc:literal,
27                $multi_doc:literal
28                $(, $_wire:tt)*
29            )
30        ),+ $(,)?
31    ) => {
32        $(
33            $(#[$cfg])*
34            $(#[cfg_attr(docsrs, doc($cfg))])*
35            impl From<$type> for Value {
36                /// Wraps an owned payload in its matching runtime value type.
37                ///
38                /// # Parameters
39                ///
40                /// * `value` - Payload moved into the returned container without cloning.
41                ///
42                /// # Returns
43                ///
44                /// A typed value owning the supplied payload.
45                #[inline(always)]
46                fn from(value: $type) -> Self {
47                    Value::$variant(value)
48                }
49            }
50        )+
51    };
52}
53
54for_each_value_type!(impl_value_from_table);
55
56impl From<&str> for Value {
57    /// Copies borrowed UTF-8 text into an owned string value.
58    ///
59    /// The returned container is independent of the input string's lifetime.
60    ///
61    /// # Parameters
62    ///
63    /// * `value` - Text copied into the container's string storage.
64    ///
65    /// # Returns
66    ///
67    /// A string value owning a copy of `value`.
68    #[inline]
69    fn from(value: &str) -> Self {
70        Value::String(value.to_string())
71    }
72}