New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add some macros from html5ever #5
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 { bail!($cx, $sp, $msg) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be |
||
}} | ||
} | ||
|
||
/// 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 => bail!($cx, $sp, $msg), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
} | ||
}} | ||
} |
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); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I look at this macro and I see a verbosity switch. The really cool thing here is being able to switch being verbose (full, formatted, allocated error) and quick (short, borrowed error).
In light of this, it might be neat to re-use the verbosity levels and setup from the log crate and branch on that. If the logging setup isn't ideal, we can also just use the verbosity levels and leave them unconnected. Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's an interesting idea. The log crate levels don't really match the html5ever use case, though. You don't need this if you're printing thru the log crate, only when reporting errors up to a library client.
We can merge the simple version now and think about this more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. I'll make an issue.