pub enum Value {
Null(DataType),
Integer(i64),
Float(f64),
Text(SmartString),
Boolean(bool),
Timestamp(DateTime<Utc>),
Json(CompactArc<str>),
}Expand description
A runtime value with type information
Each variant carries its data directly, avoiding the need for interface indirection or separate value references.
§Memory Layout (16 bytes)
Value is exactly 16 bytes due to niche optimization:
- Text(SmartString): 16 bytes with niches in tag byte (values 17-255 unused)
- Json(Arc
): 8 bytes thin pointer with null niche - Rust stores Value’s discriminant in these niche values
Note: Text uses SmartString for inline storage of strings up to 15 bytes.
Longer strings use Arc
Variants§
Null(DataType)
NULL value with optional type hint
Integer(i64)
64-bit signed integer
Float(f64)
64-bit floating point
Text(SmartString)
UTF-8 text string (SmartString: inline ≤15 bytes, Arc for larger)
Boolean(bool)
Boolean value
Timestamp(DateTime<Utc>)
Timestamp (UTC)
Json(CompactArc<str>)
JSON document (CompactArc
Implementations§
Source§impl Value
impl Value
Sourcepub fn null_unknown() -> Self
pub fn null_unknown() -> Self
Create a NULL value with unknown type
Sourcepub fn text(value: impl Into<String>) -> Self
pub fn text(value: impl Into<String>) -> Self
Create a text value
Uses SmartString::from_string_shared() for heap strings to enable
O(1) clone via Arc
Sourcepub fn text_arc(value: Arc<str>) -> Self
pub fn text_arc(value: Arc<str>) -> Self
Create a text value from Arc
Preserves the Arc reference for O(1) clone and sharing.
Sourcepub fn json_arc(value: CompactArc<str>) -> Self
pub fn json_arc(value: CompactArc<str>) -> Self
Create a JSON value from CompactArc
Sourcepub fn as_int64(&self) -> Option<i64>
pub fn as_int64(&self) -> Option<i64>
Extract as i64, with type coercion
Returns None if:
- Value is NULL
- Conversion is not possible
Sourcepub fn as_float64(&self) -> Option<f64>
pub fn as_float64(&self) -> Option<f64>
Extract as f64, with type coercion
Sourcepub fn as_boolean(&self) -> Option<bool>
pub fn as_boolean(&self) -> Option<bool>
Extract as boolean, with type coercion
Sourcepub fn as_arc_str(&self) -> Option<CompactArc<str>>
pub fn as_arc_str(&self) -> Option<CompactArc<str>>
Extract as CompactArc
Sourcepub fn as_timestamp(&self) -> Option<DateTime<Utc>>
pub fn as_timestamp(&self) -> Option<DateTime<Utc>>
Extract as DateTime
Sourcepub fn compare(&self, other: &Value) -> Result<Ordering>
pub fn compare(&self, other: &Value) -> Result<Ordering>
Compare two values for ordering
Returns:
- Ok(Ordering::Less) if self < other
- Ok(Ordering::Equal) if self == other
- Ok(Ordering::Greater) if self > other
- Err if comparison is not possible
Sourcepub fn from_typed(value: Option<&dyn Any>, data_type: DataType) -> Self
pub fn from_typed(value: Option<&dyn Any>, data_type: DataType) -> Self
Create a Value from a typed value with explicit data type
Sourcepub fn coerce_to_type(&self, target_type: DataType) -> Value
pub fn coerce_to_type(&self, target_type: DataType) -> Value
Coerce this value to the target data type
Type coercion rules:
- Integer column receiving Float → converts to Integer
- Float column receiving Integer → converts to Float
- Text column receiving any type → converts to Text
- Timestamp column receiving String → parses timestamp
- JSON column receiving valid JSON string → stores as JSON
- Boolean column receiving Integer/String → converts to Boolean
Returns the coerced value, or NULL if coercion fails.
Sourcepub fn into_coerce_to_type(self, target_type: DataType) -> Value
pub fn into_coerce_to_type(self, target_type: DataType) -> Value
Coerce value to target type, consuming self OPTIMIZATION: Avoids clone when types already match
Trait Implementations§
Source§impl FromIterator<Value> for Row
impl FromIterator<Value> for Row
Source§impl FromValue for Value
impl FromValue for Value
Source§fn from_value(value: &Value) -> Result<Self>
fn from_value(value: &Value) -> Result<Self>
Source§impl Ord for Value
Total ordering implementation for Value
impl Ord for Value
Total ordering implementation for Value
This is required for using Value as a key in BTreeMap/BTreeSet. The ordering is defined as follows:
- NULLs are always ordered first (smallest)
- Numeric types (Integer, Float) are compared by numeric value (consistent with PartialEq)
- Other different data types are ordered by their type discriminant
- Same data types use their natural ordering
IMPORTANT: This ordering MUST be consistent with PartialEq. Since Integer(5) == Float(5.0) per PartialEq, we must ensure Integer(5).cmp(&Float(5.0)) == Ordering::Equal. Violating this contract causes BTreeMap corruption.
Note: This differs from SQL NULL semantics where NULL comparisons return UNKNOWN. This ordering is only for internal index structure.
Source§impl PartialOrd for Value
impl PartialOrd for Value
impl Eq for Value
Auto Trait Implementations§
impl Freeze for Value
impl RefUnwindSafe for Value
impl Send for Value
impl Sync for Value
impl Unpin for Value
impl UnwindSafe for Value
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more