Announcing Valuable, a library for object-safe value inspection

submited by
Style Pass
2021-05-31 09:00:06

Over the past few weeks, we have been working on Valuable, a new crate that provides object-safe value inspection. It is almost ready to publish, so I thought I'd write an article to introduce it. The crate offers an object-safe trait, Valuable, that allows callers to inspect the contents of the value, whether fields, enum variants, or primitives, without knowing its type. Initially, we wrote Valuable to support Tracing; however, it is helpful in several scenarios. Object-safe value inspection is a bit of a mouthful, so let's start by looking at Tracing and why it is needed there.

Tracing is a framework for instrumenting Rust programs to collect structured, event-based diagnostic information. Some consider it a structured logging framework, and while it can fill that use case, it can do a lot more. For example, Console aims to become a powerful tool for debugging async Rust applications and uses Tracing as its backbone. Tokio and other libraries emit instrumentation via Tracing. Console aggregates the events into a model of the application's execution, enabling the developer to gain insights into bugs and other issues.

Instrumented applications emit events with rich, structured data, and collectors receive the events. Of course, at compile-time, the instrumented application and the event collectors do not know about each other. A trait object bridges the instrumentation half with the collection half enabling collectors to register themselves dynamically. So, passing rich, structured data from the instrumentation half to the collector requires passing it through the trait object boundary. Tracing supports this at a minimal level today but does not support passing nested data.

Leave a Comment