Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
2a68c719
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,发现更多精彩内容 >>
提交
2a68c719
编写于
7月 26, 2013
作者:
E
Erick Tryzelaar
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
to_either + fixes
上级
aad53cb6
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
71 addition
and
71 deletion
+71
-71
src/libextra/json.rs
src/libextra/json.rs
+9
-10
src/libextra/time.rs
src/libextra/time.rs
+4
-4
src/librustpkg/tests.rs
src/librustpkg/tests.rs
+1
-3
src/libstd/result.rs
src/libstd/result.rs
+49
-45
src/test/bench/core-std.rs
src/test/bench/core-std.rs
+1
-1
src/test/bench/shootout-fasta.rs
src/test/bench/shootout-fasta.rs
+2
-2
src/test/bench/shootout-k-nucleotide-pipes.rs
src/test/bench/shootout-k-nucleotide-pipes.rs
+1
-1
src/test/run-fail/result-get-fail.rs
src/test/run-fail/result-get-fail.rs
+1
-1
src/test/run-pass/cleanup-copy-mode.rs
src/test/run-pass/cleanup-copy-mode.rs
+2
-2
src/test/run-pass/issue-4016.rs
src/test/run-pass/issue-4016.rs
+1
-2
未找到文件。
src/libextra/json.rs
浏览文件 @
2a68c719
...
...
@@ -1867,27 +1867,26 @@ fn test_read_object() {
col
:
8u
,
msg
:
@~
"EOF while parsing object"
}));
assert_eq!
(
result
::
unwrap
(
from_str
(
"{}"
)
),
mk_object
([]));
assert_eq!
(
result
::
unwrap
(
from_str
(
"{
\"
a
\"
: 3}"
)
),
assert_eq!
(
from_str
(
"{}"
)
.unwrap
(
),
mk_object
([]));
assert_eq!
(
from_str
(
"{
\"
a
\"
: 3}"
)
.unwrap
(
),
mk_object
([(
~
"a"
,
Number
(
3.0
f
))]));
assert_eq!
(
result
::
unwrap
(
from_str
(
"{
\"
a
\"
: null,
\"
b
\"
: true }"
)),
assert_eq!
(
from_str
(
"{
\"
a
\"
: null,
\"
b
\"
: true }"
)
.unwrap
(
),
mk_object
([
(
~
"a"
,
Null
),
(
~
"b"
,
Boolean
(
true
))]));
assert_eq!
(
result
::
unwrap
(
from_str
(
"
\n
{
\"
a
\"
: null,
\"
b
\"
: true }
\n
"
)),
assert_eq!
(
from_str
(
"
\n
{
\"
a
\"
: null,
\"
b
\"
: true }
\n
"
)
.unwrap
(),
mk_object
([
(
~
"a"
,
Null
),
(
~
"b"
,
Boolean
(
true
))]));
assert_eq!
(
result
::
unwrap
(
from_str
(
"{
\"
a
\"
: 1.0 ,
\"
b
\"
: [ true ]}"
)),
assert_eq!
(
from_str
(
"{
\"
a
\"
: 1.0 ,
\"
b
\"
: [ true ]}"
)
.unwrap
(
),
mk_object
([
(
~
"a"
,
Number
(
1.0
)),
(
~
"b"
,
List
(
~
[
Boolean
(
true
)]))
]));
assert_eq!
(
result
::
unwrap
(
from_str
(
assert_eq!
(
from_str
(
~
"{"
+
"
\"
a
\"
: 1.0, "
+
"
\"
b
\"
: ["
+
...
...
@@ -1895,7 +1894,7 @@ fn test_read_object() {
"
\"
foo
\\
nbar
\"
, "
+
"{
\"
c
\"
: {
\"
d
\"
: null} } "
+
"]"
+
"}"
)),
"}"
)
.unwrap
(
),
mk_object
([
(
~
"a"
,
Number
(
1.0
f
)),
(
~
"b"
,
List
(
~
[
...
...
src/libextra/time.rs
浏览文件 @
2a68c719
...
...
@@ -1132,13 +1132,13 @@ fn test(s: &str, format: &str) -> bool {
assert
!
(
test
(
"6"
,
"%w"
));
assert
!
(
test
(
"2009"
,
"%Y"
));
assert
!
(
test
(
"09"
,
"%y"
));
assert
!
(
result
::
unwrap
(
strptime
(
"UTC"
,
"%Z"
)
)
.tm_zone
==
assert
!
(
strptime
(
"UTC"
,
"%Z"
)
.unwrap
(
)
.tm_zone
==
~
"UTC"
);
assert
!
(
result
::
unwrap
(
strptime
(
"PST"
,
"%Z"
)
)
.tm_zone
==
assert
!
(
strptime
(
"PST"
,
"%Z"
)
.unwrap
(
)
.tm_zone
==
~
""
);
assert
!
(
result
::
unwrap
(
strptime
(
"-0000"
,
"%z"
)
)
.tm_gmtoff
==
assert
!
(
strptime
(
"-0000"
,
"%z"
)
.unwrap
(
)
.tm_gmtoff
==
0
);
assert
!
(
result
::
unwrap
(
strptime
(
"-0800"
,
"%z"
)
)
.tm_gmtoff
==
assert
!
(
strptime
(
"-0800"
,
"%z"
)
.unwrap
(
)
.tm_gmtoff
==
0
);
assert
!
(
test
(
"%"
,
"%%"
));
...
...
src/librustpkg/tests.rs
浏览文件 @
2a68c719
...
...
@@ -62,9 +62,7 @@ fn git_repo_pkg() -> PkgId {
}
fn
writeFile
(
file_path
:
&
Path
,
contents
:
&
str
)
{
let
out
:
@
io
::
Writer
=
result
::
unwrap
(
io
::
file_writer
(
file_path
,
[
io
::
Create
,
io
::
Truncate
]));
let
out
=
io
::
file_writer
(
file_path
,
[
io
::
Create
,
io
::
Truncate
])
.unwrap
();
out
.write_line
(
contents
);
}
...
...
src/libstd/result.rs
浏览文件 @
2a68c719
...
...
@@ -31,27 +31,21 @@ pub enum Result<T, U> {
Err
(
U
)
}
/**
* Convert to the `either` type
*
* `ok` result variants are converted to `either::right` variants, `err`
* result variants are converted to `either::left`.
*/
#[inline]
pub
fn
to_either
<
T
:
Clone
,
U
:
Clone
>
(
res
:
&
Result
<
U
,
T
>
)
->
Either
<
T
,
U
>
{
match
*
res
{
Ok
(
ref
res
)
=>
either
::
Right
((
*
res
)
.clone
()),
Err
(
ref
fail_
)
=>
either
::
Left
((
*
fail_
)
.clone
())
impl
<
T
,
E
>
Result
<
T
,
E
>
{
/**
* Convert to the `either` type
*
* `ok` result variants are converted to `either::right` variants, `err`
* result variants are converted to `either::left`.
*/
#[inline]
pub
fn
to_either
(
self
)
->
Either
<
E
,
T
>
{
match
self
{
Ok
(
t
)
=>
either
::
Right
(
t
),
Err
(
e
)
=>
either
::
Left
(
e
),
}
}
}
impl
<
T
,
E
>
Result
<
T
,
E
>
{
/**
* Get a reference to the value out of a successful result
*
...
...
@@ -84,7 +78,7 @@ pub fn is_err(&self) -> bool {
}
/**
* Call a
function
based on a previous result
* Call a
method
based on a previous result
*
* If `*self` is `ok` then the value is extracted and passed to `op` whereupon
* `op`s result is returned. if `*self` is `err` then it is immediately
...
...
@@ -106,7 +100,7 @@ pub fn iter(&self, f: &fn(&T)) {
}
/**
* Call a
function
based on a previous result
* Call a
method
based on a previous result
*
* If `*self` is `err` then the value is extracted and passed to `op` whereupon
* `op`s result is returned. if `*self` is `ok` then it is immediately returned.
...
...
@@ -140,7 +134,7 @@ pub fn unwrap_err(self) -> E {
}
/**
* Call a
function
based on a previous result
* Call a
method
based on a previous result
*
* If `self` is `ok` then the value is extracted and passed to `op` whereupon
* `op`s result is returned. if `self` is `err` then it is immediately
...
...
@@ -149,9 +143,9 @@ pub fn unwrap_err(self) -> E {
*
* Example:
*
* let res =
read_file(file).chain(op) { |buf|
* let res =
do read_file(file).chain |buf| {
* ok(parse_bytes(buf))
* }
* }
;
*/
#[inline]
pub
fn
chain
<
U
>
(
self
,
op
:
&
fn
(
T
)
->
Result
<
U
,
E
>
)
->
Result
<
U
,
E
>
{
...
...
@@ -162,7 +156,7 @@ pub fn chain<U>(self, op: &fn(T) -> Result<U,E>) -> Result<U,E> {
}
/**
* Call a
function
based on a previous result
* Call a
method
based on a previous result
*
* If `self` is `err` then the value is extracted and passed to `op`
* whereupon `op`s result is returned. if `self` is `ok` then it is
...
...
@@ -195,13 +189,13 @@ pub fn get(&self) -> T {
}
/**
* Call a function
based on a previous result
*
* If `*self` is `err` then the value is extracted and passed to `op` whereupon
* `op`s result is wrapped in an `err` and returned. if `*self` is `ok` then it
* is immediately returned. This function can be used to pass through a
* successful result while handling an error.
*/
* Call a method
based on a previous result
*
* If `*self` is `err` then the value is extracted and passed to `op` whereupon
* `op`s result is wrapped in an `err` and returned. if `*self` is `ok` then it
* is immediately returned. This function can be used to pass through a
* successful result while handling an error.
*/
#[inline]
pub
fn
map_err
<
F
:
Clone
>
(
&
self
,
op
:
&
fn
(
&
E
)
->
F
)
->
Result
<
T
,
F
>
{
match
*
self
{
...
...
@@ -228,19 +222,19 @@ pub fn get_err(&self) -> E {
}
/**
* Call a function
based on a previous result
*
* If `res` is `ok` then the value is extracted and passed to `op` whereupon
* `op`s result is wrapped in `ok` and returned. if `res` is `err` then it is
* immediately returned. This function can be used to compose the results of
* two functions.
*
* Example:
*
* let res = map(read_file(file)
) { |buf|
* parse_bytes(buf)
* }
*/
* Call a method
based on a previous result
*
* If `res` is `ok` then the value is extracted and passed to `op` whereupon
* `op`s result is wrapped in `ok` and returned. if `res` is `err` then it is
* immediately returned. This function can be used to compose the results of
* two functions.
*
* Example:
*
* let res = read_file(file).map(
) { |buf|
* parse_bytes(buf)
* });
*/
#[inline]
pub
fn
map
<
U
:
Clone
>
(
&
self
,
op
:
&
fn
(
&
T
)
->
U
)
->
Result
<
U
,
E
>
{
match
*
self
{
...
...
@@ -351,6 +345,7 @@ pub fn iter_vec2<S,T,U>(ss: &[S], ts: &[T],
mod
tests
{
use
result
::{
Err
,
Ok
,
Result
};
use
result
;
use
either
;
pub
fn
op1
()
->
result
::
Result
<
int
,
~
str
>
{
result
::
Ok
(
666
)
}
...
...
@@ -408,4 +403,13 @@ pub fn test_get_ref_method() {
let
foo
:
Result
<
int
,
()
>
=
Ok
(
100
);
assert_eq!
(
*
foo
.get_ref
(),
100
);
}
#[test]
pub
fn
test_to_either
()
{
let
r
:
Result
<
int
,
()
>
=
Ok
(
100
);
let
err
:
Result
<
(),
int
>
=
Err
(
404
);
assert_eq!
(
r
.to_either
(),
either
::
Right
(
100
));
assert_eq!
(
err
.to_either
(),
either
::
Left
(
404
));
}
}
src/test/bench/core-std.rs
浏览文件 @
2a68c719
...
...
@@ -75,7 +75,7 @@ fn read_line() {
.push_rel
(
&
Path
(
"src/test/bench/shootout-k-nucleotide.data"
));
for
int
::
range
(
0
,
3
)
|
_
i
|
{
let
reader
=
result
::
unwrap
(
io
::
file_reader
(
&
path
)
);
let
reader
=
io
::
file_reader
(
&
path
)
.unwrap
(
);
while
!
reader
.eof
()
{
reader
.read_line
();
}
...
...
src/test/bench/shootout-fasta.rs
浏览文件 @
2a68c719
...
...
@@ -124,8 +124,8 @@ fn main() {
};
let
writer
=
if
os
::
getenv
(
"RUST_BENCH"
)
.is_some
()
{
result
::
unwrap
(
io
::
file_writer
(
&
Path
(
"./shootout-fasta.data"
),
[
io
::
Truncate
,
io
::
Create
])
)
io
::
file_writer
(
&
Path
(
"./shootout-fasta.data"
),
[
io
::
Truncate
,
io
::
Create
])
.unwrap
(
)
}
else
{
io
::
stdout
()
};
...
...
src/test/bench/shootout-k-nucleotide-pipes.rs
浏览文件 @
2a68c719
...
...
@@ -161,7 +161,7 @@ fn main() {
// get to this massive data set, but include_bin! chokes on it (#2598)
let
path
=
Path
(
env!
(
"CFG_SRC_DIR"
))
.push_rel
(
&
Path
(
"src/test/bench/shootout-k-nucleotide.data"
));
result
::
unwrap
(
io
::
file_reader
(
&
path
)
)
io
::
file_reader
(
&
path
)
.unwrap
(
)
}
else
{
io
::
stdin
()
};
...
...
src/test/run-fail/result-get-fail.rs
浏览文件 @
2a68c719
...
...
@@ -13,5 +13,5 @@
use
std
::
result
;
fn
main
()
{
error!
(
result
::
get
(
&
result
::
Err
::
<
int
,
~
str
>
(
~
"kitty"
)
));
error!
(
result
::
Err
::
<
int
,
~
str
>
(
~
"kitty"
)
.get
(
));
}
src/test/run-pass/cleanup-copy-mode.rs
浏览文件 @
2a68c719
...
...
@@ -16,7 +16,7 @@
fn
adder
(
x
:
@
int
,
y
:
@
int
)
->
int
{
return
*
x
+
*
y
;
}
fn
failer
()
->
@
int
{
fail
!
();
}
pub
fn
main
()
{
assert
!
(
result
::
is_err
(
&
task
::
try
(||
{
assert
!
(
task
::
try
(||
{
adder
(
@
2
,
failer
());
()
})));
})
.is_err
(
));
}
src/test/run-pass/issue-4016.rs
浏览文件 @
2a68c719
...
...
@@ -11,14 +11,13 @@
extern
mod
extra
;
use
std
::
result
;
use
extra
::
json
;
use
extra
::
serialize
::
Decodable
;
trait
JD
:
Decodable
<
json
::
Decoder
>
{
}
fn
exec
<
T
:
JD
>
()
{
let
doc
=
result
::
unwrap
(
json
::
from_str
(
""
)
);
let
doc
=
json
::
from_str
(
""
)
.unwrap
(
);
let
mut
decoder
=
json
::
Decoder
(
doc
);
let
_
v
:
T
=
Decodable
::
decode
(
&
mut
decoder
);
fail
!
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录