Comments (10)
Hmm, I see why you can't borrow the fields here, so you're right that an owned variant might be necessary. Thanks for the example!
from valuable.
@sunng87 just to clarify here, is it the slice that should be owned, or the NamedField
s that should be owned?
from valuable.
@hawkw I think I need for both. I'm trying to construct a Structable
from a JSON object, which may have dynamic fields that are determined at runtime, in order to get O(1) access to the fields via valuable API.
Type like this works best for my scenario:
pub enum Fields<'a> {
/// Named fields
Named(&'a [NamedField<'a>]),
OwnedNamed(Vec<OwnedNamedField>), // where OwnedNamedField hold an owned String instead of str
/// Unnamed (positional) fields or unit
Unnamed,
}
from valuable.
Right, I'm looking into a possible change like this. But, I'm not totally sure if I understand why this change is necessary. If you have a Vec
of field names, can't you just pass a borrowed slice of that Vec
into Fields::Named
?
from valuable.
That sounds reasonable. It has been a while since last time I'm running into this issue. Let me check my feature branch again to refresh my context about this.
from valuable.
@sunng87 let me know if that solution works out for you; I'd like to get this issue figured out soon, because if we do add code to make owned named fields possible, we would probably have to make some breaking changes, and it would be nice to figure out what's necessary here before releasing 0.1.
from valuable.
Sorry for late response. I may still have issue with current NamedField
vector. My use-case is to implement Structable
for serde_json::Value
and its internal Map<String, Value>
. Because the map has dynamic fields so I use StructDef::Dynamic
here.
impl<'a> Structable for Map<String, Json> {
fn definition(&'a self) -> StructDef<'a> {
let field_defs: Vec<NamedField<'a>> = Vec::new();
// add field names
let fields = Fields::Named(&field_defs);
let def = StructDef::Dynamic {
name: "Json",
fields,
};
def
}
}
There are two issues from this piece of code:
- The lifetime
'a
from StructDef<'a> should be same as theMap
because the field names inStructDef
borrows str from the map. But I goterror[E0308]: method not compatible with trait
with my code above. - The
NamedField
vector is created within the function so it's impossible to borrow it outside.
OwnedNamed(Vec<OwnedNamedField>)
in my previous comment can fix both 1 and 2 issue.
from valuable.
Another suggestion is to make name
of StructDef::Dynamic
an Option<String>
because the json object is typically anonymous in term of a type.
from valuable.
At some point, I'd say that the type isn't a structable but a mappable.
I think it would be fair to add some additional trait fns that make it easier to work w/ json objects. Eg. get(&str) -> Option<Value<'_>>
or something like that.
from valuable.
The only concern with Mappable
is it doesn't seem to have O(1) access to value with given key name.
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
- 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.