Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
a0ef3341
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,发现更多精彩内容 >>
提交
a0ef3341
编写于
12月 18, 2012
作者:
E
Erick Tryzelaar
提交者:
Erick Tryzelaar
12月 18, 2012
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
core: use movable self to clean up option/result.
上级
938058b0
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
103 addition
and
86 deletion
+103
-86
src/libcore/option.rs
src/libcore/option.rs
+58
-40
src/libcore/pipes.rs
src/libcore/pipes.rs
+2
-2
src/libcore/result.rs
src/libcore/result.rs
+43
-44
未找到文件。
src/libcore/option.rs
浏览文件 @
a0ef3341
...
...
@@ -96,19 +96,6 @@ pub enum Option<T> {
}
}
pub
pure
fn
expect
<
T
>
(
opt
:
Option
<
T
>
,
reason
:
~
str
)
->
T
{
/*!
* Gets the value out of an option without copying, printing a
* specified message on failure.
*
* # Failure
*
* Fails if the value equals `none`
*/
if
opt
.is_some
()
{
move
option
::
unwrap
(
move
opt
)
}
else
{
fail
reason
}
}
pub
pure
fn
map
<
T
,
U
>
(
opt
:
&
Option
<
T
>
,
f
:
fn
(
x
:
&
T
)
->
U
)
->
Option
<
U
>
{
//! Maps a `some` value by reference from one type to another
...
...
@@ -235,35 +222,46 @@ pub fn swap_unwrap<T>(opt: &mut Option<T>) -> T {
unwrap
(
util
::
replace
(
opt
,
None
))
}
pub
pure
fn
unwrap_
expect
<
T
>
(
opt
:
Option
<
T
>
,
reason
:
&
str
)
->
T
{
pub
pure
fn
expect
<
T
>
(
opt
:
Option
<
T
>
,
reason
:
&
str
)
->
T
{
//! As unwrap, but with a specified failure message.
if
opt
.is_none
()
{
fail
reason
.to_owned
();
}
unwrap
(
move
opt
)
match
move
opt
{
Some
(
move
val
)
=>
val
,
None
=>
fail
reason
.to_owned
(),
}
}
// Some of these should change to be &Option<T>, some should not. See below.
impl
<
T
>
Option
<
T
>
{
/// Returns true if the option equals `none`
pure
fn
is_none
()
->
bool
{
is_none
(
&
self
)
}
#[inline(always)]
pure
fn
is_none
(
&
self
)
->
bool
{
is_none
(
self
)
}
/// Returns true if the option contains some value
pure
fn
is_some
()
->
bool
{
is_some
(
&
self
)
}
}
#[inline(always)]
pure
fn
is_some
(
&
self
)
->
bool
{
is_some
(
self
)
}
impl
<
T
>
&
Option
<
T
>
{
/**
* Update an optional value by optionally running its content by reference
* through a function that returns an option.
*/
pure
fn
chain_ref
<
U
>
(
f
:
fn
(
x
:
&
T
)
->
Option
<
U
>
)
->
Option
<
U
>
{
#[inline(always)]
pure
fn
chain_ref
<
U
>
(
&
self
,
f
:
fn
(
x
:
&
T
)
->
Option
<
U
>
)
->
Option
<
U
>
{
chain_ref
(
self
,
f
)
}
/// Maps a `some` value from one type to another by reference
#[inline(always)]
pure
fn
map
<
U
>
(
&
self
,
f
:
fn
(
x
:
&
T
)
->
U
)
->
Option
<
U
>
{
map
(
self
,
f
)
}
/// Applies a function to the contained value or returns a default
pure
fn
map_default
<
U
>
(
def
:
U
,
f
:
fn
(
x
:
&
T
)
->
U
)
->
U
{
map_default
(
self
,
move
def
,
f
)
}
#[inline(always)]
pure
fn
map_default
<
U
>
(
&
self
,
def
:
U
,
f
:
fn
(
x
:
&
T
)
->
U
)
->
U
{
map_default
(
self
,
move
def
,
f
)
}
/// Performs an operation on the contained value by reference
pure
fn
iter
(
f
:
fn
(
x
:
&
T
))
{
iter
(
self
,
f
)
}
/// Maps a `some` value from one type to another by reference
pure
fn
map
<
U
>
(
f
:
fn
(
x
:
&
T
)
->
U
)
->
Option
<
U
>
{
map
(
self
,
f
)
}
#[inline(always)]
pure
fn
iter
(
&
self
,
f
:
fn
(
x
:
&
T
))
{
iter
(
self
,
f
)
}
/**
Gets an immutable reference to the value inside an option.
...
...
@@ -278,7 +276,29 @@ impl<T> &Option<T> {
Instead, prefer to use pattern matching and handle the `None`
case explicitly.
*/
pure
fn
get_ref
()
->
&
self
/
T
{
get_ref
(
self
)
}
#[inline(always)]
pure
fn
get_ref
(
&
self
)
->
&
self
/
T
{
get_ref
(
self
)
}
/**
* Gets the value out of an option without copying.
*
* # Failure
*
* Fails if the value equals `none`
*/
#[inline(always)]
pure
fn
unwrap
(
self
)
->
T
{
unwrap
(
self
)
}
/**
* Gets the value out of an option, printing a specified message on
* failure
*
* # Failure
*
* Fails if the value equals `none`
*/
#[inline(always)]
pure
fn
expect
(
self
,
reason
:
&
str
)
->
T
{
expect
(
self
,
reason
)
}
}
impl
<
T
:
Copy
>
Option
<
T
>
{
...
...
@@ -296,19 +316,17 @@ impl<T: Copy> Option<T> {
Instead, prefer to use pattern matching and handle the `None`
case explicitly.
*/
pure
fn
get
()
->
T
{
get
(
self
)
}
pure
fn
get_default
(
def
:
T
)
->
T
{
get_default
(
self
,
def
)
}
/**
* Gets the value out of an option, printing a specified message on
* failure
*
* # Failure
*
* Fails if the value equals `none`
*/
pure
fn
expect
(
reason
:
~
str
)
->
T
{
expect
(
self
,
move
reason
)
}
#[inline(always)]
pure
fn
get
(
self
)
->
T
{
get
(
self
)
}
#[inline(always)]
pure
fn
get_default
(
self
,
def
:
T
)
->
T
{
get_default
(
self
,
def
)
}
/// Applies a function zero or more times until the result is none.
pure
fn
while_some
(
blk
:
fn
(
v
:
T
)
->
Option
<
T
>
)
{
while_some
(
self
,
blk
)
}
#[inline(always)]
pure
fn
while_some
(
self
,
blk
:
fn
(
v
:
T
)
->
Option
<
T
>
)
{
while_some
(
self
,
blk
)
}
}
#[test]
...
...
src/libcore/pipes.rs
浏览文件 @
a0ef3341
...
...
@@ -412,7 +412,7 @@ pub fn send<T: Owned, Tbuffer: Owned>(p: SendPacketBuffered<T, Tbuffer>,
*/
pub
fn
recv
<
T
:
Owned
,
Tbuffer
:
Owned
>
(
p
:
RecvPacketBuffered
<
T
,
Tbuffer
>
)
->
T
{
option
::
unwrap_expect
(
try_recv
(
move
p
),
"connection closed"
)
try_recv
(
move
p
)
.expect
(
"connection closed"
)
}
/** Attempts to receive a message from a pipe.
...
...
@@ -1102,7 +1102,7 @@ fn try_recv() -> Option<T> {
}
fn
recv
()
->
T
{
option
::
unwrap_expect
(
self
.try_recv
(),
"port_set: endpoints closed"
)
self
.try_recv
()
.expect
(
"port_set: endpoints closed"
)
}
}
...
...
src/libcore/result.rs
浏览文件 @
a0ef3341
...
...
@@ -114,7 +114,7 @@ pub enum Result<T, U> {
* ok(parse_bytes(buf))
* }
*/
pub
fn
chain
<
T
,
U
,
V
>
(
res
:
Result
<
T
,
V
>
,
op
:
fn
(
t
:
T
)
pub
pure
fn
chain
<
T
,
U
,
V
>
(
res
:
Result
<
T
,
V
>
,
op
:
fn
(
T
)
->
Result
<
U
,
V
>
)
->
Result
<
U
,
V
>
{
match
move
res
{
Ok
(
move
t
)
=>
op
(
move
t
),
...
...
@@ -130,7 +130,7 @@ pub fn chain<T, U, V>(res: Result<T, V>, op: fn(t: T)
* immediately returned. This function can be used to pass through a
* successful result while handling an error.
*/
pub
fn
chain_err
<
T
,
U
,
V
>
(
pub
pure
fn
chain_err
<
T
,
U
,
V
>
(
res
:
Result
<
T
,
V
>
,
op
:
fn
(
t
:
V
)
->
Result
<
T
,
U
>
)
->
Result
<
T
,
U
>
{
...
...
@@ -154,7 +154,7 @@ pub fn chain_err<T, U, V>(
* print_buf(buf)
* }
*/
pub
fn
iter
<
T
,
E
>
(
res
:
&
Result
<
T
,
E
>
,
f
:
fn
(
&
T
))
{
pub
pure
fn
iter
<
T
,
E
>
(
res
:
&
Result
<
T
,
E
>
,
f
:
fn
(
&
T
))
{
match
*
res
{
Ok
(
ref
t
)
=>
f
(
t
),
Err
(
_
)
=>
()
...
...
@@ -169,7 +169,7 @@ pub fn iter<T, E>(res: &Result<T, E>, f: fn(&T)) {
* This function can be used to pass through a successful result while
* handling an error.
*/
pub
fn
iter_err
<
T
,
E
>
(
res
:
&
Result
<
T
,
E
>
,
f
:
fn
(
&
E
))
{
pub
pure
fn
iter_err
<
T
,
E
>
(
res
:
&
Result
<
T
,
E
>
,
f
:
fn
(
&
E
))
{
match
*
res
{
Ok
(
_
)
=>
(),
Err
(
ref
e
)
=>
f
(
e
)
...
...
@@ -190,7 +190,7 @@ pub fn iter_err<T, E>(res: &Result<T, E>, f: fn(&E)) {
* parse_bytes(buf)
* }
*/
pub
fn
map
<
T
,
E
:
Copy
,
U
:
Copy
>
(
res
:
&
Result
<
T
,
E
>
,
op
:
fn
(
&
T
)
->
U
)
pub
pure
fn
map
<
T
,
E
:
Copy
,
U
:
Copy
>
(
res
:
&
Result
<
T
,
E
>
,
op
:
fn
(
&
T
)
->
U
)
->
Result
<
U
,
E
>
{
match
*
res
{
Ok
(
ref
t
)
=>
Ok
(
op
(
t
)),
...
...
@@ -206,7 +206,7 @@ pub fn map<T, E: Copy, U: Copy>(res: &Result<T, E>, op: fn(&T) -> U)
* is immediately returned. This function can be used to pass through a
* successful result while handling an error.
*/
pub
fn
map_err
<
T
:
Copy
,
E
,
F
:
Copy
>
(
res
:
&
Result
<
T
,
E
>
,
op
:
fn
(
&
E
)
->
F
)
pub
pure
fn
map_err
<
T
:
Copy
,
E
,
F
:
Copy
>
(
res
:
&
Result
<
T
,
E
>
,
op
:
fn
(
&
E
)
->
F
)
->
Result
<
T
,
F
>
{
match
*
res
{
Ok
(
copy
t
)
=>
Ok
(
t
),
...
...
@@ -215,58 +215,55 @@ pub fn map_err<T: Copy, E, F: Copy>(res: &Result<T, E>, op: fn(&E) -> F)
}
impl
<
T
,
E
>
Result
<
T
,
E
>
{
#[inline(always)]
pure
fn
get_ref
(
&
self
)
->
&
self
/
T
{
get_ref
(
self
)
}
pure
fn
is_ok
()
->
bool
{
is_ok
(
&
self
)
}
#[inline(always)]
pure
fn
is_ok
(
&
self
)
->
bool
{
is_ok
(
self
)
}
pure
fn
is_err
()
->
bool
{
is_err
(
&
self
)
}
#[inline(always)]
pure
fn
is_err
(
&
self
)
->
bool
{
is_err
(
self
)
}
pure
fn
iter
(
f
:
fn
(
&
T
))
{
match
self
{
Ok
(
ref
t
)
=>
f
(
t
),
Err
(
_
)
=>
()
}
#[inline(always)]
pure
fn
iter
(
&
self
,
f
:
fn
(
&
T
))
{
iter
(
self
,
f
)
}
#[inline(always)]
pure
fn
iter_err
(
&
self
,
f
:
fn
(
&
E
))
{
iter_err
(
self
,
f
)
}
#[inline(always)]
pure
fn
unwrap
(
self
)
->
T
{
unwrap
(
self
)
}
#[inline(always)]
pure
fn
unwrap_err
(
self
)
->
T
{
unwrap
(
self
)
}
#[inline(always)]
pure
fn
chain
<
U
>
(
self
,
op
:
fn
(
T
)
->
Result
<
U
,
E
>
)
->
Result
<
U
,
E
>
{
chain
(
self
,
op
)
}
fn
iter_err
(
f
:
fn
(
&
E
))
{
match
self
{
Ok
(
_
)
=>
(),
Err
(
ref
e
)
=>
f
(
e
)
}
#[inline(always)]
pure
fn
chain_err
<
F
>
(
self
,
op
:
fn
(
E
)
->
Result
<
T
,
F
>
)
->
Result
<
T
,
F
>
{
chain_err
(
self
,
op
)
}
}
impl
<
T
:
Copy
,
E
>
Result
<
T
,
E
>
{
pure
fn
get
()
->
T
{
get
(
&
self
)
}
#[inline(always)]
pure
fn
get
(
&
self
)
->
T
{
get
(
self
)
}
fn
map_err
<
F
:
Copy
>
(
op
:
fn
(
&
E
)
->
F
)
->
Result
<
T
,
F
>
{
match
self
{
Ok
(
copy
t
)
=>
Ok
(
t
),
Err
(
ref
e
)
=>
Err
(
op
(
e
))
}
#[inline(always)]
pure
fn
map_err
<
F
:
Copy
>
(
&
self
,
op
:
fn
(
&
E
)
->
F
)
->
Result
<
T
,
F
>
{
map_err
(
self
,
op
)
}
}
impl
<
T
,
E
:
Copy
>
Result
<
T
,
E
>
{
pure
fn
get_err
()
->
E
{
get_err
(
&
self
)
}
fn
map
<
U
:
Copy
>
(
op
:
fn
(
&
T
)
->
U
)
->
Result
<
U
,
E
>
{
match
self
{
Ok
(
ref
t
)
=>
Ok
(
op
(
t
)),
Err
(
copy
e
)
=>
Err
(
e
)
}
}
}
impl
<
T
:
Copy
,
E
:
Copy
>
Result
<
T
,
E
>
{
fn
chain
<
U
:
Copy
>
(
op
:
fn
(
t
:
T
)
->
Result
<
U
,
E
>
)
->
Result
<
U
,
E
>
{
// XXX: Bad copy
chain
(
copy
self
,
op
)
}
#[inline(always)]
pure
fn
get_err
(
&
self
)
->
E
{
get_err
(
self
)
}
fn
chain_err
<
F
:
Copy
>
(
op
:
fn
(
t
:
E
)
->
Result
<
T
,
F
>
)
->
Result
<
T
,
F
>
{
// XXX: Bad copy
chain_err
(
copy
self
,
op
)
#[inline(always)]
pure
fn
map
<
U
:
Copy
>
(
&
self
,
op
:
fn
(
&
T
)
->
U
)
->
Result
<
U
,
E
>
{
map
(
self
,
op
)
}
}
...
...
@@ -360,7 +357,8 @@ pub fn iter_vec2<S,T,U:Copy>(ss: &[S], ts: &[T],
}
/// Unwraps a result, assuming it is an `ok(T)`
pub
fn
unwrap
<
T
,
U
>
(
res
:
Result
<
T
,
U
>
)
->
T
{
#[inline(always)]
pub
pure
fn
unwrap
<
T
,
U
>
(
res
:
Result
<
T
,
U
>
)
->
T
{
match
move
res
{
Ok
(
move
t
)
=>
move
t
,
Err
(
_
)
=>
fail
~
"unwrap called on an err result"
...
...
@@ -368,7 +366,8 @@ pub fn unwrap<T, U>(res: Result<T, U>) -> T {
}
/// Unwraps a result, assuming it is an `err(U)`
pub
fn
unwrap_err
<
T
,
U
>
(
res
:
Result
<
T
,
U
>
)
->
U
{
#[inline(always)]
pub
pure
fn
unwrap_err
<
T
,
U
>
(
res
:
Result
<
T
,
U
>
)
->
U
{
match
move
res
{
Err
(
move
u
)
=>
move
u
,
Ok
(
_
)
=>
fail
~
"unwrap called on an ok result"
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录