Comments (8)
@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.
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.
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.
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.
Ref: #4
We also may want to add a Debug(&dyn Debug)
fallback option?
from valuable.
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.
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.
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)
- `NamedValues::get` footgun.
- Feature request: Expose doc comments for struct/enum/field definitions HOT 1
- Feature request: Allow reading structable attributes
- the trait `tracing::Value` is not implemented for `valuable::Value<'_> HOT 2
- question: is valuable expected to work with tracing macros? HOT 1
- Create a span for plugin init HOT 1
- Constructing objects from `Value`
- derive: implement #[valuable] attributes HOT 3
- Add a way to iterate on non-primitives/visit specific slices for Listable
- Visitor holding `Value` HOT 13
- Consider adding a concrete lifetime to the Visit trait HOT 1
- Consider changing Value::Error to &(dyn Error + 'static) HOT 1
- Owned [NamedField] for Fields HOT 10
- Consider Visit::visit_key + Visit::visit_value HOT 4
- Release initial version HOT 6
- publish `valuable-serde`
- `dyn Display` primitive value HOT 1
- How should we implement Valuable for Uuid? HOT 3
- Strange happenings with manual implementation of `Valuable` HOT 1
- Introduce a lifetime generic to `Visit` in order to allow borrowing out specific parts of the `valuable::Value` during visits HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from valuable.