pub trait Visualize: Sized {
// Provided methods
fn data(&self) -> Option<Value> { ... }
fn associated_data(&self) -> Option<Vec<DataDescription>> { ... }
fn render_node(&self) -> String { ... }
}Expand description
A trait for defining how to visually represent a type
For manual implementations, note that each of these methods has a default, so implementors need only implement the methods they need to modify.
Provided Methods§
Sourcefn data(&self) -> Option<Value>
fn data(&self) -> Option<Value>
How to represent the data for this type, if at all
§Primitive Example
For primitives, this will likely be the primitive’s value. This example is ignored because it conflicts with the implementation of Visualize for u8 provided by this crate.
use vizz::Value;
use vizz::Visualize;
impl Visualize for u8 {
fn data(&self) -> Option<Value> {
Some(Value::Owned(self.to_string()))
}
}§Enum Example
For enums, this will likely be the variant name.
use vizz::Value;
use vizz::Visualize;
enum MyEnum {
SimpleVariant,
VariantWithData(u8),
}
impl Visualize for MyEnum {
fn data(&self) -> Option<Value> {
Some(Value::Owned(
match self {
MyEnum::SimpleVariant => "SimpleVariant",
MyEnum::VariantWithData(_) => "VariantWithData",
}.into()
))
}
}§Struct Example
For structs, this will likely be None because structs don’t have any inherent data, but instead contain other types, and therefore the default method implementation need not be overridden. The data contained in a struct should be used in Visualize::associated_data.
Sourcefn associated_data(&self) -> Option<Vec<DataDescription>>
fn associated_data(&self) -> Option<Vec<DataDescription>>
The list of associated data structures that should be included in a visualization of this data type
For named associated data, such as struct members or named enum variant members, the DataDescription::with_label method can be used to add a label.
§Primitive Example
For primitives, this will likely be None, and therefore the default method implementation need not be overridden.
§Enum Example
For enums with associated data, this will likely be those values. For enums with no associated data, this will likely be None.
use vizz::Visualize;
use vizz::DataDescription;
enum MyEnum {
SimpleVariant,
VariantWithData(u8),
}
impl Visualize for MyEnum {
fn associated_data(&self) -> Option<Vec<DataDescription>> {
match self {
MyEnum::SimpleVariant => None,
MyEnum::VariantWithData(data) => Some(vec![DataDescription::from(data)]),
}
}
}§Struct Example
For structs, this will likely be the struct members.
use vizz::Visualize;
use vizz::DataDescription;
struct MyStruct<'a> {
my_string: String,
my_ref: &'a String,
}
impl<'a> Visualize for MyStruct<'a> {
fn associated_data(&self) -> Option<Vec<DataDescription>> {
Some(vec![
DataDescription::from(&self.my_string).with_label("my_string"),
DataDescription::from(&self.my_ref).with_label("my_ref"),
])
}
}Sourcefn render_node(&self) -> String
fn render_node(&self) -> String
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.