Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
562ba04e
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,发现更多精彩内容 >>
提交
562ba04e
编写于
1月 11, 2018
作者:
D
Diggory Blake
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implement "seek_relative"
上级
a18dea90
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
42 addition
and
0 deletion
+42
-0
src/libstd/io/buffered.rs
src/libstd/io/buffered.rs
+42
-0
未找到文件。
src/libstd/io/buffered.rs
浏览文件 @
562ba04e
...
@@ -194,6 +194,31 @@ pub fn is_empty(&self) -> bool {
...
@@ -194,6 +194,31 @@ pub fn is_empty(&self) -> bool {
pub
fn
into_inner
(
self
)
->
R
{
self
.inner
}
pub
fn
into_inner
(
self
)
->
R
{
self
.inner
}
}
}
impl
<
R
:
Seek
>
BufReader
<
R
>
{
/// Seeks relative to the current position. If the new position lies within the buffer,
/// the buffer will not be flushed, allowing for more efficient seeks.
/// This method does not return the location of the underlying reader, so the caller
/// must track this information themselves if it is required.
#[unstable(feature
=
"bufreader_seek_relative"
,
issue
=
"31100"
)]
pub
fn
seek_relative
(
&
mut
self
,
offset
:
i64
)
->
io
::
Result
<
()
>
{
let
pos
=
self
.pos
as
u64
;
if
offset
<
0
{
if
let
Some
(
new_pos
)
=
pos
.checked_sub
((
-
offset
)
as
u64
)
{
self
.pos
=
new_pos
as
usize
;
return
Ok
(())
}
}
else
{
if
let
Some
(
new_pos
)
=
pos
.checked_add
(
offset
as
u64
)
{
if
new_pos
<=
self
.cap
as
u64
{
self
.pos
=
new_pos
as
usize
;
return
Ok
(())
}
}
}
self
.seek
(
SeekFrom
::
Current
(
offset
))
.map
(|
_
|())
}
}
#[stable(feature
=
"rust1"
,
since
=
"1.0.0"
)]
#[stable(feature
=
"rust1"
,
since
=
"1.0.0"
)]
impl
<
R
:
Read
>
Read
for
BufReader
<
R
>
{
impl
<
R
:
Read
>
Read
for
BufReader
<
R
>
{
fn
read
(
&
mut
self
,
buf
:
&
mut
[
u8
])
->
io
::
Result
<
usize
>
{
fn
read
(
&
mut
self
,
buf
:
&
mut
[
u8
])
->
io
::
Result
<
usize
>
{
...
@@ -942,6 +967,23 @@ fn test_buffered_reader_seek() {
...
@@ -942,6 +967,23 @@ fn test_buffered_reader_seek() {
assert_eq!
(
reader
.seek
(
SeekFrom
::
Current
(
-
2
))
.ok
(),
Some
(
3
));
assert_eq!
(
reader
.seek
(
SeekFrom
::
Current
(
-
2
))
.ok
(),
Some
(
3
));
}
}
#[test]
fn
test_buffered_reader_seek_relative
()
{
let
inner
:
&
[
u8
]
=
&
[
5
,
6
,
7
,
0
,
1
,
2
,
3
,
4
];
let
mut
reader
=
BufReader
::
with_capacity
(
2
,
io
::
Cursor
::
new
(
inner
));
assert
!
(
reader
.seek_relative
(
3
)
.is_ok
());
assert_eq!
(
reader
.fill_buf
()
.ok
(),
Some
(
&
[
0
,
1
][
..
]));
assert
!
(
reader
.seek_relative
(
0
)
.is_ok
());
assert_eq!
(
reader
.fill_buf
()
.ok
(),
Some
(
&
[
0
,
1
][
..
]));
assert
!
(
reader
.seek_relative
(
1
)
.is_ok
());
assert_eq!
(
reader
.fill_buf
()
.ok
(),
Some
(
&
[
1
][
..
]));
assert
!
(
reader
.seek_relative
(
-
1
)
.is_ok
());
assert_eq!
(
reader
.fill_buf
()
.ok
(),
Some
(
&
[
0
,
1
][
..
]));
assert
!
(
reader
.seek_relative
(
2
)
.is_ok
());
assert_eq!
(
reader
.fill_buf
()
.ok
(),
Some
(
&
[
2
,
3
][
..
]));
}
#[test]
#[test]
fn
test_buffered_reader_seek_underflow
()
{
fn
test_buffered_reader_seek_underflow
()
{
// gimmick reader that yields its position modulo 256 for each byte
// gimmick reader that yields its position modulo 256 for each byte
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录