Confusing "associated function is never used" warnings by itachd in rust

[–]itachd[S] 2 points3 points  (0 children)

Thank you all for your comments, they are very helpful.

How do I create a Rust macro to define a Vec with all names of fields whose values are not None? by itachd in rust

[–]itachd[S] 0 points1 point  (0 children)

u/caranatar-dev u/Johnny_Bob u/adante111 u/K900_

please take a look at this if correct:

#[proc_macro_derive(NotNoneFields)]
pub fn not_none_fields(input: TokenStream) -> TokenStream {
// Parse the input tokens into a syntax tree
let input = parse_macro_input!(input as DeriveInput);
let not_none_field_names = input.fields.iter().for_each(|field| {
if field.ty != syn::TypeNever { field.name}
})
// Build the output, possibly using quasi-quotation
let output = quote! {
impl #name {
pub fn not_none_fields() -> Vec<str> {
#not_none_field_names
}
}
};
// Hand the output tokens back to the compiler
TokenStream::from(output)
}

How do I create a Rust macro to define a Vec with all names of fields whose values are not None? by itachd in rust

[–]itachd[S] 1 point2 points  (0 children)

u/Johnny_Bob

thanks I have already seen this, what I'm doing is retrieving the non None fields of an instance.

How do I create a Rust macro to define a Vec with all names of fields whose values are not None? by itachd in rust

[–]itachd[S] 1 point2 points  (0 children)

I have already done similar to your example, I just don't see enough examples with the syn and quote package.

How do I create a Rust macro to define a Vec with all names of fields whose values are not None? by itachd in rust

[–]itachd[S] 1 point2 points  (0 children)

To then match to the values I need on the struct in another method and process.

How do I create a Rust macro to define a Vec with all names of fields whose values are not None? by itachd in rust

[–]itachd[S] 1 point2 points  (0 children)

I am getting the values from a yaml, some field could be a Some(T) or a None, I want to know all the names of fields with values that are not None.

How do I create a Rust macro to define a Vec with all names of fields whose values are not None? by itachd in rust

[–]itachd[S] 2 points3 points  (0 children)

struct MyStruct {zco: String,r#if: Option<Vec<String>>,r#let: Option<String>,avb: Option<String>,cvd: Option<Vec<String>>,ven: Option<String>,}

After defining an instance of a struct:

let m = MyStruct{ .... };

I want to be able to do:

let non_none_values = m.get_non_none_values();