Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exact same return value is casuing an issue. #7

Closed
Ryiski opened this issue Dec 2, 2024 · 3 comments
Closed

Exact same return value is casuing an issue. #7

Ryiski opened this issue Dec 2, 2024 · 3 comments

Comments

@Ryiski
Copy link

Ryiski commented Dec 2, 2024

Setting 2 or more empty list is causing an issue where the 2nd item and rest is referencing the type of direct_io.

#[serde_inline_default]
#[derive(Debug, Default, Serialize, Deserialize)]
struct Config {
	#[serde_inline_default(vec![])]
	pub direct_io: Vec<DirectIo>,
      
       #[serde_inline_default(vec![])]
	pub json_output: Vec<JsonOutput>,

        ......... Other fields
}

I can see in expand_struct, you are reusing the same value if it exists, can i ask why?

I mean, i can understand why, maybe just do them on primitive types?

pub(crate) fn expand_struct(mut item: ItemStruct) -> proc_macro::TokenStream {
	let mut inline_fns: Vec<(String, TokenStream, TokenStream)> = vec![];

	for (i, field) in item.fields.iter_mut().enumerate() {
		for (j, attr) in field.attrs.iter_mut().enumerate() {
			if !attr.path().is_ident("serde_inline_default") {
				continue;
			}

			let default: TokenStream = attr.parse_args().unwrap();

			// we check here if a function with the exact same return value already exists. if so, 
			// this function gets used.
			let fn_name_lit = if let Some((fn_name_lit, _, _)) = inline_fns
				.iter()
				.find(|(_, def, _)| def.to_string() == default.to_string()) <--------- HERE
			{
				fn_name_lit.clone()
			} else {
@bytedream
Copy link
Owner

Thanks for the issue report! I did this to reduce the number of total functions, which may improve compile time (haven't tested it tho, maybe the rust compiler itself optimizes this too).

@bytedream
Copy link
Owner

The new version 0.2.3 has a fix for it. I removed said logic completely. After looking into the generated assembly it seems that the compiler optimizes it itself (mostly by inlining the code).

@Ryiski
Copy link
Author

Ryiski commented Dec 3, 2024

@bytedream Can confirm it works, and thank you for the great package 👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants