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§
Sourcefn from_value(value: &Value, spanner_type: &Type) -> Result<Self, ConvertError>
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§
Sourcefn from_owned_value(
value: Value,
spanner_type: &Type,
) -> Result<Self, ConvertError>
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.
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.
fn from_value(value: &Value, spanner_type: &Type) -> Result<Self, ConvertError>
fn from_owned_value( value: Value, spanner_type: &Type, ) -> Result<Self, ConvertError>
Source§impl FromValue for Date
Available on crate feature unstable-time only.
impl FromValue for Date
unstable-time only.fn from_value(value: &Value, spanner_type: &Type) -> Result<Self, ConvertError>
Source§impl FromValue for OffsetDateTime
Available on crate feature unstable-time only.
impl FromValue for OffsetDateTime
unstable-time only.fn from_value(value: &Value, spanner_type: &Type) -> Result<Self, ConvertError>
Source§impl FromValue for String
Deserializes a string value from a Spanner Value.
impl FromValue for String
Deserializes a string value from a Spanner Value.
Supported Spanner types are:
- Textual types:
TypeCode::String,TypeCode::Json,TypeCode::Uuid, andTypeCode::Interval. - Temporal types:
TypeCode::Date(formatted asYYYY-MM-DD) andTypeCode::Timestamp(formatted as RFC 3339). - Exact decimal types:
TypeCode::Numeric. RetrievingNUMERICasStringpreserves full 38-digit precision and handles special PostgreSQL values likeNaNwithout loss or parsing errors.
Non-string types (such as INT64, BYTES, FLOAT64, FLOAT32, BOOL, ARRAY, and STRUCT)
are rejected with ConvertError::TypeMismatch.
fn from_value(value: &Value, spanner_type: &Type) -> Result<Self, ConvertError>
fn from_owned_value( value: Value, spanner_type: &Type, ) -> Result<Self, ConvertError>
Source§impl FromValue for SystemTime
impl FromValue for SystemTime
fn from_value(value: &Value, spanner_type: &Type) -> Result<Self, ConvertError>
Source§impl FromValue for Uuid
Deserializes a UUID from a Spanner Value.
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.
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.
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 format | JSON output |
|---|---|
NullValue | null |
BoolValue | true / 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::Struct | JSON object (positional → named via metadata) |
ListValue + TypeCode::Array | JSON array |
§Known limitations
- Precision Safety: Types such as
INT64,NUMERIC, and temporal types (which Spanner transmits asStringValueon 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
nullwhen deserialized asserde_json::Valuesince 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::Structis indicated but nostruct_typemetadata is available, positional values are returned as a plain JSON array to avoid silent data loss.
fn from_value(value: &Value, spanner_type: &Type) -> Result<Self, ConvertError>
fn from_owned_value( value: Value, spanner_type: &Type, ) -> Result<Self, ConvertError>
Source§impl FromValue for Vec<u8>
impl FromValue for Vec<u8>
fn from_value(value: &Value, spanner_type: &Type) -> Result<Self, ConvertError>
fn from_owned_value( value: Value, spanner_type: &Type, ) -> Result<Self, ConvertError>
Source§impl FromValue for bool
impl FromValue for bool
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.
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.
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.
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.
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.
impl FromValue for i32
Deserializes a 32-bit integer from a Spanner Value.
Accepts Spanner TypeCode::Int64 and TypeCode::Enum.
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.
impl FromValue for i64
Deserializes a 64-bit integer from a Spanner Value.
Accepts Spanner TypeCode::Int64 and TypeCode::Enum.
fn from_value(value: &Value, spanner_type: &Type) -> Result<Self, ConvertError>
Source§impl<T> FromValue for Option<T>where
T: FromValue,
impl<T> FromValue for Option<T>where
T: FromValue,
fn from_value(value: &Value, spanner_type: &Type) -> Result<Self, ConvertError>
fn from_owned_value( value: Value, spanner_type: &Type, ) -> Result<Self, ConvertError>
Source§impl<T> FromValue for Vec<T>where
T: FromValue,
impl<T> FromValue for Vec<T>where
T: FromValue,
fn from_value(value: &Value, spanner_type: &Type) -> Result<Self, ConvertError>
fn from_owned_value( value: Value, spanner_type: &Type, ) -> Result<Self, ConvertError>
Implementors§
impl FromValue for google_cloud_spanner::value::Date
Source§impl FromValue for Decimal
Converts a Spanner TypeCode::Numeric column into a Decimal.
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(...)?;).