Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
0d247807
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,发现更多精彩内容 >>
提交
0d247807
编写于
11月 30, 2014
作者:
E
Erick Tryzelaar
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
std: add Reader impl for &[u8]
上级
6687b2a6
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
76 addition
and
20 deletion
+76
-20
src/libcore/result.rs
src/libcore/result.rs
+3
-4
src/libgraphviz/lib.rs
src/libgraphviz/lib.rs
+3
-5
src/libstd/io/buffered.rs
src/libstd/io/buffered.rs
+3
-3
src/libstd/io/mem.rs
src/libstd/io/mem.rs
+61
-0
src/libstd/io/util.rs
src/libstd/io/util.rs
+2
-3
src/libtime/lib.rs
src/libtime/lib.rs
+4
-5
未找到文件。
src/libcore/result.rs
浏览文件 @
0d247807
...
...
@@ -446,13 +446,12 @@ pub fn as_mut_slice<'r>(&'r mut self) -> &'r mut [T] {
/// ```
/// use std::io::{BufReader, IoResult};
///
/// let buffer = "1\n2\n3\n4\n";
/// let mut reader = BufReader::new(buffer.as_bytes());
/// let mut buffer = "1\n2\n3\n4\n";
///
/// let mut sum = 0;
///
/// while !
reader.eof
() {
/// let line: IoResult<String> =
read
er.read_line();
/// while !
buffer.is_empty
() {
/// let line: IoResult<String> =
buff
er.read_line();
/// // Convert the string line to a number using `map` and `from_str`
/// let val: IoResult<int> = line.map(|line| {
/// from_str::<int>(line.as_slice().trim_right()).unwrap_or(0)
...
...
src/libgraphviz/lib.rs
浏览文件 @
0d247807
...
...
@@ -547,7 +547,7 @@ mod tests {
use
self
::
NodeLabels
::
*
;
use
super
::{
Id
,
LabelText
,
LabelStr
,
EscStr
,
Labeller
};
use
super
::{
Nodes
,
Edges
,
GraphWalk
,
render
};
use
std
::
io
::
{
BufReader
,
IoResult
}
;
use
std
::
io
::
IoResult
;
use
std
::
str
;
/// each node is an index in a vector in the graph.
...
...
@@ -698,8 +698,7 @@ fn target(&'a self, edge: & &'a Edge) -> Node {
fn
test_input
(
g
:
LabelledGraph
)
->
IoResult
<
String
>
{
let
mut
writer
=
Vec
::
new
();
render
(
&
g
,
&
mut
writer
)
.unwrap
();
let
mut
r
=
BufReader
::
new
(
writer
[]);
r
.read_to_string
()
(
&
mut
writer
.as_slice
())
.read_to_string
()
}
// All of the tests use raw-strings as the format for the expected outputs,
...
...
@@ -811,8 +810,7 @@ fn left_aligned_text() {
edge
(
1
,
3
,
";"
),
edge
(
2
,
3
,
";"
)));
render
(
&
g
,
&
mut
writer
)
.unwrap
();
let
mut
r
=
BufReader
::
new
(
writer
[]);
let
r
=
r
.read_to_string
();
let
r
=
(
&
mut
writer
.as_slice
())
.read_to_string
();
assert_eq!
(
r
.unwrap
()
.as_slice
(),
r#"digraph syntax_tree {
...
...
src/libstd/io/buffered.rs
浏览文件 @
0d247807
...
...
@@ -406,7 +406,7 @@ mod test {
use
prelude
::
*
;
use
super
::
*
;
use
super
::
super
::{
IoResult
,
EndOfFile
};
use
super
::
super
::
mem
::
{
MemReader
,
BufReader
}
;
use
super
::
super
::
mem
::
MemReader
;
use
self
::
test
::
Bencher
;
use
str
::
StrPrelude
;
...
...
@@ -626,14 +626,14 @@ fn test_short_reads() {
#[test]
fn
read_char_buffered
()
{
let
buf
=
[
195u8
,
159u8
];
let
mut
reader
=
BufferedReader
::
with_capacity
(
1
,
BufReader
::
new
(
&
buf
)
);
let
mut
reader
=
BufferedReader
::
with_capacity
(
1
,
buf
[]
);
assert_eq!
(
reader
.read_char
(),
Ok
(
'ß'
));
}
#[test]
fn
test_chars
()
{
let
buf
=
[
195u8
,
159u8
,
b
'a'
];
let
mut
reader
=
BufferedReader
::
with_capacity
(
1
,
BufReader
::
new
(
&
buf
)
);
let
mut
reader
=
BufferedReader
::
with_capacity
(
1
,
buf
[]
);
let
mut
it
=
reader
.chars
();
assert_eq!
(
it
.next
(),
Some
(
Ok
(
'ß'
)));
assert_eq!
(
it
.next
(),
Some
(
Ok
(
'a'
)));
...
...
src/libstd/io/mem.rs
浏览文件 @
0d247807
...
...
@@ -206,6 +206,41 @@ fn fill_buf<'a>(&'a mut self) -> IoResult<&'a [u8]> {
fn
consume
(
&
mut
self
,
amt
:
uint
)
{
self
.pos
+=
amt
;
}
}
impl
<
'a
>
Reader
for
&
'a
[
u8
]
{
#[inline]
fn
read
(
&
mut
self
,
buf
:
&
mut
[
u8
])
->
IoResult
<
uint
>
{
if
self
.is_empty
()
{
return
Err
(
io
::
standard_error
(
io
::
EndOfFile
));
}
let
write_len
=
min
(
buf
.len
(),
self
.len
());
{
let
input
=
self
[
..
write_len
];
let
output
=
buf
[
mut
..
write_len
];
slice
::
bytes
::
copy_memory
(
output
,
input
);
}
*
self
=
self
.slice_from
(
write_len
);
Ok
(
write_len
)
}
}
impl
<
'a
>
Buffer
for
&
'a
[
u8
]
{
#[inline]
fn
fill_buf
<
'a
>
(
&
'a
mut
self
)
->
IoResult
<&
'a
[
u8
]
>
{
if
self
.is_empty
()
{
Err
(
io
::
standard_error
(
io
::
EndOfFile
))
}
else
{
Ok
(
*
self
)
}
}
#[inline]
fn
consume
(
&
mut
self
,
amt
:
uint
)
{
*
self
=
self
[
amt
..
];
}
}
/// Writes to a fixed-size byte slice
///
/// If a write will not fit in the buffer, it returns an error and does not
...
...
@@ -469,6 +504,32 @@ fn test_mem_reader() {
assert
!
(
reader
.read
(
&
mut
buf
)
.is_err
());
}
#[test]
fn
test_slice_reader
()
{
let
in_buf
=
vec!
[
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
];
let
mut
reader
=
&
mut
in_buf
.as_slice
();
let
mut
buf
=
[];
assert_eq!
(
reader
.read
(
&
mut
buf
),
Ok
(
0
));
let
mut
buf
=
[
0
];
assert_eq!
(
reader
.read
(
&
mut
buf
),
Ok
(
1
));
assert_eq!
(
reader
.len
(),
7
);
let
b
:
&
[
_
]
=
&
[
0
];
assert_eq!
(
buf
.as_slice
(),
b
);
let
mut
buf
=
[
0
,
..
4
];
assert_eq!
(
reader
.read
(
&
mut
buf
),
Ok
(
4
));
assert_eq!
(
reader
.len
(),
3
);
let
b
:
&
[
_
]
=
&
[
1
,
2
,
3
,
4
];
assert_eq!
(
buf
.as_slice
(),
b
);
assert_eq!
(
reader
.read
(
&
mut
buf
),
Ok
(
3
));
let
b
:
&
[
_
]
=
&
[
5
,
6
,
7
];
assert_eq!
(
buf
[
0
..
3
],
b
);
assert
!
(
reader
.read
(
&
mut
buf
)
.is_err
());
let
mut
reader
=
&
mut
in_buf
.as_slice
();
assert_eq!
(
reader
.read_until
(
3
)
.unwrap
(),
vec!
(
0
,
1
,
2
,
3
));
assert_eq!
(
reader
.read_until
(
3
)
.unwrap
(),
vec!
(
4
,
5
,
6
,
7
));
assert
!
(
reader
.read
(
&
mut
buf
)
.is_err
());
}
#[test]
fn
test_buf_reader
()
{
let
in_buf
=
vec!
[
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
];
...
...
src/libstd/io/util.rs
浏览文件 @
0d247807
...
...
@@ -273,7 +273,7 @@ fn read(&mut self, buf: &mut [u8]) -> io::IoResult<uint> {
#[cfg(test)]
mod
test
{
use
io
::{
MemReader
,
B
ufReader
,
B
yRefReader
};
use
io
::{
MemReader
,
ByRefReader
};
use
io
;
use
boxed
::
Box
;
use
super
::
*
;
...
...
@@ -395,8 +395,7 @@ fn test_copy() {
#[test]
fn
limit_reader_buffer
()
{
let
data
=
"0123456789
\n
0123456789
\n
"
;
let
mut
r
=
BufReader
::
new
(
data
.as_bytes
());
let
r
=
&
mut
b
"0123456789
\n
0123456789
\n
"
;
{
let
mut
r
=
LimitReader
::
new
(
r
.by_ref
(),
3
);
assert_eq!
(
r
.read_line
(),
Ok
(
"012"
.to_string
()));
...
...
src/libtime/lib.rs
浏览文件 @
0d247807
...
...
@@ -32,7 +32,6 @@
use
std
::
fmt
::
Show
;
use
std
::
fmt
;
use
std
::
io
::
BufReader
;
use
std
::
num
::
SignedInt
;
use
std
::
string
::
String
;
use
std
::
time
::
Duration
;
...
...
@@ -1187,7 +1186,7 @@ fn parse_type(s: &str, pos: uint, ch: char, tm: &mut Tm)
}
}
let
mut
rdr
=
BufReader
::
new
(
format
.as_bytes
()
);
let
mut
rdr
:
&
[
u8
]
=
format
.as_bytes
(
);
let
mut
tm
=
Tm
{
tm_sec
:
0_i32
,
tm_min
:
0_i32
,
...
...
@@ -1211,13 +1210,13 @@ fn parse_type(s: &str, pos: uint, ch: char, tm: &mut Tm)
let
next
=
range
.next
;
let
mut
buf
=
[
0
];
let
c
=
match
rdr
.read
(
&
mut
buf
)
{
let
c
=
match
(
&
mut
rdr
)
.read
(
&
mut
buf
)
{
Ok
(
..
)
=>
buf
[
0
]
as
char
,
Err
(
..
)
=>
break
};
match
c
{
'%'
=>
{
let
ch
=
match
rdr
.read
(
&
mut
buf
)
{
let
ch
=
match
(
&
mut
rdr
)
.read
(
&
mut
buf
)
{
Ok
(
..
)
=>
buf
[
0
]
as
char
,
Err
(
..
)
=>
break
};
...
...
@@ -1233,7 +1232,7 @@ fn parse_type(s: &str, pos: uint, ch: char, tm: &mut Tm)
}
}
if
pos
==
len
&&
rdr
.tell
()
.unwrap
()
==
format
.len
()
as
u64
{
if
pos
==
len
&&
(
&
mut
rdr
)
.is_empty
()
{
Ok
(
Tm
{
tm_sec
:
tm
.tm_sec
,
tm_min
:
tm
.tm_min
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录