Skip to content

FieldAccessor doesn't seem to work on nested structs #3

@pinneh

Description

@pinneh

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions