Giter VIP home page Giter VIP logo

field_accessor's People

Contributors

europeanplaice avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

field_accessor's Issues

FieldAccessor doesn't seem to work on nested structs

Hi, I'm a beginner with rust, and I have a set of structs on which I want to access arbitrary properties via strings, so this crate seems to be precisely what I need. However, when I try to implement field_accessor on structs with other structs inside of them, it errors out and I don't know how to fix it.

Below is the smallest reproduction I could manage:

use field_accessor::FieldAccessor;

#[derive(FieldAccessor)]
pub struct UserData {
    some_field: String
}

#[derive( FieldAccessor)]
pub struct User {
    name: String,
    data: UserData
}

fn main() {
    let user_data = UserData{some_field: "some value".to_string()};
    let my_user = User{name: "aGoodName".to_string(), data: user_data};
    let field_name = "name".to_string();
    let name: &String = my_user.get(&field_name).unwrap();
}

This gives me the following errors on the User struct:

binary operation `!=` cannot be applied to type `UserData` E0369 Note: an implementation of `PartialEq<_>` might be missing for `UserData` Help: consider annotating `UserData` with `#[derive(PartialEq)]`
binary operation `==` cannot be applied to type `UserData` E0369 Note: an implementation of `PartialEq<_>` might be missing for `UserData` Help: consider annotating `UserData` with `#[derive(PartialEq)]`
can't compare `UserData` with `_` E0277 Help: the trait `PartialOrd<_>` is not implemented for `UserData`
no method named `clone` found for struct `UserData` in the current scope E0599 Help: items from traits can only be used if the trait is implemented and in scope Note: the following trait defines an item `clone`, perhaps you need to implement it: candidate #1: `Clone`
the trait bound `UserData: Clone` is not satisfied E0277
the trait bound `UserData: Default` is not satisfied E0277 Note: required by a bound in `std::mem::take`
`UserData` doesn't implement `Debug` E0277 Help: the trait `Debug` is not implemented for `UserData` Note: add `#[derive(Debug)]` to `UserData` or manually `impl Debug for UserData`

Now, I can implement each of these missing traits on userData, but it doesn't feel "right"
Is there something I'm missing? Is this not supported?

the name `GetterSetter` is defined multiple times. Perhaps unhygienic

When using FieldAccessor on multiple structs it results in import errors.

Code:

use field_accessor::FieldAccessor;


#[derive(FieldAccessor)]
struct Test {
    pub name: String,
}


#[derive(FieldAccessor)]
struct Test2 {
    pub name: String,
}

Error:

error[E0428]: the name `GetterSetter` is defined multiple times
 --> src/main.rs:9:10
  |
3 | #[derive(FieldAccessor)]
  |          ------------- previous definition of the trait `GetterSetter` here
...
9 | #[derive(FieldAccessor)]
  |          ^^^^^^^^^^^^^ `GetterSetter` redefined here
  |
  = note: `GetterSetter` must be defined only once in the type namespace of this module
  = note: this error originates in the derive macro `FieldAccessor` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0252]: the name `mem` is defined multiple times
 --> src/main.rs:9:10
  |
3 | #[derive(FieldAccessor)]
  |          ------------- previous import of the module `mem` here
...
9 | #[derive(FieldAccessor)]
  |          ^^^^^^^^^^^^^ `mem` reimported here
  |
  = note: `mem` must be defined only once in the type namespace of this module
  = note: this error originates in the derive macro `FieldAccessor` (in Nightly builds, run with -Z macro-backtrace for more info)

Some errors have detailed explanations: E0252, E0428.
For more information about an error, try `rustc --explain E0252`.
error: could not compile `macro_debug` due to 2 previous errors

I don't know much about macros, but the solutions I can think of are:

  • give GetterSetter a name related to the struct. Like let gettersetter = format_ident!("{}{}", ident, "GetterSetter");. The use std::mem; is still a problem though.
  • wrap the struct in a module:
pub use test::Test;
mod test {
    use field_accessor::FieldAccessor;

    #[derive(FieldAccessor)]
    pub struct Test {
        pub status: String,
    }
} 
  • split the trait declaration and the specific methods. So create another macro? to expand to the GetterSetter trait and the use std::mem; import. And then use the FieldAccessor derive

I think the user-friendliest option is number 2

Is it possible use reference fields in struct (borrowed with lifetime) ?

I have a compiled code like below

    use field_accessor::FieldAccessor;
    use serde::Serialize;

    // #[derive(Debug, Serialize, FieldAccessor)]
    #[derive(Debug, Serialize)]
    pub struct StructModel<'a> {
        pub ref_name: &'a String,
    }
    impl<'a> StructModel<'a> {
        pub fn new(ref_name: &'a String) -> StructModel<'a> {
            StructModel {
                ref_name,
            }
        }
    }

if I'm uncomment #[derive(Debug, Serialize, FieldAccessor)] it compiles with error

     pub struct StructModel<'a> {
                           - help: consider introducing lifetime `'a` here: `<'a>`
         pub ref_name: &'a String,
                        ^^ undeclared lifetime`

Is it possible to do ?
Thanks

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.