Skip to main content

FromValue

Trait FromValue 

Source
pub trait FromValue: Sized {
    // Required method
    fn from_value(
        value: &Value,
        spanner_type: &Type,
    ) -> Result<Self, ConvertError>;

    // Provided method
    fn from_owned_value(
        value: Value,
        spanner_type: &Type,
    ) -> Result<Self, ConvertError> { ... }
}
Expand description

Converts a Spanner Value into a Rust type.

Implementations are provided for all standard types like String, primitive integer and float types, decimals, timestamps, dates, vectors, and options for nullable fields.

§Null Handling Contract

Custom implementations of FromValue must return ConvertError::NotNull when given a null value (Value::is_null). This allows Option<T> to intercept the null value and return Ok(None) while still validating the column’s schema Type.

§Example: Custom converter for domain wrapper

An application can implement FromValue for custom domain or newtype wrappers:

use google_cloud_spanner::types::Type;
use google_cloud_spanner::value::{ConvertError, FromValue, Value};

#[derive(Debug, PartialEq)]
struct AccountNumber(pub String);

impl FromValue for AccountNumber {
    fn from_value(value: &Value, spanner_type: &Type) -> Result<Self, ConvertError> {
        let s = String::from_value(value, spanner_type)?;
        Ok(AccountNumber(s))
    }
}

let spanner_type = google_cloud_spanner::types::string();
let val = Value::from("ACC-98765");
let account = AccountNumber::from_value(&val, &spanner_type)?;
assert_eq!(account.0, "ACC-98765");

Required Methods§

Source

fn from_value(value: &Value, spanner_type: &Type) -> Result<Self, ConvertError>

Converts a Spanner value into the target Rust type, using the provided Spanner Type metadata for compatibility checks.

§Errors

Returns a ConvertError if the kind of the value does not match the expected kind, if the value is null but the target type is not optional (e.g., Option<T>), or if parsing or decoding the inner value format fails.

Provided Methods§

Source

fn from_owned_value( value: Value, spanner_type: &Type, ) -> Result<Self, ConvertError>

Converts an owned Spanner value into the target Rust type, using the provided Spanner Type metadata for compatibility checks.

§Example
let value = Value::from("example");
let string = String::from_owned_value(value, &types::string())?;
assert_eq!(string, "example");

The default implementation delegates to from_value using a reference to value. Types that contain heap allocations (such as String, Vec<T>, serde_json::Value, and Value) override this method to move the underlying data directly without cloning.

§Errors

Returns a ConvertError if the kind of the value does not match the expected kind, if the value is null but the target type is not optional (e.g., Option<T>), or if parsing or decoding the inner value format fails.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl FromValue for Bytes

Deserializes a byte buffer from a Spanner Value.

Accepts Spanner TypeCode::Bytes and TypeCode::Proto. Values are decoded from the wire base64-encoded string representation.

Source§

fn from_value(value: &Value, spanner_type: &Type) -> Result<Self, ConvertError>

Source§

fn from_owned_value( value: Value, spanner_type: &Type, ) -> Result<Self, ConvertError>

Source§

impl FromValue for Date

Available on crate feature unstable-time only.
Source§

fn from_value(value: &Value, spanner_type: &Type) -> Result<Self, ConvertError>

Source§

impl FromValue for OffsetDateTime

Available on crate feature unstable-time only.
Source§

fn from_value(value: &Value, spanner_type: &Type) -> Result<Self, ConvertError>

Source§

impl FromValue for String

Deserializes a string value from a Spanner Value.

Supported Spanner types are:

Non-string types (such as INT64, BYTES, FLOAT64, FLOAT32, BOOL, ARRAY, and STRUCT) are rejected with ConvertError::TypeMismatch.

Source§

fn from_value(value: &Value, spanner_type: &Type) -> Result<Self, ConvertError>

Source§

fn from_owned_value( value: Value, spanner_type: &Type, ) -> Result<Self, ConvertError>

Source§

impl FromValue for SystemTime

Source§

fn from_value(value: &Value, spanner_type: &Type) -> Result<Self, ConvertError>

Source§

impl FromValue for Uuid

Deserializes a UUID from a Spanner Value.

Accepts Spanner TypeCode::Uuid and TypeCode::String. Values are parsed from the wire 8-4-4-4-12 hex string representation.

Source§

fn from_value(value: &Value, spanner_type: &Type) -> Result<Self, ConvertError>

Source§

impl FromValue for Value

Converts any Spanner Value into a serde_json::Value using runtime Type metadata.

This enables dynamic deserialization of STRUCT, ARRAY, and nested ARRAY<STRUCT<ARRAY<...>>> columns without requiring predefined Rust types.

§Type mapping

