Skip to main content

qubit_codec/value/
value_codec_registration.rs

1// =============================================================================
2//    Copyright (c) 2026 Haixing Hu.
3//
4//    SPDX-License-Identifier: Apache-2.0
5// =============================================================================
6
7//! Distributed value-codec registrations.
8
9use crate::ValueCodecDescriptor;
10use crate::ValueCodecId;
11use crate::ValueCodecRegistrationSource;
12
13/// One statically linked value-codec implementation.
14#[derive(Clone, Copy, Debug)]
15pub struct ValueCodecRegistration {
16    id: ValueCodecId,
17    descriptor: &'static ValueCodecDescriptor,
18    source: ValueCodecRegistrationSource,
19}
20
21impl ValueCodecRegistration {
22    /// Creates a registration from validated static facts.
23    #[doc(hidden)]
24    #[must_use]
25    pub const fn new(
26        id: ValueCodecId,
27        descriptor: &'static ValueCodecDescriptor,
28        source: ValueCodecRegistrationSource,
29    ) -> Self {
30        Self { id, descriptor, source }
31    }
32
33    /// Returns the stable value-codec ID.
34    #[must_use]
35    pub const fn id(&self) -> ValueCodecId {
36        self.id
37    }
38
39    /// Returns the executable descriptor.
40    #[must_use]
41    pub const fn descriptor(&self) -> &'static ValueCodecDescriptor {
42        self.descriptor
43    }
44
45    /// Returns the linked source location.
46    #[must_use]
47    pub const fn source(&self) -> ValueCodecRegistrationSource {
48        self.source
49    }
50}
51
52/// Registers a default-constructible bidirectional string codec.
53#[macro_export]
54macro_rules! register_value_codec {
55    (id = $id:literal, codec = $codec:ty, value = $value:ty $(,)?) => {
56        const _: () = {
57            static DESCRIPTOR: $crate::ValueCodecDescriptor = $crate::ValueCodecDescriptor::of::<$codec, $value>();
58
59            fn registration() -> $crate::ValueCodecRegistration {
60                $crate::ValueCodecRegistration::new(
61                    $crate::ValueCodecId::new($id),
62                    &DESCRIPTOR,
63                    $crate::ValueCodecRegistrationSource::new(env!("CARGO_PKG_NAME"), module_path!(), file!(), line!()),
64                )
65            }
66
67            $crate::__private::inventory::submit! {
68                $crate::ValueCodecRegistrationFactory(registration)
69            }
70        };
71    };
72}