rust/src/libcore/util.rs

121 lines
2.7 KiB
Rust
Raw Normal View History

/*!
Miscellaneous helpers for common patterns.
*/
2012-08-14 14:11:15 -05:00
// NB: transitionary, de-mode-ing.
// tjc: re-forbid deprecated modes after snapshot
2012-08-14 14:11:15 -05:00
#[forbid(deprecated_pattern)];
2012-08-30 14:15:53 -05:00
use cmp::Eq;
2012-08-02 13:26:52 -05:00
/// The identity function.
2012-09-11 17:57:32 -05:00
#[inline(always)]
pub pure fn id<T>(x: T) -> T { move 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)]
pub pure fn ignore<T>(_x: T) { }
2012-08-07 13:16:51 -05:00
/// Sets `*ptr` to `new_value`, invokes `op()`, and then restores the
/// original value of `*ptr`.
#[inline(always)]
pub fn with<T: Copy, R>(
ptr: &mut T,
new_value: T,
op: &fn() -> R) -> R
{
// NDM: if swap operator were defined somewhat differently,
// we wouldn't need to copy...
let old_value = *ptr;
*ptr = move new_value;
let result = op();
*ptr = move old_value;
return move result;
}
/**
* Swap the values at two mutable locations of the same type, without
* deinitialising or copying either one.
*/
#[inline(always)]
pub fn swap<T>(x: &mut T, y: &mut T) {
*x <-> *y;
}
/**
* Replace the value at a mutable location with a new one, returning the old
* value, without deinitialising or copying either one.
*/
#[inline(always)]
pub fn replace<T>(dest: &mut T, src: T) -> T {
2012-09-10 14:14:14 -05:00
let mut tmp <- src;
swap(dest, &mut tmp);
2012-09-10 14:14:14 -05:00
move tmp
}
/// A non-copyable dummy type.
pub struct NonCopyable {
i: (),
drop { }
}
pub fn NonCopyable() -> NonCopyable { NonCopyable { i: () } }
2012-09-04 17:23:28 -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 {
assert v.is_not_empty();
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();
}
~~~
*/
pub fn unreachable() -> ! {
fail ~"internal error: entered unreachable code";
}
mod tests {
#[legacy_exports];
2012-08-02 13:26:52 -05:00
#[test]
fn identity_crisis() {
// 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)];
//FIXME #3387 assert x.eq(id(copy x));
let y = copy x;
assert x.eq(&id(y));
2012-08-02 13:26:52 -05:00
}
#[test]
fn test_swap() {
let mut x = 31337;
let mut y = 42;
swap(&mut x, &mut y);
assert x == 42;
assert y == 31337;
}
#[test]
fn test_replace() {
2012-08-20 14:23:37 -05:00
let mut x = Some(NonCopyable());
let y = replace(&mut x, None);
assert x.is_none();
assert y.is_some();
}
}