Skip to content

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.
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
Contributor

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