Spanner wire formatJSON output
NullValuenull
BoolValuetrue / false
NumberValue (finite)JSON number
NumberValue (NaN/±Infinity)null
StringValue (“NaN”/±Infinity on FLOAT columns)null
StringValue (standard)JSON string (includes stringified INT64, NUMERIC, Base64 BYTES, TIMESTAMP, DATE)
StringValue (+ TypeCode::Json)Parsed JSON object/array/value
ListValue + TypeCode::StructJSON object (positional → named via metadata)
ListValue + TypeCode::ArrayJSON array

§Known limitations

  • Precision Safety: Types such as INT64, NUMERIC, and temporal types (which Spanner transmits as StringValue on the wire) are preserved as JSON strings. This prevents precision loss (e.g. for integers exceeding $2^{53}-1$ or high-precision decimals) during deserialization.
  • NaN/Infinity → null: Non-finite floats become null when deserialized as serde_json::Value since standard JSON has no representation for NaN/Infinity. Callers cannot distinguish these from genuine SQL NULLs.
  • Duplicate field names: Spanner allows structs with duplicate field names (e.g., unnamed columns). Since JSON objects require unique keys, last-write-wins applies via serde_json::Map::insert.
  • Missing struct metadata: When TypeCode::Struct is indicated but no struct_type metadata is available, positional values are returned as a plain JSON array to avoid silent data loss.
Source§

fn from_value(value: &Value, spanner_type: &Type) -> Result<Self, ConvertError>

Source§

fn from_owned_value( value: Value, spanner_type: &Type, ) -> Result<Self, ConvertError>

Source§

impl FromValue for Vec<u8>

Source§

fn from_value(value: &Value, spanner_type: &Type) -> Result<Self, ConvertError>

Source§

fn from_owned_value( value: Value, spanner_type: &Type, ) -> Result<Self, ConvertError>

Source§

impl FromValue for bool

Source§

fn from_value(value: &Value, spanner_type: &Type) -> Result<Self, ConvertError>

Source§

impl FromValue for f32

Deserializes a 32-bit floating-point value from a Spanner Value.

Accepts Spanner TypeCode::Float32 and TypeCode::Float64.

§Precision & Range Limitations

When deserializing from a Spanner TypeCode::Float64 column, 64-bit IEEE 754 floating-point numbers are narrowed to 32-bit f32.

If a finite float value exceeds the representable range of f32 (greater than f32::MAX or less than f32::MIN / -f32::MAX), this conversion returns a ConvertError::Convert. Non-finite values (NaN, Infinity, +Infinity, and -Infinity) are preserved.

Source§

fn from_value(value: &Value, spanner_type: &Type) -> Result<Self, ConvertError>

Source§

impl FromValue for f64

Deserializes a 64-bit floating-point value from a Spanner Value.

Accepts Spanner TypeCode::Float64 and TypeCode::Float32. Values from TypeCode::Float32 columns are widened to f64 losslessly.

Source§

fn from_value(value: &Value, spanner_type: &Type) -> Result<Self, ConvertError>

Source§

impl FromValue for i32

Deserializes a 32-bit integer from a Spanner Value.

Accepts Spanner TypeCode::Int64 and TypeCode::Enum.

Source§

fn from_value(value: &Value, spanner_type: &Type) -> Result<Self, ConvertError>

Source§

impl FromValue for i64

Deserializes a 64-bit integer from a Spanner Value.

Accepts Spanner TypeCode::Int64 and TypeCode::Enum.

Source§

fn from_value(value: &Value, spanner_type: &Type) -> Result<Self, ConvertError>

Source§

impl<T> FromValue for Option<T>
where T: FromValue,

Source§

fn from_value(value: &Value, spanner_type: &Type) -> Result<Self, ConvertError>

Source§

fn from_owned_value( value: Value, spanner_type: &Type, ) -> Result<Self, ConvertError>

Source§

impl<T> FromValue for Vec<T>
where T: FromValue,

Source§

fn from_value(value: &Value, spanner_type: &Type) -> Result<Self, ConvertError>

Source§

fn from_owned_value( value: Value, spanner_type: &Type, ) -> Result<Self, ConvertError>

Implementors§

Source§

impl FromValue for google_cloud_spanner::value::Date

Source§

impl FromValue for Decimal

Converts a Spanner TypeCode::Numeric column into a Decimal.

Supports both standard fixed-point decimal notation (e.g., "123.456") and scientific notation format (e.g., "1e5", "1.5e-3").

§Precision Limitations

Cloud Spanner NUMERIC supports up to 38 decimal digits of precision (up to 29 digits before the decimal point and 9 digits after). However, Decimal uses a 96-bit unsigned integer mantissa, which can represent at most ~28–29 total decimal digits (and at most 19–20 digits before the decimal point when scale is 9).

If a Spanner numeric value exceeds the 96-bit limit of Decimal, or if a PostgreSQL dialect database returns "NaN", converting to Decimal will return a ConvertError::Convert. For values that exceed 96 bits or for PostgreSQL "NaN", callers should read the column as a String instead (e.g., let value: String = row.get(...)?;).

Source§

impl FromValue for Timestamp

Source§

impl FromValue for google_cloud_spanner::value::Value