Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dyn Display primitive value #86

Open
hawkw opened this issue Feb 7, 2022 · 1 comment
Open

dyn Display primitive value #86

hawkw opened this issue Feb 7, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@hawkw
Copy link
Member

hawkw commented Feb 7, 2022

In some cases, it may be desirable for types implementing Valuable to record themselves as a pre-formatted human-readable text representation. Currently, this is only possible with the Value::String variant, which takes a &'a str.

For types that want to record themselves using a Display (or Debug) implementation, though, Value::String isn't really sufficient. Because it takes a borrowed string slice, those types would have to allocate ahead of time and store their own formatted representation, so that the borrowed string slice can be handed out in their Valuable impls. This is not ideal.

Therefore, we should probably add &dyn fmt::Display as a primitive Value variant.

@hawkw hawkw added documentation Improvements or additions to documentation enhancement New feature or request and removed documentation Improvements or additions to documentation labels Feb 7, 2022
@fluffysquirrels
Copy link

I would also like this or a similar solution.

The workaround I use is to return a single-value tuple that is a String. This works well enough, but does add a layer of noise in the output I would prefer to avoid.

Here's an example:

struct MyStruct;

impl Valuable for MyStruct {
    fn as_value(&self) -> Value<'_> {
        // We don't store a String of the value, so can't return an &str
        // and use `Value<'a>::String(&'a str)`, encode as a tuple instead.
        Value::Tuplable(self)
    }

    fn visit(&self, visit: &mut dyn valuable::Visit) {
        let s: String = foo();
        let val = Value::String(s.as_str());
        visit.visit_unnamed_fields(&[val]);
    }
}

impl Tuplable for MyStruct {
    fn definition(&self) -> TupleDef {
        TupleDef::new_static(1)
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants