提交 47576313 编写于 作者: J Jimmy Zelinskie

Remove and replace cond! Closes #9282.

上级 adb638f5
......@@ -115,8 +115,8 @@ fn cmp(&self, other: &BigUint) -> Ordering {
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;
}
......
......@@ -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::<i32>(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 {
......
......@@ -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,
}
}
}
......
......@@ -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,
}
}
}
......
......@@ -70,11 +70,11 @@ fn max(&self, other: &$T) -> $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,
}
}
}
......
......@@ -898,42 +898,6 @@ mod $c {
}
)
//
// 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) => (
......
// 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);
}
// 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);
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册