Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
b44f423d
R
Rust
项目概览
int
/
Rust
大约 1 年 前同步成功
通知
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,发现更多精彩内容 >>
提交
b44f423d
编写于
8月 11, 2013
作者:
B
blake2-ppc
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
std::iterator: Rename .peek() to .inspect()
上级
8b9e1ce7
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
20 addition
and
19 deletion
+20
-19
src/libstd/iterator.rs
src/libstd/iterator.rs
+20
-19
未找到文件。
src/libstd/iterator.rs
浏览文件 @
b44f423d
...
...
@@ -286,15 +286,15 @@ fn flat_map<'r, B, U: Iterator<B>>(self, f: &'r fn(A) -> U)
///let xs = [1u, 4, 2, 3, 8, 9, 6];
///let sum = xs.iter()
/// .map(|&x| x)
/// .
peek
(|&x| debug!("filtering %u", x))
/// .
inspect
(|&x| debug!("filtering %u", x))
/// .filter(|&x| x % 2 == 0)
/// .
peek
(|&x| debug!("%u made it through", x))
/// .
inspect
(|&x| debug!("%u made it through", x))
/// .sum();
///println(sum.to_str());
/// ~~~
#[inline]
fn
peek
<
'r
>
(
self
,
f
:
&
'r
fn
(
&
A
))
->
Peek
<
'r
,
A
,
Self
>
{
Peek
{
iter
:
self
,
f
:
f
}
fn
inspect
<
'r
>
(
self
,
f
:
&
'r
fn
(
&
A
))
->
Inspect
<
'r
,
A
,
Self
>
{
Inspect
{
iter
:
self
,
f
:
f
}
}
/// An adaptation of an external iterator to the for-loop protocol of rust.
...
...
@@ -1329,14 +1329,14 @@ fn next_back(&mut self) -> Option<B> {
/// An iterator that calls a function with a reference to each
/// element before yielding it.
pub
struct
Peek
<
'self
,
A
,
T
>
{
pub
struct
Inspect
<
'self
,
A
,
T
>
{
priv
iter
:
T
,
priv
f
:
&
'self
fn
(
&
A
)
}
impl
<
'self
,
A
,
T
>
Peek
<
'self
,
A
,
T
>
{
impl
<
'self
,
A
,
T
>
Inspect
<
'self
,
A
,
T
>
{
#[inline]
fn
do_
peek
(
&
self
,
elt
:
Option
<
A
>
)
->
Option
<
A
>
{
fn
do_
inspect
(
&
self
,
elt
:
Option
<
A
>
)
->
Option
<
A
>
{
match
elt
{
Some
(
ref
a
)
=>
(
self
.f
)(
a
),
None
=>
()
...
...
@@ -1346,11 +1346,11 @@ fn do_peek(&self, elt: Option<A>) -> Option<A> {
}
}
impl
<
'self
,
A
,
T
:
Iterator
<
A
>>
Iterator
<
A
>
for
Peek
<
'self
,
A
,
T
>
{
impl
<
'self
,
A
,
T
:
Iterator
<
A
>>
Iterator
<
A
>
for
Inspect
<
'self
,
A
,
T
>
{
#[inline]
fn
next
(
&
mut
self
)
->
Option
<
A
>
{
let
next
=
self
.iter
.next
();
self
.do_
peek
(
next
)
self
.do_
inspect
(
next
)
}
#[inline]
...
...
@@ -1359,15 +1359,17 @@ fn size_hint(&self) -> (uint, Option<uint>) {
}
}
impl
<
'self
,
A
,
T
:
DoubleEndedIterator
<
A
>>
DoubleEndedIterator
<
A
>
for
Peek
<
'self
,
A
,
T
>
{
impl
<
'self
,
A
,
T
:
DoubleEndedIterator
<
A
>>
DoubleEndedIterator
<
A
>
for
Inspect
<
'self
,
A
,
T
>
{
#[inline]
fn
next_back
(
&
mut
self
)
->
Option
<
A
>
{
let
next
=
self
.iter
.next_back
();
self
.do_
peek
(
next
)
self
.do_
inspect
(
next
)
}
}
impl
<
'self
,
A
,
T
:
RandomAccessIterator
<
A
>>
RandomAccessIterator
<
A
>
for
Peek
<
'self
,
A
,
T
>
{
impl
<
'self
,
A
,
T
:
RandomAccessIterator
<
A
>>
RandomAccessIterator
<
A
>
for
Inspect
<
'self
,
A
,
T
>
{
#[inline]
fn
indexable
(
&
self
)
->
uint
{
self
.iter
.indexable
()
...
...
@@ -1375,7 +1377,7 @@ fn indexable(&self) -> uint {
#[inline]
fn
idx
(
&
self
,
index
:
uint
)
->
Option
<
A
>
{
self
.do_
peek
(
self
.iter
.idx
(
index
))
self
.do_
inspect
(
self
.iter
.idx
(
index
))
}
}
...
...
@@ -1651,13 +1653,13 @@ fn test_iterator_flat_map() {
}
#[test]
fn
test_
peek
()
{
fn
test_
inspect
()
{
let
xs
=
[
1u
,
2
,
3
,
4
];
let
mut
n
=
0
;
let
ys
=
xs
.iter
()
.map
(|
&
x
|
x
)
.
peek
(|
_
|
n
+=
1
)
.
inspect
(|
_
|
n
+=
1
)
.collect
::
<~
[
uint
]
>
();
assert_eq!
(
n
,
xs
.len
());
...
...
@@ -2011,11 +2013,11 @@ fn test_random_access_skip() {
}
#[test]
fn
test_random_access_
peek
()
{
fn
test_random_access_
inspect
()
{
let
xs
=
[
1
,
2
,
3
,
4
,
5
];
// test .map and .
peek
that don't implement Clone
let
it
=
xs
.iter
()
.
peek
(|
_
|
{});
// test .map and .
inspect
that don't implement Clone
let
it
=
xs
.iter
()
.
inspect
(|
_
|
{});
assert_eq!
(
xs
.len
(),
it
.indexable
());
for
(
i
,
elt
)
in
xs
.iter
()
.enumerate
()
{
assert_eq!
(
Some
(
elt
),
it
.idx
(
i
));
...
...
@@ -2027,7 +2029,6 @@ fn test_random_access_peek() {
fn
test_random_access_map
()
{
let
xs
=
[
1
,
2
,
3
,
4
,
5
];
// test .map and .peek that don't implement Clone
let
it
=
xs
.iter
()
.map
(|
x
|
*
x
);
assert_eq!
(
xs
.len
(),
it
.indexable
());
for
(
i
,
elt
)
in
xs
.iter
()
.enumerate
()
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录