GithubHelp home page GithubHelp logo

Owned [NamedField] for Fields about valuable HOT 10 OPEN

tokio-rs avatar tokio-rs commented on September 26, 2024
Owned [NamedField] for Fields

from valuable.

Comments (10)

hawkw avatar hawkw commented on September 26, 2024 1

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.

hawkw avatar hawkw commented on September 26, 2024

@sunng87 just to clarify here, is it the slice that should be owned, or the NamedFields that should be owned?

from valuable.

sunng87 avatar sunng87 commented on September 26, 2024

@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.

hawkw avatar hawkw commented on September 26, 2024

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.

sunng87 avatar sunng87 commented on September 26, 2024

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.

hawkw avatar hawkw commented on September 26, 2024

@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.

sunng87 avatar sunng87 commented on September 26, 2024

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:

  1. The lifetime 'a from StructDef<'a> should be same as the Map because the field names in StructDef borrows str from the map. But I got error[E0308]: method not compatible with trait with my code above.
  2. 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.

sunng87 avatar sunng87 commented on September 26, 2024

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.

carllerche avatar carllerche commented on September 26, 2024

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.

sunng87 avatar sunng87 commented on September 26, 2024

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)

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.