ToFieldValue

Trait ToFieldValue 

Source
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 a FieldValue for 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§

Source

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

String references are converted to owned strings for serialization, ensuring data ownership and thread safety.

Source§

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.

Source§

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.

Source§

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.

Source§

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.

Source§

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.

Source§

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.

Source§

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.

Source§

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.

Source§

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.

Source§

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.

Source§

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.

Source§

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.

Source§

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.

Source§

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.

Source§

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.

Source§

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.

Source§

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.

Source§

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.

Source§

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.

Source§

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.

Source§

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.

Source§

impl ToFieldValue for HashMap<usize, SerializableParameterState>

Source§

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

String hash maps are serialized as JSON objects, maintaining key-value relationships for configuration data, metadata, and structured text storage.

Source§

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.

Source§

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.

Source§

impl<T> ToFieldValue for Vec<T>

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.

Source§

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.

Source§

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.

Implementors§

Source§

impl ToFieldValue for DeviceType

Source§

impl ToFieldValue for AdamConfig

Source§

impl ToFieldValue for Device

Source§

impl ToFieldValue for Shape

Source§

impl<T> ToFieldValue for T
where T: Serializable,

Converts serializable objects to FieldValue representation

Serializable objects are converted using their JSON representation, providing format-aware serialization for complex data structures.