auto merge of #9291 : jzelinskie/rust/remove-cond, r=alexcrichton

This is my first contribution, so please point out anything that I may have missed.

I consulted IRC and settled on `match () { ... }` for most of the replacements.
This commit is contained in:
bors 2013-09-19 00:31:05 -07:00
commit a7cf7b7b0b
8 changed files with 55 additions and 137 deletions

View File

@ -115,8 +115,8 @@ impl TotalOrd for BigUint {
if s_len > o_len { return Greater; } if s_len > o_len { return Greater; }
for (&self_i, &other_i) in self.data.rev_iter().zip(other.data.rev_iter()) { for (&self_i, &other_i) in self.data.rev_iter().zip(other.data.rev_iter()) {
cond!((self_i < other_i) { return Less; } if self_i < other_i { return Less; }
(self_i > other_i) { return Greater; }) if self_i > other_i { return Greater; }
} }
return Equal; return Equal;
} }

View File

@ -281,11 +281,11 @@ pub fn escape_unicode(c: char, f: &fn(char)) {
// avoid calling str::to_str_radix because we don't really need to allocate // avoid calling str::to_str_radix because we don't really need to allocate
// here. // here.
f('\\'); f('\\');
let pad = cond!( let pad = match () {
(c <= '\xff') { f('x'); 2 } _ if c <= '\xff' => { f('x'); 2 }
(c <= '\uffff') { f('u'); 4 } _ if c <= '\uffff' => { f('u'); 4 }
_ { f('U'); 8 } _ => { f('U'); 8 }
); };
for offset in range_step::<i32>(4 * (pad - 1), -1, -4) { for offset in range_step::<i32>(4 * (pad - 1), -1, -4) {
unsafe { unsafe {
match ((c as i32) >> offset) & 0xf { match ((c as i32) >> offset) & 0xf {
@ -329,13 +329,13 @@ pub fn len_utf8_bytes(c: char) -> uint {
static MAX_FOUR_B: uint = 2097152u; static MAX_FOUR_B: uint = 2097152u;
let code = c as uint; let code = c as uint;
cond!( match () {
(code < MAX_ONE_B) { 1u } _ if code < MAX_ONE_B => 1u,
(code < MAX_TWO_B) { 2u } _ if code < MAX_TWO_B => 2u,
(code < MAX_THREE_B) { 3u } _ if code < MAX_THREE_B => 3u,
(code < MAX_FOUR_B) { 4u } _ if code < MAX_FOUR_B => 4u,
_ { fail!("invalid character!") } _ => fail!("invalid character!"),
) }
} }
impl ToStr for char { impl ToStr for char {

View File

@ -206,35 +206,35 @@ impl Orderable for f32 {
/// Returns `NaN` if either of the numbers are `NaN`. /// Returns `NaN` if either of the numbers are `NaN`.
#[inline] #[inline]
fn min(&self, other: &f32) -> f32 { fn min(&self, other: &f32) -> f32 {
cond!( match () {
(self.is_NaN()) { *self } _ if self.is_NaN() => *self,
(other.is_NaN()) { *other } _ if other.is_NaN() => *other,
(*self < *other) { *self } _ if *self < *other => *self,
_ { *other } _ => *other,
) }
} }
/// Returns `NaN` if either of the numbers are `NaN`. /// Returns `NaN` if either of the numbers are `NaN`.
#[inline] #[inline]
fn max(&self, other: &f32) -> f32 { fn max(&self, other: &f32) -> f32 {
cond!( match () {
(self.is_NaN()) { *self } _ if self.is_NaN() => *self,
(other.is_NaN()) { *other } _ if other.is_NaN() => *other,
(*self > *other) { *self } _ if *self > *other => *self,
_ { *other } _ => *other,
) }
} }
/// Returns the number constrained within the range `mn <= self <= mx`. /// Returns the number constrained within the range `mn <= self <= mx`.
/// If any of the numbers are `NaN` then `NaN` is returned. /// If any of the numbers are `NaN` then `NaN` is returned.
#[inline] #[inline]
fn clamp(&self, mn: &f32, mx: &f32) -> f32 { fn clamp(&self, mn: &f32, mx: &f32) -> f32 {
cond!( match () {
(self.is_NaN()) { *self } _ if self.is_NaN() => *self,
(!(*self <= *mx)) { *mx } _ if !(*self <= *mx) => *mx,
(!(*self >= *mn)) { *mn } _ if !(*self >= *mn) => *mn,
_ { *self } _ => *self,
) }
} }
} }

View File

@ -229,35 +229,35 @@ impl Orderable for f64 {
/// Returns `NaN` if either of the numbers are `NaN`. /// Returns `NaN` if either of the numbers are `NaN`.
#[inline] #[inline]
fn min(&self, other: &f64) -> f64 { fn min(&self, other: &f64) -> f64 {
cond!( match () {
(self.is_NaN()) { *self } _ if self.is_NaN() => *self,
(other.is_NaN()) { *other } _ if other.is_NaN() => *other,
(*self < *other) { *self } _ if *self < *other => *self,
_ { *other } _ => *other,
) }
} }
/// Returns `NaN` if either of the numbers are `NaN`. /// Returns `NaN` if either of the numbers are `NaN`.
#[inline] #[inline]
fn max(&self, other: &f64) -> f64 { fn max(&self, other: &f64) -> f64 {
cond!( match () {
(self.is_NaN()) { *self } _ if self.is_NaN() => *self,
(other.is_NaN()) { *other } _ if other.is_NaN() => *other,
(*self > *other) { *self } _ if *self > *other => *self,
_ { *other } _ => *other,
) }
} }
/// Returns the number constrained within the range `mn <= self <= mx`. /// Returns the number constrained within the range `mn <= self <= mx`.
/// If any of the numbers are `NaN` then `NaN` is returned. /// If any of the numbers are `NaN` then `NaN` is returned.
#[inline] #[inline]
fn clamp(&self, mn: &f64, mx: &f64) -> f64 { fn clamp(&self, mn: &f64, mx: &f64) -> f64 {
cond!( match () {
(self.is_NaN()) { *self } _ if self.is_NaN() => *self,
(!(*self <= *mx)) { *mx } _ if !(*self <= *mx) => *mx,
(!(*self >= *mn)) { *mn } _ if !(*self >= *mn) => *mn,
_ { *self } _ => *self,
) }
} }
} }

View File

@ -70,11 +70,11 @@ impl Orderable for $T {
/// Returns the number constrained within the range `mn <= self <= mx`. /// Returns the number constrained within the range `mn <= self <= mx`.
#[inline] #[inline]
fn clamp(&self, mn: &$T, mx: &$T) -> $T { fn clamp(&self, mn: &$T, mx: &$T) -> $T {
cond!( match () {
(*self > *mx) { *mx } _ if (*self > *mx) => *mx,
(*self < *mn) { *mn } _ if (*self < *mn) => *mn,
_ { *self } _ => *self,
) }
} }
} }

View File

@ -894,42 +894,6 @@ pub fn std_macros() -> @str {
} }
) )
//
// A scheme-style conditional that helps to improve code clarity in some instances when
// the `if`, `else if`, and `else` keywords obscure predicates undesirably.
//
// # Example
//
// ~~~
// let clamped =
// if x > mx { mx }
// else if x < mn { mn }
// else { x };
// ~~~
//
// Using `cond!`, the above could be written as:
//
// ~~~
// let clamped = cond!(
// (x > mx) { mx }
// (x < mn) { mn }
// _ { x }
// );
// ~~~
//
// The optional default case is denoted by `_`.
//
macro_rules! cond (
( $(($pred:expr) $body:block)+ _ $default:block ) => (
$(if $pred $body else)+
$default
);
// for if the default case was ommitted
( $(($pred:expr) $body:block)+ ) => (
$(if $pred $body)else+
);
)
// NOTE(acrichto): start removing this after the next snapshot // NOTE(acrichto): start removing this after the next snapshot
macro_rules! printf ( macro_rules! printf (
($arg:expr) => ( ($arg:expr) => (

View File

@ -1,23 +0,0 @@
// Copyright 2013 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.
fn clamp<T:Ord + Signed>(x: T, mn: T, mx: T) -> T {
cond!(
(x > mx) { return mx; }
(x < mn) { return mn; }
)
return x;
}
fn main() {
assert_eq!(clamp(1, 2, 4), 2);
assert_eq!(clamp(8, 2, 4), 4);
assert_eq!(clamp(3, 2, 4), 3);
}

View File

@ -1,23 +0,0 @@
// Copyright 2013 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.
fn clamp<T:Ord + Signed>(x: T, mn: T, mx: T) -> T {
cond!(
(x > mx) { mx }
(x < mn) { mn }
_ { x }
)
}
fn main() {
assert_eq!(clamp(1, 2, 4), 2);
assert_eq!(clamp(8, 2, 4), 4);
assert_eq!(clamp(3, 2, 4), 3);
}