Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
5d54defc
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,发现更多精彩内容 >>
提交
5d54defc
编写于
3月 17, 2012
作者:
B
Brian Anderson
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
core: Add extension methods for vec
上级
f65ea0c8
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
181 addition
and
13 deletion
+181
-13
src/libcore/core.rs
src/libcore/core.rs
+3
-2
src/libcore/vec.rs
src/libcore/vec.rs
+172
-5
src/test/run-pass/static-impl.rs
src/test/run-pass/static-impl.rs
+6
-6
未找到文件。
src/libcore/core.rs
浏览文件 @
5d54defc
...
...
@@ -5,10 +5,11 @@
import
option
::{
some
,
none
};
import
option
=
option
::
option
;
import
path
=
path
::
path
;
import
vec
::
vec_len
;
import
str
::
extensions
;
import
vec
::
extensions
;
import
option
::
extensions
;
export
path
,
option
,
some
,
none
,
vec_len
,
unreachable
;
export
path
,
option
,
some
,
none
,
unreachable
;
export
extensions
;
// Export the log levels as global constants. Higher levels mean
...
...
src/libcore/vec.rs
浏览文件 @
5d54defc
...
...
@@ -71,9 +71,9 @@
export
windowed
;
export
as_buf
;
export
as_mut_buf
;
export
vec_len
;
export
unsafe
;
export
u8
;
export
extensions
;
#[abi
=
"cdecl"
]
native
mod
rustrt
{
...
...
@@ -954,13 +954,180 @@ fn as_mut_buf<E,T>(v: [mut E], f: fn(*mut E) -> T) -> T unsafe {
let
buf
=
unsafe
::
to_ptr
(
v
)
as
*
mut
E
;
f
(
buf
)
}
#[doc
=
"An extension implementation providing a `len` method"
]
impl
vec_len
<
T
>
for
[
const
T
]
{
#[doc
=
"Return the length of the vector"
]
#[inline(always)]
#[doc
=
"Extension methods for vectors"
]
impl
extensions
<
T
>
for
[
const
T
]
{
#[doc
=
"
Return true if a vector contains an element with the given value
"
]
#[inline]
fn
contains
(
x
:
T
)
->
bool
{
contains
(
self
,
x
)
}
#[doc
=
"Returns the number of elements that are equal to a given value"
]
#[inline]
fn
count
(
x
:
T
)
->
uint
{
count
(
self
,
x
)
}
#[doc
=
"Reduce a vector from left to right"
]
#[inline]
fn
foldl
<
U
:
copy
>
(
z
:
U
,
p
:
fn
(
U
,
T
)
->
U
)
->
U
{
foldl
(
z
,
self
,
p
)
}
#[doc
=
"Reduce a vector from right to left"
]
#[inline]
fn
foldr
<
U
:
copy
>
(
z
:
U
,
p
:
fn
(
T
,
U
)
->
U
)
->
U
{
foldr
(
self
,
z
,
p
)
}
#[doc
=
"Returns true if a vector contains no elements"
]
#[inline]
fn
is_empty
()
->
bool
{
is_empty
(
self
)
}
#[doc
=
"Returns true if a vector contains some elements"
]
#[inline]
fn
is_not_empty
()
->
bool
{
is_not_empty
(
self
)
}
#[doc
=
"
Iterates over a vector
Iterates over vector `v` and, for each element, calls function `f` with
the element's value.
"
]
#[inline]
fn
iter
(
f
:
fn
(
T
))
{
iter
(
self
,
f
)
}
#[doc
=
"
Iterates over a vector's elements and indexes
Iterates over vector `v` and, for each element, calls function `f` with
the element's value and index.
"
]
#[inline]
fn
iteri
(
f
:
fn
(
uint
,
T
))
{
iteri
(
self
,
f
)
}
#[doc
=
"Returns the length of a vector"
]
#[inline]
fn
len
()
->
uint
{
len
(
self
)
}
#[doc
=
"
Find the first index matching some predicate
Apply function `f` to each element of `v`. When function `f` returns true
then an option containing the index is returned. If `f` matches no
elements then none is returned.
"
]
#[inline]
fn
position
(
f
:
fn
(
T
)
->
bool
)
->
option
<
uint
>
{
position
(
self
,
f
)
}
#[doc
=
"Find the first index containing a matching value"
]
#[inline]
fn
position_elem
(
x
:
T
)
->
option
<
uint
>
{
position_elem
(
self
,
x
)
}
#[doc
=
"
Iterates over a vector in reverse
Iterates over vector `v` and, for each element, calls function `f` with
the element's value.
"
]
#[inline]
fn
riter
(
f
:
fn
(
T
))
{
riter
(
self
,
f
)
}
#[doc
=
"
Iterates over a vector's elements and indexes in reverse
Iterates over vector `v` and, for each element, calls function `f` with
the element's value and index.
"
]
#[inline]
fn
riteri
(
f
:
fn
(
uint
,
T
))
{
riteri
(
self
,
f
)
}
#[doc
=
"
Find the last index matching some predicate
Apply function `f` to each element of `v` in reverse order. When function
`f` returns true then an option containing the index is returned. If `f`
matches no elements then none is returned.
"
]
#[inline]
fn
rposition
(
f
:
fn
(
T
)
->
bool
)
->
option
<
uint
>
{
rposition
(
self
,
f
)
}
#[doc
=
"Find the last index containing a matching value"
]
#[inline]
fn
rposition_elem
(
x
:
T
)
->
option
<
uint
>
{
rposition_elem
(
self
,
x
)
}
}
#[doc
=
"Extension methods for vectors"
]
impl
extensions
<
T
:
copy
>
for
[
const
T
]
{
#[doc
=
"
Search for the first element that matches a given predicate
Apply function `f` to each element of `v`, starting from the first.
When function `f` returns true then an option containing the element
is returned. If `f` matches no elements then none is returned.
"
]
#[inline]
fn
find
(
f
:
fn
(
T
)
->
bool
)
->
option
<
T
>
{
find
(
self
,
f
)
}
#[doc
=
"Returns the first element of a vector"
]
#[inline]
fn
head
()
->
T
{
head
(
self
)
}
#[doc
=
"Returns all but the last elemnt of a vector"
]
#[inline]
fn
init
()
->
[
T
]
{
init
(
self
)
}
#[doc
=
"
Returns the last element of a `v`, failing if the vector is empty.
"
]
#[inline]
fn
last
()
->
T
{
last
(
self
)
}
#[doc
=
"
Search for the last element that matches a given predicate
Apply function `f` to each element of `v` in reverse order. When function
`f` returns true then an option containing the element is returned. If `f`
matches no elements then none is returned.
"
]
#[inline]
fn
rfind
(
f
:
fn
(
T
)
->
bool
)
->
option
<
T
>
{
rfind
(
self
,
f
)
}
#[doc
=
"Returns a copy of the elements from [`start`..`end`) from `v`."
]
#[inline]
fn
slice
(
start
:
uint
,
end
:
uint
)
->
[
T
]
{
slice
(
self
,
start
,
end
)
}
#[doc
=
"Returns all but the first element of a vector"
]
#[inline]
fn
tail
()
->
[
T
]
{
tail
(
self
)
}
}
#[doc
=
"Extension methods for vectors"
]
impl
extensions
<
T
>
for
[
T
]
{
#[doc
=
"
Return true if a predicate matches all elements
If the vector contains no elements then true is returned.
"
]
#[inline]
fn
all
(
f
:
fn
(
T
)
->
bool
)
->
bool
{
all
(
self
,
f
)
}
#[doc
=
"
Return true if a predicate matches any elements
If the vector contains no elements then false is returned.
"
]
#[inline]
fn
any
(
f
:
fn
(
T
)
->
bool
)
->
bool
{
any
(
self
,
f
)
}
#[doc
=
"
Apply a function to each element of a vector and return the results
If function `f` returns `none` then that element is excluded from
the resulting vector.
"
]
#[inline]
fn
filter_map
<
U
:
copy
>
(
f
:
fn
(
T
)
->
option
<
U
>
)
->
[
U
]
{
filter_map
(
self
,
f
)
}
#[doc
=
"
Apply a function eo each element of a vector and return a concatenation
of each result vector
"
]
#[inline]
fn
flat_map
<
U
>
(
f
:
fn
(
T
)
->
[
U
])
->
[
U
]
{
flat_map
(
self
,
f
)
}
#[doc
=
"
Apply a function to each element of a vector and return the results
"
]
#[inline]
fn
map
<
U
>
(
f
:
fn
(
T
)
->
U
)
->
[
U
]
{
map
(
self
,
f
)
}
}
#[doc
=
"Extension methods for vectors"
]
impl
extensions
<
T
:
copy
>
for
[
T
]
{
#[doc
=
"
Construct a new vector from the elements of a vector for which some
predicate holds.
Apply function `f` to each element of `v` and return a vector containing
only those elements for which `f` returned true.
"
]
#[inline]
fn
filter
(
f
:
fn
(
T
)
->
bool
)
->
[
T
]
{
filter
(
self
,
f
)
}
}
#[doc
=
"Unsafe operations"
]
mod
unsafe
{
// FIXME: This should have crate visibility
...
...
src/test/run-pass/static-impl.rs
浏览文件 @
5d54defc
...
...
@@ -18,9 +18,9 @@ fn times(f: fn(uint)) {
}
impl
util
<
T
>
for
[
T
]
{
fn
length
()
->
uint
{
vec
::
len
(
self
)
}
fn
iter
(
f
:
fn
(
T
))
{
for
x
in
self
{
f
(
x
);
}
}
fn
map
<
U
>
(
f
:
fn
(
T
)
->
U
)
->
[
U
]
{
fn
length
_
()
->
uint
{
vec
::
len
(
self
)
}
fn
iter
_
(
f
:
fn
(
T
))
{
for
x
in
self
{
f
(
x
);
}
}
fn
map
_
<
U
>
(
f
:
fn
(
T
)
->
U
)
->
[
U
]
{
let
mut
r
=
[];
for
elt
in
self
{
r
+=
[
f
(
elt
)];
}
r
...
...
@@ -33,9 +33,9 @@ impl foo for int { fn plus() -> int { self + 10 } }
assert
10u
.plus
()
==
30
;
assert
"hi"
.plus
()
==
200
;
assert
[
1
]
.length
()
.str
()
==
"1"
;
assert
[
3
,
4
]
.map
({|
a
|
a
+
4
})[
0
]
==
7
;
assert
[
3
,
4
]
.map
::
<
uint
>
({|
a
|
a
as
uint
+
4u
})[
0
]
==
7u
;
assert
[
1
]
.length
_
()
.str
()
==
"1"
;
assert
[
3
,
4
]
.map
_
({|
a
|
a
+
4
})[
0
]
==
7
;
assert
[
3
,
4
]
.map
_
::
<
uint
>
({|
a
|
a
as
uint
+
4u
})[
0
]
==
7u
;
let
mut
x
=
0u
;
10u
.times
{|
_
n
|
x
+=
2u
;}
assert
x
==
20u
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录