Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
c133b211
R
Rust
项目概览
int
/
Rust
11 个月 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
c133b211
编写于
1月 09, 2015
作者:
B
bors
浏览文件
操作
浏览文件
下载
差异文件
auto merge of #20776 : kmcallister/rust/macro-cleanup, r=alexcrichton
r? @alexcrichton. This passes tests for me.
上级
73a25f55
a96a8b2b
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
15 addition
and
183 deletion
+15
-183
src/doc/trpl/macros.md
src/doc/trpl/macros.md
+6
-2
src/libcore/macros.rs
src/libcore/macros.rs
+7
-2
src/libstd/lib.rs
src/libstd/lib.rs
+2
-1
src/libstd/macros.rs
src/libstd/macros.rs
+0
-178
未找到文件。
src/doc/trpl/macros.md
浏览文件 @
c133b211
...
...
@@ -440,14 +440,18 @@ to print "I am never printed" and to run forever.
# Scoping and macro import/export
Macros occupy a single global namespace. The interaction with Rust's system of
modules and crates is somewhat complex.
Macros are expanded at an early stage in compilation, before name resolution.
One downside is that scoping works differently for macros, compared to other
constructs in the language.
Definition and expansion of macros both happen in a single depth-first,
lexical-order traversal of a crate's source. So a macro defined at module scope
is visible to any subsequent code in the same module, which includes the body
of any subsequent child
`mod`
items.
A macro defined within the body of a single
`fn`
, or anywhere else not at
module scope, is visible only within that item.
If a module has the
`macro_use`
attribute, its macros are also visible in its
parent module after the child's
`mod`
item. If the parent also has
`macro_use`
then the macros will be visible in the grandparent after the parent's
`mod`
...
...
src/libcore/macros.rs
浏览文件 @
c133b211
...
...
@@ -49,15 +49,16 @@
/// assert!(a + b == 30, "a = {}, b = {}", a, b);
/// ```
#[macro_export]
#[stable]
macro_rules!
assert
{
(
$cond:expr
)
=>
(
if
!
$cond
{
panic!
(
concat!
(
"assertion failed: "
,
stringify!
(
$cond
)))
}
);
(
$cond:expr
,
$
(
$arg:
expr
),
+
)
=>
(
(
$cond:expr
,
$
(
$arg:
tt
)
+
)
=>
(
if
!
$cond
{
panic!
(
$
(
$arg
)
,
+
)
panic!
(
$
(
$arg
)
+
)
}
);
}
...
...
@@ -75,6 +76,7 @@
/// assert_eq!(a, b);
/// ```
#[macro_export]
#[stable]
macro_rules!
assert_eq
{
(
$left:expr
,
$right:expr
)
=>
({
match
(
&
(
$left
),
&
(
$right
))
{
...
...
@@ -116,6 +118,7 @@
/// debug_assert!(a + b == 30, "a = {}, b = {}", a, b);
/// ```
#[macro_export]
#[stable]
macro_rules!
debug_assert
{
(
$
(
$arg:tt
)
*
)
=>
(
if
cfg!
(
not
(
ndebug
))
{
assert!
(
$
(
$arg
)
*
);
})
}
...
...
@@ -227,6 +230,7 @@
/// }
/// ```
#[macro_export]
#[unstable
=
"relationship with panic is unclear"
]
macro_rules!
unreachable
{
()
=>
({
panic!
(
"internal error: entered unreachable code"
)
...
...
@@ -242,6 +246,7 @@
/// A standardised placeholder for marking unfinished code. It panics with the
/// message `"not yet implemented"` when executed.
#[macro_export]
#[unstable
=
"relationship with panic is unclear"
]
macro_rules!
unimplemented
{
()
=>
(
panic!
(
"not yet implemented"
))
}
src/libstd/lib.rs
浏览文件 @
c133b211
...
...
@@ -122,7 +122,8 @@
extern
crate
log
;
#[macro_use]
#[macro_reexport(write,
writeln)]
#[macro_reexport(assert,
assert_eq,
debug_assert,
debug_assert_eq,
unreachable,
unimplemented,
write,
writeln)]
extern
crate
core
;
#[macro_use]
...
...
src/libstd/macros.rs
浏览文件 @
c133b211
...
...
@@ -60,184 +60,6 @@
});
}
/// Ensure that a boolean expression is `true` at runtime.
///
/// This will invoke the `panic!` macro if the provided expression cannot be
/// evaluated to `true` at runtime.
///
/// # Example
///
/// ```
/// // the panic message for these assertions is the stringified value of the
/// // expression given.
/// assert!(true);
/// # fn some_computation() -> bool { true }
/// assert!(some_computation());
///
/// // assert with a custom message
/// # let x = true;
/// assert!(x, "x wasn't true!");
/// # let a = 3i; let b = 27i;
/// assert!(a + b == 30, "a = {}, b = {}", a, b);
/// ```
#[macro_export]
#[stable]
macro_rules!
assert
{
(
$cond:expr
)
=>
(
if
!
$cond
{
panic!
(
concat!
(
"assertion failed: "
,
stringify!
(
$cond
)))
}
);
(
$cond:expr
,
$
(
$arg:tt
)
+
)
=>
(
if
!
$cond
{
panic!
(
$
(
$arg
)
+
)
}
);
}
/// Asserts that two expressions are equal to each other, testing equality in
/// both directions.
///
/// On panic, this macro will print the values of the expressions.
///
/// # Example
///
/// ```
/// let a = 3i;
/// let b = 1i + 2i;
/// assert_eq!(a, b);
/// ```
#[macro_export]
#[stable]
macro_rules!
assert_eq
{
(
$left:expr
,
$right:expr
)
=>
({
match
(
&
(
$left
),
&
(
$right
))
{
(
left_val
,
right_val
)
=>
{
// check both directions of equality....
if
!
((
*
left_val
==
*
right_val
)
&&
(
*
right_val
==
*
left_val
))
{
panic!
(
"assertion failed: `(left == right) && (right == left)`
\
(left: `{:?}`, right: `{:?}`)"
,
*
left_val
,
*
right_val
)
}
}
}
})
}
/// Ensure that a boolean expression is `true` at runtime.
///
/// This will invoke the `panic!` macro if the provided expression cannot be
/// evaluated to `true` at runtime.
///
/// Unlike `assert!`, `debug_assert!` statements can be disabled by passing
/// `--cfg ndebug` to the compiler. This makes `debug_assert!` useful for
/// checks that are too expensive to be present in a release build but may be
/// helpful during development.
///
/// # Example
///
/// ```
/// // the panic message for these assertions is the stringified value of the
/// // expression given.
/// debug_assert!(true);
/// # fn some_expensive_computation() -> bool { true }
/// debug_assert!(some_expensive_computation());
///
/// // assert with a custom message
/// # let x = true;
/// debug_assert!(x, "x wasn't true!");
/// # let a = 3i; let b = 27i;
/// debug_assert!(a + b == 30, "a = {}, b = {}", a, b);
/// ```
#[macro_export]
#[stable]
macro_rules!
debug_assert
{
(
$
(
$arg:tt
)
*
)
=>
(
if
cfg!
(
not
(
ndebug
))
{
assert!
(
$
(
$arg
)
*
);
})
}
/// Asserts that two expressions are equal to each other, testing equality in
/// both directions.
///
/// On panic, this macro will print the values of the expressions.
///
/// Unlike `assert_eq!`, `debug_assert_eq!` statements can be disabled by
/// passing `--cfg ndebug` to the compiler. This makes `debug_assert_eq!`
/// useful for checks that are too expensive to be present in a release build
/// but may be helpful during development.
///
/// # Example
///
/// ```
/// let a = 3i;
/// let b = 1i + 2i;
/// debug_assert_eq!(a, b);
/// ```
#[macro_export]
macro_rules!
debug_assert_eq
{
(
$
(
$arg:tt
)
*
)
=>
(
if
cfg!
(
not
(
ndebug
))
{
assert_eq!
(
$
(
$arg
)
*
);
})
}
/// A utility macro for indicating unreachable code.
///
/// This is useful any time that the compiler can't determine that some code is unreachable. For
/// example:
///
/// * Match arms with guard conditions.
/// * Loops that dynamically terminate.
/// * Iterators that dynamically terminate.
///
/// # Panics
///
/// This will always panic.
///
/// # Examples
///
/// Match arms:
///
/// ```rust
/// fn foo(x: Option<int>) {
/// match x {
/// Some(n) if n >= 0 => println!("Some(Non-negative)"),
/// Some(n) if n < 0 => println!("Some(Negative)"),
/// Some(_) => unreachable!(), // compile error if commented out
/// None => println!("None")
/// }
/// }
/// ```
///
/// Iterators:
///
/// ```rust
/// fn divide_by_three(x: u32) -> u32 { // one of the poorest implementations of x/3
/// for i in std::iter::count(0_u32, 1) {
/// if 3*i < i { panic!("u32 overflow"); }
/// if x < 3*i { return i-1; }
/// }
/// unreachable!();
/// }
/// ```
#[macro_export]
#[unstable
=
"relationship with panic is unclear"
]
macro_rules!
unreachable
{
()
=>
({
panic!
(
"internal error: entered unreachable code"
)
});
(
$msg:expr
)
=>
({
unreachable!
(
"{}"
,
$msg
)
});
(
$fmt:expr
,
$
(
$arg:tt
)
*
)
=>
({
panic!
(
concat!
(
"internal error: entered unreachable code: "
,
$fmt
),
$
(
$arg
)
*
)
});
}
/// A standardised placeholder for marking unfinished code. It panics with the
/// message `"not yet implemented"` when executed.
#[macro_export]
#[unstable
=
"relationship with panic is unclear"
]
macro_rules!
unimplemented
{
()
=>
(
panic!
(
"not yet implemented"
))
}
/// Use the syntax described in `std::fmt` to create a value of type `String`.
/// See `std::fmt` for more information.
///
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录