pub trait ToFieldValue {
// Required method
fn to_field_value(&self) -> FieldValue;
}Expand description
Trait for converting values to FieldValue for serialization
This trait provides the foundation for automatic type conversion during serialization.
It allows the StructSerializer to handle different types generically without
requiring specialized methods for each type. The conversion should be lossless
and reversible to ensure data integrity.
§Purpose
The ToFieldValue trait enables:
- Generic serialization: Single interface for all serializable types
- Type safety: Compile-time guarantees for conversion correctness
- Automatic detection: No manual type specification required
- Extensibility: Easy to implement for custom types
- Performance: Zero-cost abstractions for type conversion
§Required Methods
to_field_value- Convert the value to aFieldValuefor serialization
§Examples
The trait enables automatic type conversion for serialization with compile-time safety.
§Implementors
Common types that implement this trait include:
- Primitive types:
bool,i8,i16,i32,i64,u8,u16,u32,u64,usize,f32,f64 - String types:
String,&str - Collections:
Vec<T>,HashMap<String, String>,Option<T> - Custom types: Any type implementing
ToFieldValue
§Thread Safety
Implementations should be thread-safe and not modify any shared state during conversion.
Required Methods§
Sourcefn to_field_value(&self) -> FieldValue
fn to_field_value(&self) -> FieldValue
Converts the value to a FieldValue for serialization
This method should convert the implementing type into the appropriate
FieldValue variant. The conversion should be lossless and reversible
to ensure data integrity during serialization and deserialization cycles.
§Returns
A FieldValue representing the serialized form of the value
§Examples
Converts various types to their serializable representation with automatic type detection.
Implementations on Foreign Types§
Source§impl ToFieldValue for &str
Converts string references to FieldValue representation
impl ToFieldValue for &str
Converts string references to FieldValue representation
String references are converted to owned strings for serialization, ensuring data ownership and thread safety.
fn to_field_value(&self) -> FieldValue
Source§impl ToFieldValue for bool
Converts boolean values to FieldValue representation
impl ToFieldValue for bool
Converts boolean values to FieldValue representation
Boolean values are stored as native boolean types in both JSON and binary formats, maintaining their logical meaning across serialization boundaries.
fn to_field_value(&self) -> FieldValue
Source§impl ToFieldValue for f32
Converts 32-bit floating point numbers to FieldValue representation
impl ToFieldValue for f32
Converts 32-bit floating point numbers to FieldValue representation
Single-precision floating point for values requiring decimal precision with moderate storage requirements.
fn to_field_value(&self) -> FieldValue
Source§impl ToFieldValue for f64
Converts 64-bit floating point numbers to FieldValue representation
impl ToFieldValue for f64
Converts 64-bit floating point numbers to FieldValue representation
Double-precision floating point for values requiring high decimal precision, commonly used for scientific calculations and financial data.
fn to_field_value(&self) -> FieldValue
Source§impl ToFieldValue for i8
Converts 8-bit signed integers to FieldValue representation
impl ToFieldValue for i8
Converts 8-bit signed integers to FieldValue representation
Small integers are stored efficiently in both JSON and binary formats, with proper range validation during deserialization.
fn to_field_value(&self) -> FieldValue
Source§impl ToFieldValue for i16
Converts 16-bit signed integers to FieldValue representation
impl ToFieldValue for i16
Converts 16-bit signed integers to FieldValue representation
Medium-sized integers provide a balance between range and storage efficiency for most common use cases.
fn to_field_value(&self) -> FieldValue
Source§impl ToFieldValue for i32
Converts 32-bit signed integers to FieldValue representation
impl ToFieldValue for i32
Converts 32-bit signed integers to FieldValue representation
Standard integer type used for most numeric data, providing sufficient range for typical application needs.
fn to_field_value(&self) -> FieldValue
Source§impl ToFieldValue for i64
Converts 64-bit signed integers to FieldValue representation
impl ToFieldValue for i64
Converts 64-bit signed integers to FieldValue representation
Large integer type for values requiring extended range, such as timestamps, file sizes, or large counts.
fn to_field_value(&self) -> FieldValue
Source§impl ToFieldValue for str
Converts string slices to FieldValue representation
impl ToFieldValue for str
Converts string slices to FieldValue representation
String slices are converted to owned strings for serialization, maintaining UTF-8 encoding and preserving all character data.
fn to_field_value(&self) -> FieldValue
Source§impl ToFieldValue for u8
Converts 8-bit unsigned integers to FieldValue representation
impl ToFieldValue for u8
Converts 8-bit unsigned integers to FieldValue representation
Small unsigned integers for values that are always non-negative, such as array indices or small counts.
fn to_field_value(&self) -> FieldValue
Source§impl ToFieldValue for u16
Converts 16-bit unsigned integers to FieldValue representation
impl ToFieldValue for u16
Converts 16-bit unsigned integers to FieldValue representation
Medium-sized unsigned integers for values requiring positive range with moderate storage efficiency.
fn to_field_value(&self) -> FieldValue
Source§impl ToFieldValue for u32
Converts 32-bit unsigned integers to FieldValue representation
impl ToFieldValue for u32
Converts 32-bit unsigned integers to FieldValue representation
Standard unsigned integer type for large positive values, commonly used for sizes, counts, and identifiers.
fn to_field_value(&self) -> FieldValue
Source§impl ToFieldValue for u64
Converts 64-bit unsigned integers to FieldValue representation
impl ToFieldValue for u64
Converts 64-bit unsigned integers to FieldValue representation
Large unsigned integer type for very large positive values, such as memory addresses or large file sizes.
fn to_field_value(&self) -> FieldValue
Source§impl ToFieldValue for usize
Converts platform-specific size integers to FieldValue representation
impl ToFieldValue for usize
Converts platform-specific size integers to FieldValue representation
Size type that adapts to the platform’s pointer size, commonly used for array lengths, memory sizes, and indexing operations.
fn to_field_value(&self) -> FieldValue
Source§impl ToFieldValue for String
Converts owned strings to FieldValue representation
impl ToFieldValue for String
Converts owned strings to FieldValue representation
Owned strings are cloned for serialization to maintain data ownership while preserving the original string for further use.
fn to_field_value(&self) -> FieldValue
Source§impl ToFieldValue for Vec<bool>
Converts boolean vectors to FieldValue representation
impl ToFieldValue for Vec<bool>
Converts boolean vectors to FieldValue representation
Boolean vectors are serialized as JSON-compatible arrays, maintaining logical relationships and providing clear true/false representation.
fn to_field_value(&self) -> FieldValue
Source§impl ToFieldValue for Vec<f32>
Converts single-precision float vectors to FieldValue representation
impl ToFieldValue for Vec<f32>
Converts single-precision float vectors to FieldValue representation
Float vectors are serialized as JSON-compatible arrays to maintain human readability while preserving numerical precision for scientific and engineering applications.
fn to_field_value(&self) -> FieldValue
Source§impl ToFieldValue for Vec<f64>
Converts double-precision float vectors to FieldValue representation
impl ToFieldValue for Vec<f64>
Converts double-precision float vectors to FieldValue representation
Double-precision float vectors maintain high numerical accuracy for scientific calculations while providing JSON-compatible serialization for data exchange.
fn to_field_value(&self) -> FieldValue
Source§impl ToFieldValue for Vec<i32>
Converts integer vectors to FieldValue representation
impl ToFieldValue for Vec<i32>
Converts integer vectors to FieldValue representation
Integer vectors are serialized as JSON-compatible arrays for human readability, while maintaining efficient binary storage for performance-critical applications.
fn to_field_value(&self) -> FieldValue
Source§impl ToFieldValue for Vec<u8>
Converts byte vectors to FieldValue representation
impl ToFieldValue for Vec<u8>
Converts byte vectors to FieldValue representation
Byte vectors are cloned for serialization to maintain data ownership while preserving the original vector for further use.
fn to_field_value(&self) -> FieldValue
Source§impl ToFieldValue for Vec<usize>
Converts size vectors to FieldValue representation
impl ToFieldValue for Vec<usize>
Converts size vectors to FieldValue representation
Size vectors are serialized as JSON-compatible arrays, adapting to the platform’s pointer size while maintaining cross-platform compatibility.
fn to_field_value(&self) -> FieldValue
Source§impl ToFieldValue for Vec<String>
Converts string vectors to FieldValue representation
impl ToFieldValue for Vec<String>
Converts string vectors to FieldValue representation
String vectors are serialized as JSON-compatible arrays, preserving UTF-8 encoding and maintaining string relationships for text processing applications.
fn to_field_value(&self) -> FieldValue
Source§impl ToFieldValue for HashMap<usize, SerializableParameterState>
impl ToFieldValue for HashMap<usize, SerializableParameterState>
Source§fn to_field_value(&self) -> FieldValue
fn to_field_value(&self) -> FieldValue
Convert parameter states HashMap to FieldValue for serialization
This method converts the HashMap of parameter states into a FieldValue::Object suitable for serialization. It handles the conversion of usize keys to string format required by the FieldValue::Object representation while preserving all parameter state data.
§Returns
FieldValue::Object with string keys and SerializableParameterState values
§Key Conversion
- Input: HashMap<usize, SerializableParameterState> with numeric tensor IDs
- Output: FieldValue::Object with string keys for JSON compatibility
- Mapping: Each usize key is converted to string representation
- Preservation: All parameter state data is preserved exactly
§Performance
- Time Complexity: O(n) where n is the number of parameter states
- Memory Usage: Allocates new HashMap for string keys
- Conversion: Efficient string conversion for numeric keys
Source§impl ToFieldValue for HashMap<String, String>
Converts string hash maps to FieldValue representation
impl ToFieldValue for HashMap<String, String>
Converts string hash maps to FieldValue representation
String hash maps are serialized as JSON objects, maintaining key-value relationships for configuration data, metadata, and structured text storage.
fn to_field_value(&self) -> FieldValue
Source§impl ToFieldValue for [u8]
Converts byte slices to FieldValue representation
impl ToFieldValue for [u8]
Converts byte slices to FieldValue representation
Byte slices are converted to owned byte vectors for serialization, preserving raw binary data without encoding assumptions.
fn to_field_value(&self) -> FieldValue
Source§impl<T> ToFieldValue for Option<T>where
T: ToFieldValue,
Converts optional values to FieldValue representation
impl<T> ToFieldValue for Option<T>where
T: ToFieldValue,
Converts optional values to FieldValue representation
Optional values are serialized with explicit None/Some representation, maintaining the optional nature of the data across serialization boundaries.
fn to_field_value(&self) -> FieldValue
Source§impl<T> ToFieldValue for Vec<T>where
T: StructSerializable,
Converts serializable struct vectors to FieldValue representation
impl<T> ToFieldValue for Vec<T>where
T: StructSerializable,
Converts serializable struct vectors to FieldValue representation
Struct vectors are serialized as arrays of objects, maintaining the structure and relationships of complex data while providing JSON-compatible format.
fn to_field_value(&self) -> FieldValue
Source§impl<const N: usize> ToFieldValue for [i32; N]
Converts fixed-size integer arrays to FieldValue representation
impl<const N: usize> ToFieldValue for [i32; N]
Converts fixed-size integer arrays to FieldValue representation
Fixed-size integer arrays are serialized as JSON-compatible arrays, maintaining the original structure while providing human-readable format.
fn to_field_value(&self) -> FieldValue
Source§impl<const N: usize> ToFieldValue for [u8; N]
Converts fixed-size byte arrays to FieldValue representation
impl<const N: usize> ToFieldValue for [u8; N]
Converts fixed-size byte arrays to FieldValue representation
Fixed-size byte arrays are converted to vectors for serialization, maintaining the original data while providing flexible storage.
fn to_field_value(&self) -> FieldValue
Implementors§
impl ToFieldValue for DeviceType
impl ToFieldValue for AdamConfig
impl ToFieldValue for Device
impl ToFieldValue for Shape
impl<T> ToFieldValue for Twhere
T: Serializable,
Converts serializable objects to FieldValue representation
Serializable objects are converted using their JSON representation, providing format-aware serialization for complex data structures.