forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
auto merge of rust-lang#18486 : nikomatsakis/rust/operator-dispatch, …
…r=pcwalton This branch cleans up overloaded operator resolution so that it is strictly based on the traits in `ops`, rather than going through the normal method lookup mechanism. It also adds full support for autoderef to overloaded index (whereas before autoderef only worked for non-overloaded index) as well as for the slicing operators. This is a [breaking-change]: in the past, we were accepting combinations of operands that were not intended to be accepted. For example, it was possible to compare a fixed-length array and a slice, or apply the `!` operator to a `&int`. See the first two commits in this pull-request for examples. One downside of this change is that comparing fixed-length arrays doesn't always work as smoothly as it did before. Before this, comparisons sometimes worked due to various coercions to slices. I've added impls for `Eq`, `Ord`, etc for fixed-lengths arrays up to and including length 32, but if the array is longer than that you'll need to either newtype the array or convert to slices. Note that this plays better with deriving in any case than the previous scheme. Fixes rust-lang#4920. Fixes rust-lang#16821. Fixes rust-lang#15757. cc @alexcrichton cc @aturon
- Loading branch information
Showing
31 changed files
with
1,009 additions
and
263 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// 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. | ||
*/ | ||
|
||
#![stable] | ||
#![experimental] // not yet reviewed | ||
|
||
use cmp::*; | ||
use option::{Option}; | ||
|
||
// macro for implementing n-ary tuple functions and operations | ||
macro_rules! array_impls { | ||
($($N:expr)+) => { | ||
$( | ||
#[unstable = "waiting for PartialEq to stabilize"] | ||
impl<T:PartialEq> PartialEq for [T, ..$N] { | ||
#[inline] | ||
fn eq(&self, other: &[T, ..$N]) -> bool { | ||
self[] == other[] | ||
} | ||
#[inline] | ||
fn ne(&self, other: &[T, ..$N]) -> bool { | ||
self[] != other[] | ||
} | ||
} | ||
|
||
#[unstable = "waiting for Eq to stabilize"] | ||
impl<T:Eq> Eq for [T, ..$N] { } | ||
|
||
#[unstable = "waiting for PartialOrd to stabilize"] | ||
impl<T:PartialOrd> PartialOrd for [T, ..$N] { | ||
#[inline] | ||
fn partial_cmp(&self, other: &[T, ..$N]) -> Option<Ordering> { | ||
PartialOrd::partial_cmp(&self[], &other[]) | ||
} | ||
#[inline] | ||
fn lt(&self, other: &[T, ..$N]) -> bool { | ||
PartialOrd::lt(&self[], &other[]) | ||
} | ||
#[inline] | ||
fn le(&self, other: &[T, ..$N]) -> bool { | ||
PartialOrd::le(&self[], &other[]) | ||
} | ||
#[inline] | ||
fn ge(&self, other: &[T, ..$N]) -> bool { | ||
PartialOrd::ge(&self[], &other[]) | ||
} | ||
#[inline] | ||
fn gt(&self, other: &[T, ..$N]) -> bool { | ||
PartialOrd::gt(&self[], &other[]) | ||
} | ||
} | ||
|
||
#[unstable = "waiting for Ord to stabilize"] | ||
impl<T:Ord> Ord for [T, ..$N] { | ||
#[inline] | ||
fn cmp(&self, other: &[T, ..$N]) -> Ordering { | ||
Ord::cmp(&self[], &other[]) | ||
} | ||
} | ||
)+ | ||
} | ||
} | ||
|
||
array_impls! { | ||
0 1 2 3 4 5 6 7 8 9 | ||
10 11 12 13 14 15 16 17 18 19 | ||
20 21 22 23 24 25 26 27 28 29 | ||
30 31 32 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.