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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions src/libcore/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,36 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

/*!
* Implementations of things like `Eq` for fixed-length arrays
* up to a certain length. Eventually we should able to generalize
* to all lengths.
*/
//! Implementations of things like `Eq` for fixed-length arrays
//! up to a certain length. Eventually we should able to generalize
//! to all lengths.

#![stable]
#![experimental] // not yet reviewed

use cmp::*;
use option::{Option};
use clone::Clone;
use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
use fmt;
use kinds::Copy;
use option::Option;

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

*self
}
}

#[unstable = "waiting for Show to stabilize"]
impl<T:fmt::Show> fmt::Show for [T, ..$N] {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Show::fmt(&self[], f)
}
}

#[unstable = "waiting for PartialEq to stabilize"]
impl<T:PartialEq> PartialEq for [T, ..$N] {
#[inline]
Expand Down