Skip to content
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 3 commits into from Feb 27, 2015
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/format.rs
@@ -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!(&not_formatted[..], "Vague error");
/// assert!(not_formatted.is_borrowed())
/// # }
/// ```
#[macro_export]
macro_rules! format_if {
Copy link
Owner

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?

Copy link
Collaborator Author

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.

Copy link
Owner

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.

($enabled:expr, $borrowed:expr, $fmt:expr, $($args:expr),*) => {
if $enabled {
::std::borrow::Cow::Owned(format!($fmt, $($args),*))
} else {
::std::borrow::Cow::Borrowed($borrowed)
}
}
}
4 changes: 4 additions & 0 deletions src/lib.rs
Expand Up @@ -6,3 +6,7 @@
//! A collection of great and ubiqutitous macros.
//!

pub mod test;
pub mod mem;
pub mod format;
pub mod syntax_ext;
44 changes: 44 additions & 0 deletions src/mem.rs
@@ -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)
}
),+
)
}
}
31 changes: 31 additions & 0 deletions src/syntax_ext.rs
@@ -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) }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be ext_bail

}}
}

/// 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),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

}
}}
}
24 changes: 24 additions & 0 deletions src/test.rs
@@ -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);
}
}
}