pub enum Value {
Show 22 variants
Empty(DataType),
Bool(bool),
Char(char),
Int8(i8),
Int16(i16),
Int32(i32),
Int64(i64),
Int128(i128),
UInt8(u8),
UInt16(u16),
UInt32(u32),
UInt64(u64),
UInt128(u128),
Float32(f32),
Float64(f64),
String(String),
Date(NaiveDate),
Time(NaiveTime),
DateTime(NaiveDateTime),
Instant(DateTime<Utc>),
BigInteger(BigInt),
BigDecimal(BigDecimal),
}
Expand description
Single value container
Uses an enum to represent different types of values, providing type-safe value storage and access.
§Features
- Zero-cost abstraction with compile-time type checking
- Supports multiple basic data types
- Provides two sets of APIs for type checking and type conversion
- Automatic memory management
§Example
use common_rs::util::value::Value;
// Create an integer value
let value = Value::Int32(42);
assert_eq!(value.get_int32().unwrap(), 42);
// Type conversion
let converted = value.as_int64().unwrap();
assert_eq!(converted, 42i64);
// String value
let text = Value::String("hello".to_string());
assert_eq!(text.get_string().unwrap(), "hello");
§Author
Haixing Hu
Variants§
Empty(DataType)
Empty value (has type but no value)
Bool(bool)
Boolean value
Char(char)
Character value
Int8(i8)
8-bit signed integer
Int16(i16)
16-bit signed integer
Int32(i32)
32-bit signed integer
Int64(i64)
64-bit signed integer
Int128(i128)
128-bit signed integer
UInt8(u8)
8-bit unsigned integer
UInt16(u16)
16-bit unsigned integer
UInt32(u32)
32-bit unsigned integer
UInt64(u64)
64-bit unsigned integer
UInt128(u128)
128-bit unsigned integer
Float32(f32)
32-bit floating point number
Float64(f64)
64-bit floating point number
String(String)
String
Date(NaiveDate)
Date
Time(NaiveTime)
Time
DateTime(NaiveDateTime)
Date and time
Instant(DateTime<Utc>)
UTC instant
BigInteger(BigInt)
Big integer type
BigDecimal(BigDecimal)
Big decimal type
Implementations§
Source§impl Value
impl Value
Sourcepub fn new<T>(value: T) -> Selfwhere
Self: ValueConstructor<T>,
pub fn new<T>(value: T) -> Selfwhere
Self: ValueConstructor<T>,
Generic constructor method
Creates a Value
from any supported type, avoiding direct use of enum variants.
§Type Parameters
T
- The type of the value to wrap
§Returns
Returns a Value
wrapping the given value
§Example
use crate::util::value::Value;
// Basic types
let v = Value::new(42i32);
assert_eq!(v.get_int32().unwrap(), 42);
let v = Value::new(true);
assert_eq!(v.get_bool().unwrap(), true);
// String
let v = Value::new("hello".to_string());
assert_eq!(v.get_string().unwrap(), "hello");
Sourcepub fn get<T>(&self) -> ValueResult<T>where
Self: ValueGetter<T>,
pub fn get<T>(&self) -> ValueResult<T>where
Self: ValueGetter<T>,
Generic getter method
Automatically selects the correct getter method based on the target type, performing strict type checking.
§Type Parameters
T
- The target type to retrieve
§Returns
If types match, returns the value of the corresponding type; otherwise returns an error
§Example
use crate::util::value::Value;
let value = Value::Int32(42);
// Through type inference
let num: i32 = value.get().unwrap();
assert_eq!(num, 42);
// Explicitly specify type parameter
let num = value.get::<i32>().unwrap();
assert_eq!(num, 42);
// Different type
let text = Value::String("hello".to_string());
let s: String = text.get().unwrap();
assert_eq!(s, "hello");
// Boolean value
let flag = Value::Bool(true);
let b: bool = flag.get().unwrap();
assert_eq!(b, true);
Sourcepub fn set<T>(&mut self, value: T) -> ValueResult<()>where
Self: ValueSetter<T>,
pub fn set<T>(&mut self, value: T) -> ValueResult<()>where
Self: ValueSetter<T>,
Generic setter method
Automatically selects the correct setter method based on the target type, performing strict type checking.
§Type Parameters
T
- The target type to set
§Parameters
value
- The value to set
§Returns
If setting succeeds, returns Ok(())
; otherwise returns an error
§Example
use crate::util::value::Value;
let mut value = Value::Empty(DataType::Int32);
// Through type inference
value.set(42i32).unwrap();
assert_eq!(value.get_int32().unwrap(), 42);
// Explicitly specify type parameter
value.set::<i32>(100).unwrap();
assert_eq!(value.get_int32().unwrap(), 100);
// String type
let mut text = Value::Empty(DataType::String);
text.set("hello".to_string()).unwrap();
assert_eq!(text.get_string().unwrap(), "hello");
Sourcepub fn data_type(&self) -> DataType
pub fn data_type(&self) -> DataType
Get the data type of the value
§Returns
Returns the data type corresponding to this value
§Example
use crate::util::value::{Value, DataType};
let value = Value::Int32(42);
assert_eq!(value.data_type(), DataType::Int32);
let empty = Value::Empty(DataType::String);
assert_eq!(empty.data_type(), DataType::String);
Sourcepub fn set_type(&mut self, data_type: DataType)
pub fn set_type(&mut self, data_type: DataType)
Set the data type
If the new type differs from the current type, clears the value and sets the new type.
§Parameters
data_type
- The data type to set
§Example
use crate::util::value::{Value, DataType};
let mut value = Value::Int32(42);
value.set_type(DataType::String);
assert!(value.is_empty());
assert_eq!(value.data_type(), DataType::String);
Sourcepub fn get_bool(&self) -> ValueResult<bool>
pub fn get_bool(&self) -> ValueResult<bool>
Sourcepub fn get_char(&self) -> ValueResult<char>
pub fn get_char(&self) -> ValueResult<char>
Sourcepub fn get_int8(&self) -> ValueResult<i8>
pub fn get_int8(&self) -> ValueResult<i8>
Sourcepub fn get_int16(&self) -> ValueResult<i16>
pub fn get_int16(&self) -> ValueResult<i16>
Sourcepub fn get_int32(&self) -> ValueResult<i32>
pub fn get_int32(&self) -> ValueResult<i32>
Sourcepub fn get_int64(&self) -> ValueResult<i64>
pub fn get_int64(&self) -> ValueResult<i64>
Sourcepub fn get_int128(&self) -> ValueResult<i128>
pub fn get_int128(&self) -> ValueResult<i128>
Sourcepub fn get_uint8(&self) -> ValueResult<u8>
pub fn get_uint8(&self) -> ValueResult<u8>
Sourcepub fn get_uint16(&self) -> ValueResult<u16>
pub fn get_uint16(&self) -> ValueResult<u16>
Sourcepub fn get_uint32(&self) -> ValueResult<u32>
pub fn get_uint32(&self) -> ValueResult<u32>
Sourcepub fn get_uint64(&self) -> ValueResult<u64>
pub fn get_uint64(&self) -> ValueResult<u64>
Sourcepub fn get_uint128(&self) -> ValueResult<u128>
pub fn get_uint128(&self) -> ValueResult<u128>
Sourcepub fn get_float32(&self) -> ValueResult<f32>
pub fn get_float32(&self) -> ValueResult<f32>
Sourcepub fn get_float64(&self) -> ValueResult<f64>
pub fn get_float64(&self) -> ValueResult<f64>
Sourcepub fn get_string(&self) -> ValueResult<&str>
pub fn get_string(&self) -> ValueResult<&str>
Sourcepub fn get_date(&self) -> ValueResult<NaiveDate>
pub fn get_date(&self) -> ValueResult<NaiveDate>
Sourcepub fn get_time(&self) -> ValueResult<NaiveTime>
pub fn get_time(&self) -> ValueResult<NaiveTime>
Sourcepub fn get_datetime(&self) -> ValueResult<NaiveDateTime>
pub fn get_datetime(&self) -> ValueResult<NaiveDateTime>
Sourcepub fn get_instant(&self) -> ValueResult<DateTime<Utc>>
pub fn get_instant(&self) -> ValueResult<DateTime<Utc>>
Get UTC instant value
§Returns
If types match, returns the UTC instant value; otherwise returns an error
Sourcepub fn get_biginteger(&self) -> ValueResult<BigInt>
pub fn get_biginteger(&self) -> ValueResult<BigInt>
Sourcepub fn get_bigdecimal(&self) -> ValueResult<BigDecimal>
pub fn get_bigdecimal(&self) -> ValueResult<BigDecimal>
Get big decimal value
§Returns
If types match, returns the big decimal value; otherwise returns an error
§Example
use crate::util::value::Value;
use bigdecimal::BigDecimal;
let value = Value::BigDecimal(BigDecimal::from(123.456));
assert_eq!(value.get_bigdecimal().unwrap(), BigDecimal::from(123.456));
Sourcepub fn as_bool(&self) -> ValueResult<bool>
pub fn as_bool(&self) -> ValueResult<bool>
Convert to boolean value
§Returns
Attempts to convert the value to a boolean, supporting conversion from various types
§Example
use crate::util::value::Value;
let value = Value::Int32(1);
assert_eq!(value.as_bool().unwrap(), true);
let value = Value::String("true".to_string());
assert_eq!(value.as_bool().unwrap(), true);
Sourcepub fn as_int32(&self) -> ValueResult<i32>
pub fn as_int32(&self) -> ValueResult<i32>
Convert to int32 value
§Returns
Attempts to convert the value to i32, supporting conversion from various types
§Example
use crate::util::value::Value;
let value = Value::Int8(42);
assert_eq!(value.as_int32().unwrap(), 42);
let value = Value::String("123".to_string());
assert_eq!(value.as_int32().unwrap(), 123);
Sourcepub fn as_int64(&self) -> ValueResult<i64>
pub fn as_int64(&self) -> ValueResult<i64>
Convert to int64 value
§Returns
Attempts to convert the value to i64, supporting conversion from various types
§Example
use crate::util::value::Value;
let value = Value::Int32(42);
assert_eq!(value.as_int64().unwrap(), 42i64);
let value = Value::Float64(123.456);
assert_eq!(value.as_int64().unwrap(), 123i64);
Sourcepub fn as_float64(&self) -> ValueResult<f64>
pub fn as_float64(&self) -> ValueResult<f64>
Convert to float64 value
§Returns
Attempts to convert the value to f64, supporting conversion from various types
§Example
use crate::util::value::Value;
let value = Value::Int32(42);
assert_eq!(value.as_float64().unwrap(), 42.0);
let value = Value::Bool(true);
assert_eq!(value.as_float64().unwrap(), 1.0);
Sourcepub fn as_string(&self) -> ValueResult<String>
pub fn as_string(&self) -> ValueResult<String>
Sourcepub fn set_bool(&mut self, value: bool) -> ValueResult<()>
pub fn set_bool(&mut self, value: bool) -> ValueResult<()>
Sourcepub fn set_char(&mut self, value: char) -> ValueResult<()>
pub fn set_char(&mut self, value: char) -> ValueResult<()>
Sourcepub fn set_int8(&mut self, value: i8) -> ValueResult<()>
pub fn set_int8(&mut self, value: i8) -> ValueResult<()>
Sourcepub fn set_int16(&mut self, value: i16) -> ValueResult<()>
pub fn set_int16(&mut self, value: i16) -> ValueResult<()>
Sourcepub fn set_int32(&mut self, value: i32) -> ValueResult<()>
pub fn set_int32(&mut self, value: i32) -> ValueResult<()>
Sourcepub fn set_int64(&mut self, value: i64) -> ValueResult<()>
pub fn set_int64(&mut self, value: i64) -> ValueResult<()>
Sourcepub fn set_int128(&mut self, value: i128) -> ValueResult<()>
pub fn set_int128(&mut self, value: i128) -> ValueResult<()>
Sourcepub fn set_uint8(&mut self, value: u8) -> ValueResult<()>
pub fn set_uint8(&mut self, value: u8) -> ValueResult<()>
Sourcepub fn set_uint16(&mut self, value: u16) -> ValueResult<()>
pub fn set_uint16(&mut self, value: u16) -> ValueResult<()>
Sourcepub fn set_uint32(&mut self, value: u32) -> ValueResult<()>
pub fn set_uint32(&mut self, value: u32) -> ValueResult<()>
Sourcepub fn set_uint64(&mut self, value: u64) -> ValueResult<()>
pub fn set_uint64(&mut self, value: u64) -> ValueResult<()>
Sourcepub fn set_uint128(&mut self, value: u128) -> ValueResult<()>
pub fn set_uint128(&mut self, value: u128) -> ValueResult<()>
Sourcepub fn set_float32(&mut self, value: f32) -> ValueResult<()>
pub fn set_float32(&mut self, value: f32) -> ValueResult<()>
Sourcepub fn set_float64(&mut self, value: f64) -> ValueResult<()>
pub fn set_float64(&mut self, value: f64) -> ValueResult<()>
Sourcepub fn set_string(&mut self, value: String) -> ValueResult<()>
pub fn set_string(&mut self, value: String) -> ValueResult<()>
Set string value
§Parameters
value
- The string value to set
§Returns
If setting succeeds, returns Ok(())
; otherwise returns an error
§Example
use crate::util::value::Value;
let mut value = Value::Empty(DataType::String);
value.set_string("hello".to_string()).unwrap();
assert_eq!(value.get_string().unwrap(), "hello");
Sourcepub fn set_date(&mut self, value: NaiveDate) -> ValueResult<()>
pub fn set_date(&mut self, value: NaiveDate) -> ValueResult<()>
Sourcepub fn set_time(&mut self, value: NaiveTime) -> ValueResult<()>
pub fn set_time(&mut self, value: NaiveTime) -> ValueResult<()>
Sourcepub fn set_datetime(&mut self, value: NaiveDateTime) -> ValueResult<()>
pub fn set_datetime(&mut self, value: NaiveDateTime) -> ValueResult<()>
Sourcepub fn set_instant(&mut self, value: DateTime<Utc>) -> ValueResult<()>
pub fn set_instant(&mut self, value: DateTime<Utc>) -> ValueResult<()>
Sourcepub fn set_biginteger(&mut self, value: BigInt) -> ValueResult<()>
pub fn set_biginteger(&mut self, value: BigInt) -> ValueResult<()>
Set big integer value
§Parameters
value
- The big integer value to set
§Returns
If setting succeeds, returns Ok(())
; otherwise returns an error
§Example
use crate::util::value::Value;
use num_bigint::BigInt;
let mut value = Value::Empty(DataType::BigInteger);
value.set_biginteger(BigInt::from(123456789)).unwrap();
assert_eq!(value.get_biginteger().unwrap(), BigInt::from(123456789));
Sourcepub fn set_bigdecimal(&mut self, value: BigDecimal) -> ValueResult<()>
pub fn set_bigdecimal(&mut self, value: BigDecimal) -> ValueResult<()>
Set big decimal value
§Parameters
value
- The big decimal value to set
§Returns
If setting succeeds, returns Ok(())
; otherwise returns an error
§Example
use crate::util::value::Value;
use bigdecimal::BigDecimal;
let mut value = Value::Empty(DataType::BigDecimal);
value.set_bigdecimal(BigDecimal::from(123.456)).unwrap();
assert_eq!(value.get_bigdecimal().unwrap(), BigDecimal::from(123.456));