Skip to content
This repository was archived by the owner on Dec 17, 2025. It is now read-only.

fix: impl_generics_with_additional_lifetimes#41

Merged
VictorKoenders merged 3 commits intobincode-org:trunkfrom
Daniel-Aaron-Bloom:fix-impl_for_with_lifetimes
Mar 30, 2023
Merged

fix: impl_generics_with_additional_lifetimes#41
VictorKoenders merged 3 commits intobincode-org:trunkfrom
Daniel-Aaron-Bloom:fix-impl_for_with_lifetimes

Conversation

@Daniel-Aaron-Bloom
Copy link
Contributor

I'm not entirely sure why this function was originally trying to bound lifetimes, but regardless, what it currently does is very incorrect as it adds copies of every generic for each lifetime passed in.

I don't think any of that is right, so this fixes that, removes the unused helper as_lifetime, and adds a test to ensure the expected behavior occurs.

@codecov
Copy link

codecov bot commented Mar 30, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: +7.77 🎉

Comparison is base (04567e9) 39.94% compared to head (1bee6fd) 47.72%.

❗ Current head 1bee6fd differs from pull request most recent head 54634d3. Consider uploading reports for the commit 54634d3 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##            trunk      #41      +/-   ##
==========================================
+ Coverage   39.94%   47.72%   +7.77%     
==========================================
  Files          19       19              
  Lines        1860     1869       +9     
==========================================
+ Hits          743      892     +149     
+ Misses       1117      977     -140     
Impacted Files Coverage Δ
src/generate/generator.rs 57.74% <100.00%> (+57.74%) ⬆️
src/parse/generics.rs 70.94% <100.00%> (+12.06%) ⬆️

... and 5 files with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Contributor

@VictorKoenders VictorKoenders left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, I think the code was like this because it was doing something else before, and this ended up the way it was refactored

Either that or I'm just not smart

Either case this is better!

@VictorKoenders VictorKoenders merged commit b75a429 into bincode-org:trunk Mar 30, 2023
@VictorKoenders
Copy link
Contributor

Anything else you want to make a PR of or shall I release 0.0.12 with these changes?

@Daniel-Aaron-Bloom Daniel-Aaron-Bloom deleted the fix-impl_for_with_lifetimes branch March 30, 2023 07:31
@mkj
Copy link
Contributor

mkj commented Mar 30, 2023

This is no longer adding dependent lifetimes, that are needed at least in my case. The docs have

The lifetimes passed to this function will automatically depend on any other lifetime this struct or enum may have. Example:

The struct is struct Foo<'a> {}
You call `generator.impl_for_with_lifetime(“Bar”, &[“b”])
The code will be impl<'a, 'b: 'a> Bar<'b> for Foo<'a> {}

https://docs.rs/virtue/latest/virtue/generate/struct.Generator.html#method.impl_for_with_lifetimes

From my example use, the diff after this change is:

-impl < 'de : 'a, 'a > crate :: sshwire :: SSHDecode < 'de > for UserauthPwChangeReq < 'a >
+impl < 'de, 'a > crate :: sshwire :: SSHDecode < 'de > for UserauthPwChangeReq < 'a >

(that's for https://github.com/mkj/sunset/blob/c97ff133d51ae482cbb61ae355dafdaa08dd3a34/sshwire-derive/src/lib.rs#L392 targetting struct https://github.com/mkj/sunset/blob/c97ff133d51ae482cbb61ae355dafdaa08dd3a34/src/packets.rs#L164)

@VictorKoenders
Copy link
Contributor

I ran into this in bincode as well. I solved it with

generator.impl_for_with_lifetimes(format!("{}::BorrowDecode", crate_name), ["__de"])
    .modify_generic_constraints(|generics, where_constraints| {
        for lt in generics.iter_lifetimes() {
            where_constraints.push_parsed_constraint(format!("'__de: '{}", lt.ident))?;
        }
        Ok(())
    })?

I'm thinking the new behavior is correct because not everyone will want this behavior, but maybe we can add a helper method that does this?

@mkj
Copy link
Contributor

mkj commented Mar 31, 2023

Thanks, that works for me. I agree it shouldn't be the default. Maybe just adding an example in the impl_for_with_lifetimes() documentation would be enough.

@VictorKoenders
Copy link
Contributor

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants