Skip to main content

qubit_value/value/
value_ref.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//! Borrowed semantic views for [`crate::Value`].
10
11use qubit_datatype::DataType;
12
13/// Generates semantic views from the same closed table as owned storage.
14macro_rules! define_view {
15    (; $(([$($cfg:meta),*], $variant:ident, $type:ty, $data_type:expr, $materialization:ident, $json_class:ident, $number_projection:ident, $value_doc:literal, $multi_doc:literal $(, $_wire:tt)*)),+ $(,)?) => {
16        /// Borrowed semantic view preserving source types without owning payloads.
17        ///
18        /// The lifetime is that of the source storage; copying this view does
19        /// not clone strings, maps, JSON, or other rich payloads.
20        ///
21        /// # Type Parameters
22        ///
23        /// - `'a`: lifetime of the source storage borrowed by payload references.
24        ///
25        /// # Examples
26        ///
27        /// ```
28        /// use qubit_value::Value;
29        /// use qubit_value::ValueRef;
30        ///
31        /// let value = Value::from(42_i32);
32        /// let view: ValueRef<'_> = value.view();
33        /// assert!(matches!(view, ValueRef::Int32(42)));
34        /// ```
35        #[must_use]
36        #[non_exhaustive]
37        #[derive(Debug, Clone, Copy)]
38        pub enum ValueRef<'a> {
39            /// Unset storage retaining its declared type.
40            Unset(#[doc = "Declared source type."] DataType),
41            $(
42                #[doc = $value_doc]
43                $(#[$cfg])*
44                $(#[cfg_attr(docsrs, doc($cfg))])*
45                $variant(#[doc = "Borrowed or copied source payload."] value_view_payload_type!($variant, $number_projection, 'a, $type)),
46            )+
47        }
48
49    };
50}
51
52for_each_value_type!(define_view);