Skip to main content

to_value

Function to_value 

Source
pub fn to_value<T>(value: &T) -> Result<Value>
where T: ?Sized + Serialize,
Expand description

Convert any serializable type into a Value.

This is the inverse of from_value. The same representation rules as text serialization apply: structs and maps become groups, sequences become arrays (homogeneous scalars) or lists, enum unit variants become strings, and data-carrying variants become single-setting groups.

§Errors

Returns an error for values with no libconfig representation: bare None, unit, u64 beyond i64::MAX, or invalid setting names. Non-finite floats are representable in a Value and only rejected when written out as text.

§Examples

use libconfig::{to_value, Value};

#[derive(serde::Serialize)]
struct S { x: i32 }

let v = to_value(&S { x: 7 }).unwrap();
assert_eq!(v["x"], 7);