From 47576313695170013cfe07d57e81c2e879a14c14 Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Wed, 18 Sep 2013 07:21:57 -0400 Subject: [PATCH] Remove and replace cond! Closes #9282. --- src/libextra/num/bigint.rs | 4 +-- src/libstd/char.rs | 24 +++++++-------- src/libstd/num/f32.rs | 36 +++++++++++----------- src/libstd/num/f64.rs | 36 +++++++++++----------- src/libstd/num/uint_macros.rs | 10 +++--- src/libsyntax/ext/expand.rs | 36 ---------------------- src/test/run-pass/cond-macro-no-default.rs | 23 -------------- src/test/run-pass/cond-macro.rs | 23 -------------- 8 files changed, 55 insertions(+), 137 deletions(-) delete mode 100644 src/test/run-pass/cond-macro-no-default.rs delete mode 100644 src/test/run-pass/cond-macro.rs diff --git a/src/libextra/num/bigint.rs b/src/libextra/num/bigint.rs index 039694f5881..936efed94e4 100644 --- a/src/libextra/num/bigint.rs +++ b/src/libextra/num/bigint.rs @@ -115,8 +115,8 @@ impl TotalOrd for BigUint { if s_len > o_len { return Greater; } for (&self_i, &other_i) in self.data.rev_iter().zip(other.data.rev_iter()) { - cond!((self_i < other_i) { return Less; } - (self_i > other_i) { return Greater; }) + if self_i < other_i { return Less; } + if self_i > other_i { return Greater; } } return Equal; } diff --git a/src/libstd/char.rs b/src/libstd/char.rs index 911d883f88a..431fc27a202 100644 --- a/src/libstd/char.rs +++ b/src/libstd/char.rs @@ -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 // here. f('\\'); - let pad = cond!( - (c <= '\xff') { f('x'); 2 } - (c <= '\uffff') { f('u'); 4 } - _ { f('U'); 8 } - ); + let pad = match () { + _ if c <= '\xff' => { f('x'); 2 } + _ if c <= '\uffff' => { f('u'); 4 } + _ => { f('U'); 8 } + }; for offset in range_step::(4 * (pad - 1), -1, -4) { unsafe { match ((c as i32) >> offset) & 0xf { @@ -329,13 +329,13 @@ pub fn len_utf8_bytes(c: char) -> uint { static MAX_FOUR_B: uint = 2097152u; let code = c as uint; - cond!( - (code < MAX_ONE_B) { 1u } - (code < MAX_TWO_B) { 2u } - (code < MAX_THREE_B) { 3u } - (code < MAX_FOUR_B) { 4u } - _ { fail!("invalid character!") } - ) + match () { + _ if code < MAX_ONE_B => 1u, + _ if code < MAX_TWO_B => 2u, + _ if code < MAX_THREE_B => 3u, + _ if code < MAX_FOUR_B => 4u, + _ => fail!("invalid character!"), + } } impl ToStr for char { diff --git a/src/libstd/num/f32.rs b/src/libstd/num/f32.rs index 0addcce3eb6..c2babdf92db 100644 --- a/src/libstd/num/f32.rs +++ b/src/libstd/num/f32.rs @@ -206,35 +206,35 @@ impl Orderable for f32 { /// Returns `NaN` if either of the numbers are `NaN`. #[inline] fn min(&self, other: &f32) -> f32 { - cond!( - (self.is_NaN()) { *self } - (other.is_NaN()) { *other } - (*self < *other) { *self } - _ { *other } - ) + match () { + _ if self.is_NaN() => *self, + _ if other.is_NaN() => *other, + _ if *self < *other => *self, + _ => *other, + } } /// Returns `NaN` if either of the numbers are `NaN`. #[inline] fn max(&self, other: &f32) -> f32 { - cond!( - (self.is_NaN()) { *self } - (other.is_NaN()) { *other } - (*self > *other) { *self } - _ { *other } - ) + match () { + _ if self.is_NaN() => *self, + _ if other.is_NaN() => *other, + _ if *self > *other => *self, + _ => *other, + } } /// Returns the number constrained within the range `mn <= self <= mx`. /// If any of the numbers are `NaN` then `NaN` is returned. #[inline] fn clamp(&self, mn: &f32, mx: &f32) -> f32 { - cond!( - (self.is_NaN()) { *self } - (!(*self <= *mx)) { *mx } - (!(*self >= *mn)) { *mn } - _ { *self } - ) + match () { + _ if self.is_NaN() => *self, + _ if !(*self <= *mx) => *mx, + _ if !(*self >= *mn) => *mn, + _ => *self, + } } } diff --git a/src/libstd/num/f64.rs b/src/libstd/num/f64.rs index b0675278238..5b2f3ac119b 100644 --- a/src/libstd/num/f64.rs +++ b/src/libstd/num/f64.rs @@ -229,35 +229,35 @@ impl Orderable for f64 { /// Returns `NaN` if either of the numbers are `NaN`. #[inline] fn min(&self, other: &f64) -> f64 { - cond!( - (self.is_NaN()) { *self } - (other.is_NaN()) { *other } - (*self < *other) { *self } - _ { *other } - ) + match () { + _ if self.is_NaN() => *self, + _ if other.is_NaN() => *other, + _ if *self < *other => *self, + _ => *other, + } } /// Returns `NaN` if either of the numbers are `NaN`. #[inline] fn max(&self, other: &f64) -> f64 { - cond!( - (self.is_NaN()) { *self } - (other.is_NaN()) { *other } - (*self > *other) { *self } - _ { *other } - ) + match () { + _ if self.is_NaN() => *self, + _ if other.is_NaN() => *other, + _ if *self > *other => *self, + _ => *other, + } } /// Returns the number constrained within the range `mn <= self <= mx`. /// If any of the numbers are `NaN` then `NaN` is returned. #[inline] fn clamp(&self, mn: &f64, mx: &f64) -> f64 { - cond!( - (self.is_NaN()) { *self } - (!(*self <= *mx)) { *mx } - (!(*self >= *mn)) { *mn } - _ { *self } - ) + match () { + _ if self.is_NaN() => *self, + _ if !(*self <= *mx) => *mx, + _ if !(*self >= *mn) => *mn, + _ => *self, + } } } diff --git a/src/libstd/num/uint_macros.rs b/src/libstd/num/uint_macros.rs index 0a9c912a6e2..7cd1be7ab74 100644 --- a/src/libstd/num/uint_macros.rs +++ b/src/libstd/num/uint_macros.rs @@ -70,11 +70,11 @@ impl Orderable for $T { /// Returns the number constrained within the range `mn <= self <= mx`. #[inline] fn clamp(&self, mn: &$T, mx: &$T) -> $T { - cond!( - (*self > *mx) { *mx } - (*self < *mn) { *mn } - _ { *self } - ) + match () { + _ if (*self > *mx) => *mx, + _ if (*self < *mn) => *mn, + _ => *self, + } } } diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index ac094c27a81..5163642e484 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -898,42 +898,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 macro_rules! printf ( ($arg:expr) => ( diff --git a/src/test/run-pass/cond-macro-no-default.rs b/src/test/run-pass/cond-macro-no-default.rs deleted file mode 100644 index 8bd1a772e55..00000000000 --- a/src/test/run-pass/cond-macro-no-default.rs +++ /dev/null @@ -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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -fn clamp(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); -} diff --git a/src/test/run-pass/cond-macro.rs b/src/test/run-pass/cond-macro.rs deleted file mode 100644 index 61a51b67261..00000000000 --- a/src/test/run-pass/cond-macro.rs +++ /dev/null @@ -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 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -fn clamp(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); -}