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

Implement TryFrom<String> for Name #347

Merged
merged 2 commits into from
May 23, 2024
Merged

Implement TryFrom<String> for Name #347

merged 2 commits into from
May 23, 2024

Conversation

RickyDaMa
Copy link
Collaborator

@RickyDaMa RickyDaMa commented May 21, 2024

Allows for re-using the allocated memory if you already have the owned type, and also avoids allocating in the error case 😎

This addresses that invasive brain thought I get when I have to rely on the compiler to realise it can re-use an allocation, instead of explicitly having a code path for it

This PR also contains an unrelated commit to fix a clippy lint

Allows for re-using the allocated memory if you already have the owned type
Copy link
Member

@cmyr cmyr left a comment

Choose a reason for hiding this comment

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

This looks fine, but unfortunately I do not believe it reuses the allocation? For instance it doesn't appear that From<String> for Arc<str> reuses an allocation (I'm inferring this, but the docs imply that the data is copied to the new allocation)

@RickyDaMa
Copy link
Collaborator Author

That's fair, I'd never actually delved into the Arc conversion implementation

Maybe it just helps the compiler optimise it, since it can see the allocation for the String will be dropped as the method takes ownership. Not sure, it's been a while since I've read about such things, so I don't have a citation to jump to

@RickyDaMa RickyDaMa merged commit bc069f4 into master May 23, 2024
4 checks passed
@RickyDaMa RickyDaMa deleted the try-from-string-name branch May 23, 2024 16:39
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