2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 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.
|
|
|
|
|
2012-09-19 18:52:32 -05:00
|
|
|
/*!
|
|
|
|
|
|
|
|
Miscellaneous helpers for common patterns.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2013-01-08 21:37:25 -06:00
|
|
|
use prelude::*;
|
2012-08-30 14:15:53 -05:00
|
|
|
|
2012-08-02 13:26:52 -05:00
|
|
|
/// The identity function.
|
2012-09-11 17:57:32 -05:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn id<T>(x: T) -> T { x }
|
2012-08-02 13:26:52 -05:00
|
|
|
|
2012-08-07 13:16:51 -05:00
|
|
|
/// Ignores a value.
|
2012-09-11 17:57:32 -05:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn ignore<T>(_x: T) { }
|
2012-08-07 13:16:51 -05:00
|
|
|
|
2012-08-21 17:55:17 -05:00
|
|
|
/// Sets `*ptr` to `new_value`, invokes `op()`, and then restores the
|
|
|
|
/// original value of `*ptr`.
|
|
|
|
#[inline(always)]
|
2013-02-20 19:07:17 -06:00
|
|
|
pub fn with<T:Copy,R>(
|
2012-08-21 17:55:17 -05:00
|
|
|
ptr: &mut T,
|
2012-10-02 13:37:37 -05:00
|
|
|
new_value: T,
|
2012-08-21 17:55:17 -05:00
|
|
|
op: &fn() -> R) -> R
|
|
|
|
{
|
|
|
|
// NDM: if swap operator were defined somewhat differently,
|
|
|
|
// we wouldn't need to copy...
|
|
|
|
|
|
|
|
let old_value = *ptr;
|
2013-02-15 02:51:28 -06:00
|
|
|
*ptr = new_value;
|
2012-08-21 17:55:17 -05:00
|
|
|
let result = op();
|
2013-02-15 02:51:28 -06:00
|
|
|
*ptr = old_value;
|
|
|
|
return result;
|
2012-08-21 17:55:17 -05:00
|
|
|
}
|
|
|
|
|
2012-08-01 18:04:43 -05:00
|
|
|
/**
|
|
|
|
* Swap the values at two mutable locations of the same type, without
|
|
|
|
* deinitialising or copying either one.
|
|
|
|
*/
|
2012-08-02 13:40:42 -05:00
|
|
|
#[inline(always)]
|
2012-09-28 19:41:45 -05:00
|
|
|
pub fn swap<T>(x: &mut T, y: &mut T) {
|
2012-08-01 18:04:43 -05:00
|
|
|
*x <-> *y;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace the value at a mutable location with a new one, returning the old
|
|
|
|
* value, without deinitialising or copying either one.
|
|
|
|
*/
|
2012-08-02 13:40:42 -05:00
|
|
|
#[inline(always)]
|
2012-10-02 13:37:37 -05:00
|
|
|
pub fn replace<T>(dest: &mut T, src: T) -> T {
|
2013-02-15 02:51:28 -06:00
|
|
|
let mut tmp = src;
|
2012-08-01 18:04:43 -05:00
|
|
|
swap(dest, &mut tmp);
|
2013-02-15 02:51:28 -06:00
|
|
|
tmp
|
2012-08-01 18:04:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// A non-copyable dummy type.
|
2012-09-28 19:41:45 -05:00
|
|
|
pub struct NonCopyable {
|
2012-09-07 16:50:47 -05:00
|
|
|
i: (),
|
2013-02-27 18:13:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for NonCopyable {
|
|
|
|
fn finalize(&self) { }
|
2012-08-01 18:04:43 -05:00
|
|
|
}
|
|
|
|
|
2012-09-28 19:41:45 -05:00
|
|
|
pub fn NonCopyable() -> NonCopyable { NonCopyable { i: () } }
|
2012-09-04 17:23:28 -05:00
|
|
|
|
2012-09-20 16:27:25 -05:00
|
|
|
/**
|
|
|
|
A utility function for indicating unreachable code. It will fail if
|
|
|
|
executed. This is occasionally useful to put after loops that never
|
|
|
|
terminate normally, but instead directly return from a function.
|
|
|
|
|
|
|
|
# Example
|
|
|
|
|
|
|
|
~~~
|
|
|
|
fn choose_weighted_item(v: &[Item]) -> Item {
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(!v.is_empty());
|
2012-09-20 16:27:25 -05:00
|
|
|
let mut so_far = 0u;
|
|
|
|
for v.each |item| {
|
|
|
|
so_far += item.weight;
|
|
|
|
if so_far > 100 {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// The above loop always returns, so we must hint to the
|
|
|
|
// type checker that it isn't possible to get down here
|
|
|
|
util::unreachable();
|
|
|
|
}
|
|
|
|
~~~
|
|
|
|
|
|
|
|
*/
|
2012-09-28 19:41:45 -05:00
|
|
|
pub fn unreachable() -> ! {
|
2013-02-11 21:26:38 -06:00
|
|
|
fail!(~"internal error: entered unreachable code");
|
2012-09-20 16:27:25 -05:00
|
|
|
}
|
|
|
|
|
2013-02-26 23:10:03 -06:00
|
|
|
#[cfg(test)]
|
2012-08-01 18:04:43 -05:00
|
|
|
mod tests {
|
2013-02-28 10:57:33 -06:00
|
|
|
use option::{None, Some};
|
2013-01-08 21:37:25 -06:00
|
|
|
use util::{NonCopyable, id, replace, swap};
|
|
|
|
|
2012-08-02 13:26:52 -05:00
|
|
|
#[test]
|
2013-01-29 13:47:18 -06:00
|
|
|
pub fn identity_crisis() {
|
2012-08-02 13:26:52 -05:00
|
|
|
// Writing a test for the identity function. How did it come to this?
|
2012-08-27 18:26:35 -05:00
|
|
|
let x = ~[(5, false)];
|
2013-03-28 20:39:09 -05:00
|
|
|
//FIXME #3387 assert!(x.eq(id(copy x)));
|
2012-08-28 17:54:45 -05:00
|
|
|
let y = copy x;
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(x.eq(&id(y)));
|
2012-08-02 13:26:52 -05:00
|
|
|
}
|
2012-08-01 18:04:43 -05:00
|
|
|
#[test]
|
2013-01-29 13:47:18 -06:00
|
|
|
pub fn test_swap() {
|
2012-08-01 18:04:43 -05:00
|
|
|
let mut x = 31337;
|
|
|
|
let mut y = 42;
|
|
|
|
swap(&mut x, &mut y);
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(x == 42);
|
|
|
|
assert!(y == 31337);
|
2012-08-01 18:04:43 -05:00
|
|
|
}
|
|
|
|
#[test]
|
2013-01-29 13:47:18 -06:00
|
|
|
pub fn test_replace() {
|
2012-08-20 14:23:37 -05:00
|
|
|
let mut x = Some(NonCopyable());
|
|
|
|
let y = replace(&mut x, None);
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(x.is_none());
|
|
|
|
assert!(y.is_some());
|
2012-08-01 18:04:43 -05:00
|
|
|
}
|
|
|
|
}
|