Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from kmcallister/h5e-macros
Add some macros from html5ever
- Loading branch information
Showing
5 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
//! Macros for string formatting. | ||
|
||
/// Conditionally perform string formatting. | ||
/// | ||
/// If `$enabled` is true, then do the formatting and return a `Cow::Owned`. | ||
/// | ||
/// Otherwise, just return the borrowed (often `'static`) string | ||
/// `$borrowed`. | ||
/// | ||
/// When `$enabled` is false, this avoids the overhead of allocating | ||
/// and writing to a buffer, as well as any overhead or side effects | ||
/// of the format arguments. | ||
/// | ||
/// # Example | ||
/// | ||
/// You can use `format_if` to implement a detailed error logging facility | ||
/// that can be enabled at runtime. | ||
/// | ||
/// ``` | ||
/// # #[macro_use] extern crate mac; | ||
/// # fn main() { | ||
/// let formatted = format_if!(true, "Vague error", "Error code {:?}", 3); | ||
/// | ||
/// assert_eq!(&formatted[..], "Error code 3"); | ||
/// assert!(formatted.is_owned()); | ||
/// | ||
/// let not_formatted = format_if!(false, "Vague error", "Error code {:?}", { | ||
/// // Note that the argument is not evaluated. | ||
/// panic!("oops"); | ||
/// }); | ||
/// | ||
/// assert_eq!(¬_formatted[..], "Vague error"); | ||
/// assert!(not_formatted.is_borrowed()) | ||
/// # } | ||
/// ``` | ||
#[macro_export] | ||
macro_rules! format_if { | ||
($enabled:expr, $borrowed:expr, $fmt:expr, $($args:expr),*) => { | ||
if $enabled { | ||
::std::borrow::Cow::Owned(format!($fmt, $($args),*)) | ||
} else { | ||
::std::borrow::Cow::Borrowed($borrowed) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//! Macros for low-level memory manipulation. | ||
|
||
/// Make a tuple of the addresses of some of a struct's fields. | ||
/// | ||
/// This is useful when you are transmuting between struct types | ||
/// and would like an additional dynamic check that the layouts | ||
/// match. It's difficult to make such an assertion statically | ||
/// in Rust at present. | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// # #[macro_use] extern crate mac; | ||
/// use std::mem; | ||
/// | ||
/// # fn main() { | ||
/// struct Foo { x: i32, y: i32 } | ||
/// struct Bar { x: u32, y: u32 } | ||
/// | ||
/// let foo = Foo { x: 3, y: 4 }; | ||
/// let old_addrs = addrs_of!(foo => x, y); | ||
/// | ||
/// let bar = unsafe { | ||
/// mem::transmute::<&Foo, &Bar>(&foo) | ||
/// }; | ||
/// let new_addrs = addrs_of!(bar => x, y); | ||
/// assert_eq!(old_addrs, new_addrs); | ||
/// | ||
/// assert_eq!(bar.x, 3); | ||
/// assert_eq!(bar.y, 4); | ||
/// # } | ||
/// ``` | ||
#[macro_export] | ||
macro_rules! addrs_of { | ||
($obj:expr => $($field:ident),+) => { | ||
( // make a tuple | ||
$( | ||
unsafe { | ||
::std::mem::transmute::<_, usize>(&$obj.$field) | ||
} | ||
),+ | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//! Macros useful when writing procedural syntax extensions. | ||
//! | ||
//! The macros themselves are ordinary `macro_rules!` macros. | ||
|
||
/// Call `span_err` on an `ExtCtxt` and return `DummyResult::any`. | ||
#[macro_export] | ||
macro_rules! ext_bail { | ||
($cx:expr, $sp:expr, $msg:expr) => {{ | ||
$cx.span_err($sp, $msg); | ||
return ::syntax::ext::base::DummyResult::any($sp); | ||
}} | ||
} | ||
|
||
/// `ext_bail!` if the condition `$e` is true. | ||
#[macro_export] | ||
macro_rules! ext_bail_if { | ||
($e:expr, $cx:expr, $sp:expr, $msg:expr) => {{ | ||
if $e { ext_bail!($cx, $sp, $msg) } | ||
}} | ||
} | ||
|
||
/// Unwrap the `Option` `$e`, or `ext_bail!`. | ||
#[macro_export] | ||
macro_rules! ext_expect { | ||
($cx:expr, $sp:expr, $e:expr, $msg:expr) => {{ | ||
match $e { | ||
Some(x) => x, | ||
None => ext_bail!($cx, $sp, $msg), | ||
} | ||
}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//! Macros for writing test suites. | ||
|
||
/// Generate a test function `$name` which asserts that `$left` and `$right` | ||
/// are equal. | ||
/// | ||
/// # Example | ||
/// | ||
/// ``` | ||
/// # #[macro_use] extern crate mac; | ||
/// mod test { | ||
/// # // doesn't actually run the test :/ | ||
/// test_eq!(two_and_two_is_four, 2 + 2, 4); | ||
/// } | ||
/// # fn main() { } | ||
/// ``` | ||
#[macro_export] | ||
macro_rules! test_eq { | ||
($name:ident, $left:expr, $right:expr) => { | ||
#[test] | ||
fn $name() { | ||
assert_eq!($left, $right); | ||
} | ||
} | ||
} |