28 releases (17 stable)

1.10.0 Oct 23, 2024
1.9.0 May 1, 2024
1.8.1 Mar 19, 2024
1.4.2 Oct 19, 2023
0.1.0 Sep 25, 2020

#81 in Encoding

Download history 369735/week @ 2024-07-31 410846/week @ 2024-08-07 399807/week @ 2024-08-14 420882/week @ 2024-08-21 423034/week @ 2024-08-28 499460/week @ 2024-09-04 473870/week @ 2024-09-11 468107/week @ 2024-09-18 506166/week @ 2024-09-25 537964/week @ 2024-10-02 544544/week @ 2024-10-09 531559/week @ 2024-10-16 547051/week @ 2024-10-23 497292/week @ 2024-10-30 530655/week @ 2024-11-06 529794/week @ 2024-11-13

2,210,690 downloads per month
Used in 2,941 crates (6 directly)

Apache-2.0 OR MIT

240KB
5.5K SLoC

value-bag

Rust Latest version Documentation Latest

What is a value bag?

A ValueBag is an anonymous structured value that supports casting, downcasting, formatting, and serializing. The producer of a ValueBag and its eventual consumer don't need to agree on a serialization contract. Any translation is handled internally by ValueBag.

Say we capture an i32 using its Display implementation as a ValueBag:

let bag = ValueBag::capture_display(42);

That value can then be cast to a concrete integer type, like u64:

let num = bag.as_u64().unwrap();

assert_eq!(42, num);

It could also be serialized as a number using serde:

let num = serde_json::to_value(&bag).unwrap();

assert!(num.is_number());

It works for more complex types too. Say we derive sval::Value on a type and capture it as a ValueBag:

#[derive(Value)]
struct Work {
    id: u64,
    description: String,
}

let work = Work {
    id: 123,
    description: String::from("do the work"),
}

let bag = ValueBag::capture_sval2(&work);

We could still serialize that value using serde without losing structure:

let obj = serde_json::to_value(&bag).unwrap();

assert!(obj.is_object());

It could also be formatted using Display:

assert_eq!("Work { id: 123, description: \"do the work\" }", bag.to_string());

The tradeoff in all this is that ValueBag needs to depend on the serialization frameworks (sval, serde, and std::fmt) that it supports, instead of just providing an API of its own for others to plug into. Doing this lets ValueBag guarantee everything will always line up, and keep its own public API narrow.

Getting started

Add the value-bag crate to your Cargo.toml:

[dependencies.value-bag]
version = "1.10.0"

You'll probably also want to add a feature for either sval (if you're in a no-std environment) or serde (if you need to integrate with other code that uses serde):

[dependencies.value-bag]
version = "1.10.0"
features = ["sval2"]
[dependencies.value-bag]
version = "1.10.0"
features = ["serde1"]

Then you're ready to capture anonymous values!

#[derive(Serialize)]
struct MyValue {
    title: String,
    description: String,
    version: u32,
}

// Capture a value that implements `serde::Serialize`
let bag = ValueBag::capture_serde1(&my_value);

// Print the contents of the value bag
println!("{:?}", bag);

Cargo features

The value-bag crate is no-std by default, and offers the following Cargo features:

  • std: Enable support for the standard library. This allows more types to be captured in a ValueBag.
  • error: Enable support for capturing std::error::Errors. Implies std.
  • sval: Enable support for using the sval serialization framework for inspecting ValueBags by implementing sval::value::Value. Implies sval2.
    • sval2: Enable support for the stable 2.x.x version of sval.
  • serde: Enable support for using the serde serialization framework for inspecting ValueBags by implementing serde::Serialize. Implies std and serde1.
    • serde1: Enable support for the stable 1.x.x version of serde.
  • owned: Add support for buffering ValueBags into an owned Send + Sync variant.
  • seq: Add support for working with sequences without needing to go through a full serialization framework.
  • test: Add test helpers for inspecting the shape of the value inside a ValueBag.

Dependencies

~0–260KB