diff --git a/src/libarena/lib.rs b/src/libarena/lib.rs index 415b12c7d05..3e93430eb69 100644 --- a/src/libarena/lib.rs +++ b/src/libarena/lib.rs @@ -132,7 +132,7 @@ impl Drop for Arena { #[inline] fn round_up(base: uint, align: uint) -> uint { - (base.checked_add(&(align - 1))).unwrap() & !(&(align - 1)) + (base.checked_add(&(align - 1))).unwrap() & !(align - 1) } // Walk down a chunk, running the destructors for any objects stored diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index f9f4ae534c1..b29d7d33782 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -919,7 +919,7 @@ impl Vec { /// /// ``` /// let mut vec = vec![1i, 2, 3, 4]; - /// vec.retain(|x| x%2 == 0); + /// vec.retain(|&x| x%2 == 0); /// assert_eq!(vec, vec![2, 4]); /// ``` #[unstable = "the closure argument may become an unboxed closure"] diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 522eb833637..b787de4423a 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -787,8 +787,8 @@ impl> FromIterator> for Option { /// use std::uint; /// /// let v = vec!(1u, 2u); - /// let res: Option> = v.iter().map(|x: &uint| - /// if *x == uint::MAX { None } + /// let res: Option> = v.iter().map(|&x: &uint| + /// if x == uint::MAX { None } /// else { Some(x + 1) } /// ).collect(); /// assert!(res == Some(vec!(2u, 3u))); diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 5b75e98baef..2ad5521bb76 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -894,8 +894,8 @@ impl> FromIterator> for Result { /// use std::uint; /// /// let v = vec!(1u, 2u); - /// let res: Result, &'static str> = v.iter().map(|x: &uint| - /// if *x == uint::MAX { Err("Overflow!") } + /// let res: Result, &'static str> = v.iter().map(|&x: &uint| + /// if x == uint::MAX { Err("Overflow!") } /// else { Ok(x + 1) } /// ).collect(); /// assert!(res == Ok(vec!(2u, 3u))); diff --git a/src/libcoretest/iter.rs b/src/libcoretest/iter.rs index 5d333d48e96..aeab18ca05e 100644 --- a/src/libcoretest/iter.rs +++ b/src/libcoretest/iter.rs @@ -356,7 +356,7 @@ fn test_iterator_size_hint() { assert_eq!(vi.zip(v2.iter()).size_hint(), (3, Some(3))); assert_eq!(vi.scan(0i, |_,_| Some(0i)).size_hint(), (0, Some(10))); assert_eq!(vi.filter(|_| false).size_hint(), (0, Some(10))); - assert_eq!(vi.map(|i| i+1).size_hint(), (10, Some(10))); + assert_eq!(vi.map(|&i| i+1).size_hint(), (10, Some(10))); assert_eq!(vi.filter_map(|_| Some(0i)).size_hint(), (0, Some(10))); } @@ -388,9 +388,9 @@ fn test_any() { #[test] fn test_find() { let v: &[int] = &[1i, 3, 9, 27, 103, 14, 11]; - assert_eq!(*v.iter().find(|x| *x & 1 == 0).unwrap(), 14); - assert_eq!(*v.iter().find(|x| *x % 3 == 0).unwrap(), 3); - assert!(v.iter().find(|x| *x % 12 == 0).is_none()); + assert_eq!(*v.iter().find(|&&x| x & 1 == 0).unwrap(), 14); + assert_eq!(*v.iter().find(|&&x| x % 3 == 0).unwrap(), 3); + assert!(v.iter().find(|&&x| x % 12 == 0).is_none()); } #[test] diff --git a/src/libstd/bitflags.rs b/src/libstd/bitflags.rs index 97a1f68606f..d8023dd3e4e 100644 --- a/src/libstd/bitflags.rs +++ b/src/libstd/bitflags.rs @@ -177,13 +177,13 @@ macro_rules! bitflags { /// Returns `true` if there are flags common to both `self` and `other`. #[inline] pub fn intersects(&self, other: $BitFlags) -> bool { - !(self & other).is_empty() + !(*self & other).is_empty() } /// Returns `true` all of the flags in `other` are contained within `self`. #[inline] pub fn contains(&self, other: $BitFlags) -> bool { - (self & other) == other + (*self & other) == other } /// Inserts the specified flags in-place. diff --git a/src/libstd/time/duration.rs b/src/libstd/time/duration.rs index 102f0028557..c3adae8cff8 100644 --- a/src/libstd/time/duration.rs +++ b/src/libstd/time/duration.rs @@ -319,7 +319,7 @@ impl fmt::Show for Duration { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { // technically speaking, negative duration is not valid ISO 8601, // but we need to print it anyway. - let (abs, sign) = if self.secs < 0 { (-self, "-") } else { (*self, "") }; + let (abs, sign) = if self.secs < 0 { (-*self, "-") } else { (*self, "") }; let days = abs.secs / SECS_PER_DAY; let secs = abs.secs - days * SECS_PER_DAY; diff --git a/src/libtime/lib.rs b/src/libtime/lib.rs index 590d0bfdcab..ac3574f5a03 100644 --- a/src/libtime/lib.rs +++ b/src/libtime/lib.rs @@ -96,7 +96,7 @@ impl Add for Timespec { let d_sec = other.num_seconds(); // It is safe to unwrap the nanoseconds, because there cannot be // more than one second left, which fits in i64 and in i32. - let d_nsec = (other - Duration::seconds(d_sec)) + let d_nsec = (*other - Duration::seconds(d_sec)) .num_nanoseconds().unwrap() as i32; let mut sec = self.sec + d_sec; let mut nsec = self.nsec + d_nsec; diff --git a/src/test/bench/shootout-meteor.rs b/src/test/bench/shootout-meteor.rs index 6e80c07a1a2..47e1969172d 100644 --- a/src/test/bench/shootout-meteor.rs +++ b/src/test/bench/shootout-meteor.rs @@ -290,7 +290,7 @@ fn search( let masks_at = &masks[i]; // for every unused piece - for id in range(0u, 10).filter(|id| board & (1 << (id + 50)) == 0) { + for id in range(0u, 10).filter(|&id| board & (1 << (id + 50)) == 0) { // for each mask that fits on the board for m in masks_at[id].iter().filter(|&m| board & *m == 0) { // This check is too costly. diff --git a/src/test/run-pass/operator-multidispatch.rs b/src/test/run-pass/operator-multidispatch.rs new file mode 100644 index 00000000000..fc79e4f0edb --- /dev/null +++ b/src/test/run-pass/operator-multidispatch.rs @@ -0,0 +1,41 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Test that we can overload the `+` operator for points so that two +// points can be added, and a point can be added to an integer. + +use std::ops; + +#[deriving(Show,PartialEq,Eq)] +struct Point { + x: int, + y: int +} + +impl ops::Add for Point { + fn add(&self, other: &Point) -> Point { + Point {x: self.x + (*other).x, y: self.y + (*other).y} + } +} + +impl ops::Add for Point { + fn add(&self, &other: &int) -> Point { + Point {x: self.x + other, + y: self.y + other} + } +} + +pub fn main() { + let mut p = Point {x: 10, y: 20}; + p = p + Point {x: 101, y: 102}; + assert_eq!(p, Point {x: 111, y: 122}); + p = p + 1; + assert_eq!(p, Point {x: 112, y: 123}); +}