europeanplaice / field_accessor Goto Github PK
View Code? Open in Web Editor NEWA procedural macro to get and set a struct field by String dynamically.
Home Page: https://crates.io/crates/field_accessor
License: MIT License
A procedural macro to get and set a struct field by String dynamically.
Home Page: https://crates.io/crates/field_accessor
License: MIT License
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?
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:
let gettersetter = format_ident!("{}{}", ident, "GetterSetter");
. The use std::mem;
is still a problem though.pub use test::Test;
mod test {
use field_accessor::FieldAccessor;
#[derive(FieldAccessor)]
pub struct Test {
pub status: String,
}
}
use std::mem;
import. And then use the FieldAccessor deriveI think the user-friendliest option is number 2
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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.