2014-05-01 12:47:18 -05:00
|
|
|
// Copyright 2014 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.
|
|
|
|
|
|
|
|
#![macro_escape]
|
|
|
|
|
|
|
|
/// Entry point of failure, for details, see std::macros
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! fail(
|
|
|
|
() => (
|
2014-05-10 15:47:46 -05:00
|
|
|
fail!("{}", "explicit failure")
|
2014-05-01 12:47:18 -05:00
|
|
|
);
|
|
|
|
($msg:expr) => (
|
2014-05-10 15:47:46 -05:00
|
|
|
fail!("{}", $msg)
|
2014-05-01 12:47:18 -05:00
|
|
|
);
|
2014-05-10 15:47:46 -05:00
|
|
|
($fmt:expr, $($arg:tt)*) => ({
|
|
|
|
// a closure can't have return type !, so we need a full
|
|
|
|
// function to pass to format_args!, *and* we need the
|
|
|
|
// file and line numbers right here; so an inner bare fn
|
|
|
|
// is our only choice.
|
|
|
|
//
|
|
|
|
// LLVM doesn't tend to inline this, presumably because begin_unwind_fmt
|
|
|
|
// is #[cold] and #[inline(never)] and because this is flagged as cold
|
|
|
|
// as returning !. We really do want this to be inlined, however,
|
|
|
|
// because it's just a tiny wrapper. Small wins (156K to 149K in size)
|
|
|
|
// were seen when forcing this to be inlined, and that number just goes
|
|
|
|
// up with the number of calls to fail!()
|
|
|
|
#[inline(always)]
|
|
|
|
fn run_fmt(fmt: &::std::fmt::Arguments) -> ! {
|
|
|
|
::core::failure::begin_unwind(fmt, file!(), line!())
|
|
|
|
}
|
|
|
|
format_args!(run_fmt, $fmt, $($arg)*)
|
|
|
|
});
|
2014-05-01 12:47:18 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
/// Runtime assertion, for details see std::macros
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! assert(
|
|
|
|
($cond:expr) => (
|
|
|
|
if !$cond {
|
|
|
|
fail!(concat!("assertion failed: ", stringify!($cond)))
|
|
|
|
}
|
|
|
|
);
|
2014-05-10 15:47:46 -05:00
|
|
|
($cond:expr, $($arg:tt)*) => (
|
|
|
|
if !$cond {
|
|
|
|
fail!($($arg)*)
|
|
|
|
}
|
|
|
|
);
|
2014-05-01 12:47:18 -05:00
|
|
|
)
|
|
|
|
|
std: Recreate a `collections` module
As with the previous commit with `librand`, this commit shuffles around some
`collections` code. The new state of the world is similar to that of librand:
* The libcollections crate now only depends on libcore and liballoc.
* The standard library has a new module, `std::collections`. All functionality
of libcollections is reexported through this module.
I would like to stress that this change is purely cosmetic. There are very few
alterations to these primitives.
There are a number of notable points about the new organization:
* std::{str, slice, string, vec} all moved to libcollections. There is no reason
that these primitives shouldn't be necessarily usable in a freestanding
context that has allocation. These are all reexported in their usual places in
the standard library.
* The `hashmap`, and transitively the `lru_cache`, modules no longer reside in
`libcollections`, but rather in libstd. The reason for this is because the
`HashMap::new` contructor requires access to the OSRng for initially seeding
the hash map. Beyond this requirement, there is no reason that the hashmap
could not move to libcollections.
I do, however, have a plan to move the hash map to the collections module. The
`HashMap::new` function could be altered to require that the `H` hasher
parameter ascribe to the `Default` trait, allowing the entire `hashmap` module
to live in libcollections. The key idea would be that the default hasher would
be different in libstd. Something along the lines of:
// src/libstd/collections/mod.rs
pub type HashMap<K, V, H = RandomizedSipHasher> =
core_collections::HashMap<K, V, H>;
This is not possible today because you cannot invoke static methods through
type aliases. If we modified the compiler, however, to allow invocation of
static methods through type aliases, then this type definition would
essentially be switching the default hasher from `SipHasher` in libcollections
to a libstd-defined `RandomizedSipHasher` type. This type's `Default`
implementation would randomly seed the `SipHasher` instance, and otherwise
perform the same as `SipHasher`.
This future state doesn't seem incredibly far off, but until that time comes,
the hashmap module will live in libstd to not compromise on functionality.
* In preparation for the hashmap moving to libcollections, the `hash` module has
moved from libstd to libcollections. A previously snapshotted commit enables a
distinct `Writer` trait to live in the `hash` module which `Hash`
implementations are now parameterized over.
Due to using a custom trait, the `SipHasher` implementation has lost its
specialized methods for writing integers. These can be re-added
backwards-compatibly in the future via default methods if necessary, but the
FNV hashing should satisfy much of the need for speedier hashing.
A list of breaking changes:
* HashMap::{get, get_mut} no longer fails with the key formatted into the error
message with `{:?}`, instead, a generic message is printed. With backtraces,
it should still be not-too-hard to track down errors.
* The HashMap, HashSet, and LruCache types are now available through
std::collections instead of the collections crate.
* Manual implementations of hash should be parameterized over `hash::Writer`
instead of just `Writer`.
[breaking-change]
2014-05-29 20:50:12 -05:00
|
|
|
/// Runtime assertion, only without `--cfg ndebug`
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! debug_assert(
|
|
|
|
($(a:tt)*) => ({
|
|
|
|
if cfg!(not(ndebug)) {
|
|
|
|
assert!($($a)*);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
2014-05-11 13:14:14 -05:00
|
|
|
/// Runtime assertion for equality, for details see std::macros
|
std: Recreate a `collections` module
As with the previous commit with `librand`, this commit shuffles around some
`collections` code. The new state of the world is similar to that of librand:
* The libcollections crate now only depends on libcore and liballoc.
* The standard library has a new module, `std::collections`. All functionality
of libcollections is reexported through this module.
I would like to stress that this change is purely cosmetic. There are very few
alterations to these primitives.
There are a number of notable points about the new organization:
* std::{str, slice, string, vec} all moved to libcollections. There is no reason
that these primitives shouldn't be necessarily usable in a freestanding
context that has allocation. These are all reexported in their usual places in
the standard library.
* The `hashmap`, and transitively the `lru_cache`, modules no longer reside in
`libcollections`, but rather in libstd. The reason for this is because the
`HashMap::new` contructor requires access to the OSRng for initially seeding
the hash map. Beyond this requirement, there is no reason that the hashmap
could not move to libcollections.
I do, however, have a plan to move the hash map to the collections module. The
`HashMap::new` function could be altered to require that the `H` hasher
parameter ascribe to the `Default` trait, allowing the entire `hashmap` module
to live in libcollections. The key idea would be that the default hasher would
be different in libstd. Something along the lines of:
// src/libstd/collections/mod.rs
pub type HashMap<K, V, H = RandomizedSipHasher> =
core_collections::HashMap<K, V, H>;
This is not possible today because you cannot invoke static methods through
type aliases. If we modified the compiler, however, to allow invocation of
static methods through type aliases, then this type definition would
essentially be switching the default hasher from `SipHasher` in libcollections
to a libstd-defined `RandomizedSipHasher` type. This type's `Default`
implementation would randomly seed the `SipHasher` instance, and otherwise
perform the same as `SipHasher`.
This future state doesn't seem incredibly far off, but until that time comes,
the hashmap module will live in libstd to not compromise on functionality.
* In preparation for the hashmap moving to libcollections, the `hash` module has
moved from libstd to libcollections. A previously snapshotted commit enables a
distinct `Writer` trait to live in the `hash` module which `Hash`
implementations are now parameterized over.
Due to using a custom trait, the `SipHasher` implementation has lost its
specialized methods for writing integers. These can be re-added
backwards-compatibly in the future via default methods if necessary, but the
FNV hashing should satisfy much of the need for speedier hashing.
A list of breaking changes:
* HashMap::{get, get_mut} no longer fails with the key formatted into the error
message with `{:?}`, instead, a generic message is printed. With backtraces,
it should still be not-too-hard to track down errors.
* The HashMap, HashSet, and LruCache types are now available through
std::collections instead of the collections crate.
* Manual implementations of hash should be parameterized over `hash::Writer`
instead of just `Writer`.
[breaking-change]
2014-05-29 20:50:12 -05:00
|
|
|
#[macro_export]
|
2014-05-11 13:14:14 -05:00
|
|
|
macro_rules! assert_eq(
|
|
|
|
($cond1:expr, $cond2:expr) => ({
|
|
|
|
let c1 = $cond1;
|
|
|
|
let c2 = $cond2;
|
|
|
|
if c1 != c2 || c2 != c1 {
|
|
|
|
fail!("expressions not equal, left: {}, right: {}", c1, c2);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
std: Recreate a `collections` module
As with the previous commit with `librand`, this commit shuffles around some
`collections` code. The new state of the world is similar to that of librand:
* The libcollections crate now only depends on libcore and liballoc.
* The standard library has a new module, `std::collections`. All functionality
of libcollections is reexported through this module.
I would like to stress that this change is purely cosmetic. There are very few
alterations to these primitives.
There are a number of notable points about the new organization:
* std::{str, slice, string, vec} all moved to libcollections. There is no reason
that these primitives shouldn't be necessarily usable in a freestanding
context that has allocation. These are all reexported in their usual places in
the standard library.
* The `hashmap`, and transitively the `lru_cache`, modules no longer reside in
`libcollections`, but rather in libstd. The reason for this is because the
`HashMap::new` contructor requires access to the OSRng for initially seeding
the hash map. Beyond this requirement, there is no reason that the hashmap
could not move to libcollections.
I do, however, have a plan to move the hash map to the collections module. The
`HashMap::new` function could be altered to require that the `H` hasher
parameter ascribe to the `Default` trait, allowing the entire `hashmap` module
to live in libcollections. The key idea would be that the default hasher would
be different in libstd. Something along the lines of:
// src/libstd/collections/mod.rs
pub type HashMap<K, V, H = RandomizedSipHasher> =
core_collections::HashMap<K, V, H>;
This is not possible today because you cannot invoke static methods through
type aliases. If we modified the compiler, however, to allow invocation of
static methods through type aliases, then this type definition would
essentially be switching the default hasher from `SipHasher` in libcollections
to a libstd-defined `RandomizedSipHasher` type. This type's `Default`
implementation would randomly seed the `SipHasher` instance, and otherwise
perform the same as `SipHasher`.
This future state doesn't seem incredibly far off, but until that time comes,
the hashmap module will live in libstd to not compromise on functionality.
* In preparation for the hashmap moving to libcollections, the `hash` module has
moved from libstd to libcollections. A previously snapshotted commit enables a
distinct `Writer` trait to live in the `hash` module which `Hash`
implementations are now parameterized over.
Due to using a custom trait, the `SipHasher` implementation has lost its
specialized methods for writing integers. These can be re-added
backwards-compatibly in the future via default methods if necessary, but the
FNV hashing should satisfy much of the need for speedier hashing.
A list of breaking changes:
* HashMap::{get, get_mut} no longer fails with the key formatted into the error
message with `{:?}`, instead, a generic message is printed. With backtraces,
it should still be not-too-hard to track down errors.
* The HashMap, HashSet, and LruCache types are now available through
std::collections instead of the collections crate.
* Manual implementations of hash should be parameterized over `hash::Writer`
instead of just `Writer`.
[breaking-change]
2014-05-29 20:50:12 -05:00
|
|
|
/// Runtime assertion for equality, only without `--cfg ndebug`
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! debug_assert_eq(
|
|
|
|
($($a:tt)*) => ({
|
|
|
|
if cfg!(not(ndebug)) {
|
|
|
|
assert_eq!($($a)*);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
2014-05-01 12:47:18 -05:00
|
|
|
/// Runtime assertion, disableable at compile time
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! debug_assert(
|
|
|
|
($($arg:tt)*) => (if cfg!(not(ndebug)) { assert!($($arg)*); })
|
|
|
|
)
|
2014-05-10 15:48:26 -05:00
|
|
|
|
|
|
|
/// Short circuiting evaluation on Err
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! try(
|
|
|
|
($e:expr) => (match $e { Ok(e) => e, Err(e) => return Err(e) })
|
|
|
|
)
|
2014-05-11 13:14:14 -05:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
macro_rules! vec( ($($e:expr),*) => ({
|
|
|
|
let mut _v = ::std::vec::Vec::new();
|
|
|
|
$(_v.push($e);)*
|
|
|
|
_v
|
|
|
|
}) )
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
macro_rules! format( ($($arg:tt)*) => (format_args!(::fmt::format, $($arg)*)) )
|
std: Recreate a `collections` module
As with the previous commit with `librand`, this commit shuffles around some
`collections` code. The new state of the world is similar to that of librand:
* The libcollections crate now only depends on libcore and liballoc.
* The standard library has a new module, `std::collections`. All functionality
of libcollections is reexported through this module.
I would like to stress that this change is purely cosmetic. There are very few
alterations to these primitives.
There are a number of notable points about the new organization:
* std::{str, slice, string, vec} all moved to libcollections. There is no reason
that these primitives shouldn't be necessarily usable in a freestanding
context that has allocation. These are all reexported in their usual places in
the standard library.
* The `hashmap`, and transitively the `lru_cache`, modules no longer reside in
`libcollections`, but rather in libstd. The reason for this is because the
`HashMap::new` contructor requires access to the OSRng for initially seeding
the hash map. Beyond this requirement, there is no reason that the hashmap
could not move to libcollections.
I do, however, have a plan to move the hash map to the collections module. The
`HashMap::new` function could be altered to require that the `H` hasher
parameter ascribe to the `Default` trait, allowing the entire `hashmap` module
to live in libcollections. The key idea would be that the default hasher would
be different in libstd. Something along the lines of:
// src/libstd/collections/mod.rs
pub type HashMap<K, V, H = RandomizedSipHasher> =
core_collections::HashMap<K, V, H>;
This is not possible today because you cannot invoke static methods through
type aliases. If we modified the compiler, however, to allow invocation of
static methods through type aliases, then this type definition would
essentially be switching the default hasher from `SipHasher` in libcollections
to a libstd-defined `RandomizedSipHasher` type. This type's `Default`
implementation would randomly seed the `SipHasher` instance, and otherwise
perform the same as `SipHasher`.
This future state doesn't seem incredibly far off, but until that time comes,
the hashmap module will live in libstd to not compromise on functionality.
* In preparation for the hashmap moving to libcollections, the `hash` module has
moved from libstd to libcollections. A previously snapshotted commit enables a
distinct `Writer` trait to live in the `hash` module which `Hash`
implementations are now parameterized over.
Due to using a custom trait, the `SipHasher` implementation has lost its
specialized methods for writing integers. These can be re-added
backwards-compatibly in the future via default methods if necessary, but the
FNV hashing should satisfy much of the need for speedier hashing.
A list of breaking changes:
* HashMap::{get, get_mut} no longer fails with the key formatted into the error
message with `{:?}`, instead, a generic message is printed. With backtraces,
it should still be not-too-hard to track down errors.
* The HashMap, HashSet, and LruCache types are now available through
std::collections instead of the collections crate.
* Manual implementations of hash should be parameterized over `hash::Writer`
instead of just `Writer`.
[breaking-change]
2014-05-29 20:50:12 -05:00
|
|
|
|
|
|
|
/// Write some formatted data into a stream.
|
|
|
|
///
|
|
|
|
/// Identical to the macro in `std::macros`
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! write(
|
|
|
|
($dst:expr, $($arg:tt)*) => ({
|
|
|
|
format_args_method!($dst, write_fmt, $($arg)*)
|
|
|
|
})
|
|
|
|
)
|