Miscellaneous helpers for common patterns.
Void
- A type with no inhabitantsNonCopyable
- A non-copyable dummy type.of ::std::cmp::Eq for NonCopyable
- Automatically derived.of ::std::cmp::TotalEq for NonCopyable
- Automatically derived.of ::std::cmp::Ord for NonCopyable
- Automatically derived.of ::std::cmp::TotalOrd for NonCopyable
- Automatically derived.of Drop for NonCopyable
for Void
id
- The identity function.ignore
- Ignores a value.replace
swap
- Swap the values at two mutable locations of the same type, without deinitialising or copying either one.unreachable
- A utility function for indicating unreachable codewith
- Sets *ptr
to new_value
, invokes op()
, and then restores the original value of *ptr
.Void
A type with no inhabitants
NonCopyable
pub struct NonCopyable;
A non-copyable dummy type.
::std::cmp::Eq
for NonCopyable
Automatically derived.
eq
fn eq(&self, __arg_0: &NonCopyable) -> ::bool
ne
fn ne(&self, __arg_0: &NonCopyable) -> ::bool
::std::cmp::TotalEq
for NonCopyable
Automatically derived.
equals
fn equals(&self, __arg_0: &NonCopyable) -> ::bool
::std::cmp::Ord
for NonCopyable
Automatically derived.
lt
fn lt(&self, __arg_0: &NonCopyable) -> ::bool
le
fn le(&self, __arg_0: &NonCopyable) -> ::bool
gt
fn gt(&self, __arg_0: &NonCopyable) -> ::bool
ge
fn ge(&self, __arg_0: &NonCopyable) -> ::bool
::std::cmp::TotalOrd
for NonCopyable
Automatically derived.
cmp
fn cmp(&self, __arg_0: &NonCopyable) -> ::std::cmp::Ordering
Drop
for NonCopyable
drop
fn drop(&self)
Void
uninhabited
fn uninhabited(self) -> !
A utility function for ignoring this uninhabited type
id
fn id<T>(x: T) -> T
The identity function.
ignore
fn ignore<T>(_x: T)
Ignores a value.
replace
fn replace<T>(dest: &mut T, mut src: T) -> T
Replace the value at a mutable location with a new one, returning the old value, without deinitialising or copying either one.
swap
fn swap<T>(x: &mut T, y: &mut T)
Swap the values at two mutable locations of the same type, without deinitialising or copying either one.
unreachable
fn unreachable() -> !
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.
fn choose_weighted_item(v: &[Item]) -> Item {
assert!(!v.is_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();
}
with
fn with<T, R>(ptr: @mut T, value: T, op: &fn() -> R) -> R
Sets *ptr
to new_value
, invokes op()
, and then restores the original value of *ptr
.
NB: This function accepts @mut T
and not &mut T
to avoid an obvious borrowck hazard. Typically passing in &mut T
will cause borrow check errors because it freezes whatever location that &mut T
is stored in (either statically or dynamically).