substrait/parse/
typed.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! A generic new type wrapper.
4
5use std::{any, fmt, hash, marker::PhantomData};
6
7/// A generic new type wrapper for values of type `T` that belong to items of type `U`.
8pub struct Typed<T, U> {
9    /// The wrapped value.
10    value: T,
11    /// The attached type.
12    _type: PhantomData<U>,
13}
14
15impl<T, U> Typed<T, U> {
16    /// Internal method to construct a new wrapper from a value.
17    pub(crate) fn new(value: T) -> Self {
18        Self {
19            value,
20            _type: PhantomData,
21        }
22    }
23
24    /// Returns a reference to the wrapped value.
25    pub fn value(&self) -> &T {
26        &self.value
27    }
28
29    /// Returns the inner value.
30    pub fn into_inner(self) -> T {
31        self.value
32    }
33}
34
35impl<T: AsRef<V>, U, V: ?Sized> AsRef<V> for Typed<T, U> {
36    fn as_ref(&self) -> &V {
37        self.value.as_ref()
38    }
39}
40
41impl<T: Clone, U> Clone for Typed<T, U> {
42    fn clone(&self) -> Self {
43        Self {
44            value: self.value.clone(),
45            _type: self._type,
46        }
47    }
48}
49
50impl<T: Copy, U> Copy for Typed<T, U> {}
51
52impl<T: fmt::Debug, U> fmt::Debug for Typed<T, U> {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        f.debug_tuple(any::type_name::<U>())
55            .field(&self.value)
56            .finish()
57    }
58}
59
60impl<T: fmt::Display, U> fmt::Display for Typed<T, U> {
61    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62        fmt::Display::fmt(&self.value, f)
63    }
64}
65
66impl<T: PartialEq, U> PartialEq for Typed<T, U> {
67    fn eq(&self, other: &Self) -> bool {
68        self.value == other.value
69    }
70}
71
72impl<T: Eq, U> Eq for Typed<T, U> {}
73
74impl<T: hash::Hash, U> hash::Hash for Typed<T, U> {
75    fn hash<H: hash::Hasher>(&self, state: &mut H) {
76        self.value.hash(state);
77    }
78}
79
80/// A generic anchor new type for the anchor mechanism used in Substrait data.
81pub type Anchor<T> = Typed<u32, T>;