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

Add Show and Clone trait to arrays #18951

Merged
merged 1 commit into from
Nov 18, 2014
Merged

Conversation

tbu-
Copy link
Contributor

@tbu- tbu- commented Nov 14, 2014

Due to not being able to parametrize over array sizes, Clone is only
implemented for element types that are Copyable.

Due to not being able to parametrize over array sizes, `Clone` is only
implemented for element types that are `Copy`able.

// macro for implementing n-ary tuple functions and operations
macro_rules! array_impls {
($($N:expr)+) => {
$(
#[unstable = "waiting for Clone to stabilize"]
impl<T:Copy> Clone for [T, ..$N] {
fn clone(&self) -> [T, ..$N] {
Copy link

Choose a reason for hiding this comment

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

You should be able to implement for non-Copy Ts unsafely:

fn clone(&self) -> [T, ..$N] {
    let mut cloned: [T, ..$N] = unsafe { mem::zeroed() };
    for i in range(0, $N) {
        cloned[i] = x[i].clone();
    }
    cloned
}

I think this is safe with regards to destructors and clone() failing but someone else should confirm. :-)

Copy link
Contributor

Choose a reason for hiding this comment

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

That's not safe because clone can throw, and calling a destructor on zeroed memory is not well-defined. Drop flags are an implementation detail and removing them is planned. It is possible to write this code correctly, but you need to handle the unwinding case manually.

Copy link
Contributor

Choose a reason for hiding this comment

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

Exceptions are just plain awful.

@nikomatsakis
Copy link
Contributor

cc @aturon I'm assuming there'd be no objections, particularly as these impls are marked unstable.

@aturon
Copy link
Member

aturon commented Nov 17, 2014

Yep, LGTM

@bors bors merged commit eb9684e into rust-lang:master Nov 18, 2014
lnicola pushed a commit to lnicola/rust that referenced this pull request Jan 20, 2025
Add missing `#[rust_analyzer::rust_fixture]` annotations
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.

5 participants