GithubHelp home page GithubHelp logo

Comments (8)

carllerche avatar carllerche commented on September 26, 2024 1

@taiki-e Ah right, my mistake.

The strategy we use is similar to what @KodrAus is describing, but we use *able traits instead of fns. So, here, hypothetically, we would have trait Displayable and that would call a Visit::visit_display(display: &dyn Display) or something.

from valuable.

taiki-e avatar taiki-e commented on September 26, 2024 1

triage: A variant of Value to handle this (Value::Displayable?) can be added later without API break, so there is no need to consider this as a blocker of 0.1.0.

from valuable.

taiki-e avatar taiki-e commented on September 26, 2024

Another case of this problem is when I want to pass Value::Error to visitors. This can be avoided by making the error static (like the following example), but the problem remains that the error message cannot contain the details.

struct PathAsStr(std::path::PathBuf);

impl Valuable for PathAsStr {
    fn as_value(&self) -> Value<'_> {
        #[derive(Debug)]
        struct Error;
        static ERROR: Error = Error;
        impl std::fmt::Display for Error {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                std::fmt::Display::fmt(&"path is not valid UTF-8 string", f)
            }
        }
        impl std::error::Error for Error {}

        match self.0.to_str() {
            Some(s) => Value::String(s),
            None => Value::Error(&ERROR),
        }
    }

    fn visit(&self, visit: &mut dyn Visit) {
        visit.visit_value(self.as_value());
    }
}

from valuable.

carllerche avatar carllerche commented on September 26, 2024

Well, the intent is that we avoid work in the as_value() fn, so a conversion to string should be avoided. For both cases, we could possibly add a Display variant:

Value::Display(&dyn Display)

In the second case, the error is then received when visiting and the visitor can handle it as it wishes.

from valuable.

carllerche avatar carllerche commented on September 26, 2024

Ref: #4

We also may want to add a Debug(&dyn Debug) fallback option?

from valuable.

taiki-e avatar taiki-e commented on September 26, 2024

so a conversion to string should be avoided. For both cases, we could possibly add a Display variant:

Hmm. The creation of String is a red herring. The struct returned by Path::display() is also owned by the function, so the addition of Value::Display does not solve the problem.

That said, I think that Value::Display might be useful regardless of this problem.

from valuable.

KodrAus avatar KodrAus commented on September 26, 2024

When I ran into this case in the past I ended up with an API like this: https://docs.rs/value-bag/1.0.0-alpha.7/value_bag/fill/index.html

The variant on Value is effectively a &'a dyn Fn(Visit), so your value itself satisfies the 'a lifetime, but the data you pass to the visitor can be short-lived.

from valuable.

sunng87 avatar sunng87 commented on September 26, 2024

When working with valuable in handlebars, I may have a case for this issue as well.

During a template rendering process, some intermediate values are computed and are owned by internal context. If I will use valuable::Value as my internal type system, it needs capability to hold data like CoW.

Not quite sure if this matches original rational of valuable. But it seems to be required to use in handlebars.

from valuable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.