Skip to main content

corium_core/
value.rs

1//! Engine-internal values.
2
3use std::{cmp::Ordering, sync::Arc};
4
5use crate::{EntityId, KwId};
6
7/// A finite `f64` wrapper ordered by IEEE-754 total-order bit transformation.
8#[derive(Clone, Copy, Debug)]
9pub struct TotalF64(pub f64);
10
11impl TotalF64 {
12    /// Returns the sortable transformed bits.
13    #[must_use]
14    pub const fn sortable_bits(self) -> u64 {
15        let bits = self.0.to_bits();
16        if (bits & (1_u64 << 63)) == 0 {
17            bits ^ (1_u64 << 63)
18        } else {
19            !bits
20        }
21    }
22}
23impl PartialEq for TotalF64 {
24    fn eq(&self, other: &Self) -> bool {
25        self.sortable_bits() == other.sortable_bits()
26    }
27}
28impl Eq for TotalF64 {}
29impl PartialOrd for TotalF64 {
30    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
31        Some(self.cmp(other))
32    }
33}
34impl Ord for TotalF64 {
35    fn cmp(&self, other: &Self) -> Ordering {
36        self.sortable_bits().cmp(&other.sortable_bits())
37    }
38}
39
40/// Core v1 Corium value types.
41#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
42pub enum Value {
43    /// Boolean.
44    Bool(bool),
45    /// Signed 64-bit integer.
46    Long(i64),
47    /// Totally ordered double.
48    Double(TotalF64),
49    /// Milliseconds since Unix epoch, UTC.
50    Instant(i64),
51    /// 128-bit UUID bytes represented as an integer.
52    Uuid(u128),
53    /// Interned keyword.
54    Keyword(KwId),
55    /// UTF-8 string.
56    Str(Arc<str>),
57    /// Byte array.
58    Bytes(Arc<[u8]>),
59    /// Entity reference.
60    Ref(EntityId),
61}
62
63impl Value {
64    /// Returns whether this value has the requested schema type.
65    #[must_use]
66    pub const fn has_type(&self, value_type: crate::ValueType) -> bool {
67        matches!(
68            (self, value_type),
69            (Self::Bool(_), crate::ValueType::Bool)
70                | (Self::Long(_), crate::ValueType::Long)
71                | (Self::Double(_), crate::ValueType::Double)
72                | (Self::Instant(_), crate::ValueType::Instant)
73                | (Self::Uuid(_), crate::ValueType::Uuid)
74                | (Self::Keyword(_), crate::ValueType::Keyword)
75                | (Self::Str(_), crate::ValueType::Str)
76                | (Self::Bytes(_), crate::ValueType::Bytes)
77                | (Self::Ref(_), crate::ValueType::Ref)
78        )
79    }
80}