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

Tracking Issue for core::mem::copy #98262

Open
1 of 3 tasks
jyn514 opened this issue Jun 19, 2022 · 10 comments
Open
1 of 3 tasks

Tracking Issue for core::mem::copy #98262

jyn514 opened this issue Jun 19, 2022 · 10 comments
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@jyn514
Copy link
Member

jyn514 commented Jun 19, 2022

Feature gate: #![feature(mem_copy_fn)]

This is a tracking issue for core::mem::copy, which is a way - like mem::drop - to copy a value without creating a new closure.

Public API

// core::mem
pub fn copy<T: Copy>(x: &T) -> T {}

Steps / History

Unresolved Questions

  • Should we find a way to make this an associated function on Copy instead?
@jyn514 jyn514 added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC labels Jun 19, 2022
@eggyal
Copy link
Contributor

eggyal commented Jun 20, 2022

If you have x: &T where T: Copy, why would you want core::mem::copy(x) when you can instead do *x? Won't that just be the implementation of this function?

@jyn514
Copy link
Member Author

jyn514 commented Jun 20, 2022

@eggyal it's the difference between option.map(|x| *x) and option.map(copy). The second is easier to read and write.

(In this particular case, there's Option::copied, but there's not Result::copied_err or anything; and this is useful for custom types too)

@DutchGhost
Copy link
Contributor

Why not make it const right away?

pub const fn copy<T: Copy>(x: &T) -> T {
    *x
}

compiles on stable...

@eggyal
Copy link
Contributor

eggyal commented Jun 20, 2022

Why not make it const right away?

Of course, this works if it's a free function; but would not (for now) if it's made an associated function on the Copy trait—so perhaps that alone might resolve the unresolved question ?

@Lokathor
Copy link
Contributor

i'm mildly concerned that std::mem::copy and std::ptr::copy both would exist but do very different sorts of things. Can we call this new function copied, same as the iterator/option method?

@jyn514
Copy link
Member Author

jyn514 commented Jul 20, 2022

Hmm sure, copied seems fine.

I do think that all the functions in std::ptr are kind of expected to deal with raw memory; I don't think it would be hard to tell them apart in practice. But I don't feel strongly.

@Lokathor
Copy link
Contributor

in an ideal world everything could be fully imported, so this helps the ideal.

@jyn514
Copy link
Member Author

jyn514 commented Aug 30, 2022

prior art:

/// Helper to ensure that queries only return `Copy` types.
#[inline(always)]
fn copy<T: Copy>(x: &T) -> T {
*x
}

@aleksanderkrauze
Copy link

I think this function should be marked #[must_use] as well.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 29, 2022
thomcc pushed a commit to tcdi/postgrestd that referenced this issue Feb 10, 2023
@Stargateur
Copy link
Contributor

Stargateur commented Feb 24, 2023

I feel this method while useful may be premature, specially we already have other solution like copied() and cloned(), I advice we wait for a strong demand before make this a stable method. I don't think I ever needed such thing except for few case in Option or Iterator that already have solution for that. If this copy is not in the prelude (hiding in mem module), there is very few reason to have it, write the closure could be as fast. Copy::copy would be better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants