Skip to content

Accept &str (and slice) in metrics arguments through ToOwned - #291

Open
AdamGS wants to merge 2 commits into
cloudflare:mainfrom
AdamGS:main
Open

AdamGS wants to merge 2 commits into
cloudflare:mainfrom
AdamGS:main

Conversation

@AdamGS

@AdamGS AdamGS commented Oct 27, 2026

Copy link
Copy Markdown

The current version of #[metrics] forces arguments to either be Copy or Clone, which for strings forces some unfortunate type signatures (like &String) which are way less ergonomic.

This PR adds supports for two common types that implement ToOwned - str and slice.

#ident #colon_token ::std::clone::Clone::clone(#ident)
},
ArgMode::ToOwned(_) => quote! {
#ident #colon_token ::std::borrow::ToOwned::to_owned(#ident)

@TheJokr TheJokr Oct 28, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

ToOwned has a blanket impl for any T: Clone, I think we can simplify this by using to_owned for all ArgMode::Clone types? (rename the enum variant if you want)

that would make it strictly more powerful than the current released code, and we wouldn't have to add a check for is_unsized (thus also supporting type aliases and custom unsized types).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

pushed a new version, seems smaller and is indeed functionally identical as far as I can tell.

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