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

Support mocking multiple traits with same name #601

Merged
merged 1 commit into from
Aug 20, 2024
Merged

Support mocking multiple traits with same name #601

merged 1 commit into from
Aug 20, 2024

Conversation

discosultan
Copy link
Contributor

An attempt to solve #600.

Not really sure about backward compatibility. Should be fine as we only change the generation of a private identifier. But it could cause issues if either a length limit is reached or a there's a name collision with a different ident.

@@ -176,3 +174,45 @@ impl MockTrait {
)
}
}

fn write_join_path(
Copy link
Owner

Choose a reason for hiding this comment

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

It would be more idiomatic if this function allocated and returned a new String.

Copy link
Contributor Author

@discosultan discosultan Aug 17, 2024

Choose a reason for hiding this comment

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

I was trying to avoid string allocations but then again, I couldn't figure out a way to know the required capacity ahead of time anyway, so even the current single String::new approach keeps resizing it internally.

I've now updated the fn to return an owned string instead.

Ok(())
}

fn hash_path_arguments(path: &Path) -> Option<u64> {
Copy link
Owner

Choose a reason for hiding this comment

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

Do you have any examples of a path that has path_args in segment that isn't the last?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't have an example. This was done since I assumed processing the hash + concatenating the ident are symmetric. Whereas before both were done on the last segment, now we go over all the segments. Happy to revert this part to only process the last segment.

// Similar to how the `format_ident!` constructs an `Ident`.
// https://github.com/dtolnay/quote/blob/3256f897c9821f9b397cb81851ae9a1672a84cb7/src/runtime.rs#L478-L484
fn ident_maybe_raw(id: &str, span: Span) -> Ident {
if let Some(id) = id.strip_prefix("r#") {
Copy link
Owner

Choose a reason for hiding this comment

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

Ouch. Did you personally have a need to mock a trait with a raw identifier? This warrants a test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It caused the existing raw_identifier test to not compile

Copy link
Owner

@asomers asomers left a comment

Choose a reason for hiding this comment

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

Looks good. Now how about a CHANGELOG entry?

fn join_path_segments(path: &Path, sep: &str) -> String {
let mut output = String::new();
for segment in &path.segments {
if write!(
Copy link
Owner

Choose a reason for hiding this comment

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

Too bad there isn't something like a Punctuated::join method to help with this part.

CHANGELOG.md Outdated
@@ -3,6 +3,14 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [ unreleased ]
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
## [ unreleased ]
## [ Unreleased ] - ReleaseDate

Copy link
Owner

@asomers asomers left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution!

@asomers asomers merged commit 7dd21ad into asomers:master Aug 20, 2024
3 checks passed
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

Successfully merging this pull request may close these issues.

2 participants