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

Cow from array #117342

Closed
wants to merge 2 commits into from
Closed

Cow from array #117342

wants to merge 2 commits into from

Conversation

bend-n
Copy link
Contributor

@bend-n bend-n commented Oct 29, 2023

enables

Cow::<[u8]>::from(&[])

as well as

Cow::<[i32]>::from(&[1, 1, 2, 3, 5, 8])

References #116890

@rustbot
Copy link
Collaborator

rustbot commented Oct 29, 2023

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Mark-Simulacrum (or someone else) soon.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 29, 2023
@workingjubilee workingjubilee added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 29, 2023
@workingjubilee
Copy link
Member

r? libs-api

@rustbot rustbot assigned dtolnay and unassigned Mark-Simulacrum Oct 29, 2023
Copy link
Member

@dtolnay dtolnay 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 the PR!

Before I put this to FCP, would you be able to share some example code in which doing this conversion generically using From is useful? Usually I'd expect what you showed to be written as:

Cow::Borrowed::<[i32]>(&[1, 1, 2, 3, 5, 8])

or:

Cow::Borrowed(&[1, 1, 2, 3, 5, 8] as &[i32])

@dtolnay dtolnay added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 30, 2023
@bend-n
Copy link
Contributor Author

bend-n commented Oct 30, 2023

Its supposed to fix Cow::from(&[1, 1, 2, 3, 5, 8][..]). I didnt think of simply using Cow::Borrowed.

I think its still useful though, if only because it saves you a rewrite when you do it (dont have to change it to Cow::Borrowed), as its very annoying when your doctest fails because of this.

Is Cow::Borrowed / Cow::Owned generally preferred to Cow::from anyway?

@dtolnay
Copy link
Member

dtolnay commented Oct 30, 2023

I would say, yes, Cow::Borrowed is preferred. There is no advantage to going through a generic From impl in the case you describe; it's only more roundabout.

The impls exists in support of generic code like this:

pub fn set_arg(
&mut self,
name: impl Into<Cow<'static, str>>,
arg: impl IntoDiagnosticArg,
) -> &mut Self {
self.args.insert(name.into(), arg.into_diagnostic_arg());
self
}

where they enable different callers to efficiently pass a variety of different types for the name arg.

@bend-n
Copy link
Contributor Author

bend-n commented Oct 30, 2023

Wont this help, there, then?

Co-authored-by: zachs18 <[email protected]>
@oisyn
Copy link

oisyn commented Nov 17, 2023

@dtolnay That generic code is the exact reason I opened an issue about this about a month ago: #116890. If a function accepts a impl Cow<'a, [T]>, you can't give it a &[T; N], currently.

fn foo<'a>(v: impl Into<Cow<'a, [i32]>>) { }

fn main() {
    foo(&[1, 2, 3]);
}

@zachs18
Copy link
Contributor

zachs18 commented Dec 6, 2023

CC #113489

Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

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

Thanks zachs18! Closing in favor of #113489, as that one was opened earlier and demonstrated the use case.

@dtolnay dtolnay closed this Jan 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-fcp This change is insta-stable, so needs a completed FCP to proceed. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants