Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
75fe66e3
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,发现更多精彩内容 >>
提交
75fe66e3
编写于
12月 22, 2016
作者:
A
Andrew Paseltiner
提交者:
Aidan Hobson Sayers
12月 23, 2016
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implement placement-in protocol for `Vec`
上级
17d873c2
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
95 addition
and
1 deletion
+95
-1
src/libcollections/lib.rs
src/libcollections/lib.rs
+1
-0
src/libcollections/vec.rs
src/libcollections/vec.rs
+73
-1
src/libcollectionstest/lib.rs
src/libcollectionstest/lib.rs
+2
-0
src/libcollectionstest/vec.rs
src/libcollectionstest/vec.rs
+19
-0
未找到文件。
src/libcollections/lib.rs
浏览文件 @
75fe66e3
...
...
@@ -45,6 +45,7 @@
#![feature(nonzero)]
#![feature(pattern)]
#![feature(placement_in)]
#![feature(placement_in_syntax)]
#![feature(placement_new_protocol)]
#![feature(shared)]
#![feature(slice_get_slice)]
...
...
src/libcollections/vec.rs
浏览文件 @
75fe66e3
...
...
@@ -77,7 +77,7 @@
use
core
::
intrinsics
::{
arith_offset
,
assume
};
use
core
::
iter
::{
FromIterator
,
FusedIterator
,
TrustedLen
};
use
core
::
mem
;
use
core
::
ops
::{
In
dex
,
IndexMut
};
use
core
::
ops
::{
In
Place
,
Index
,
IndexMut
,
Place
,
Placer
};
use
core
::
ops
;
use
core
::
ptr
;
use
core
::
ptr
::
Shared
;
...
...
@@ -1246,6 +1246,29 @@ fn extend_with_element(&mut self, n: usize, value: T) {
pub
fn
extend_from_slice
(
&
mut
self
,
other
:
&
[
T
])
{
self
.spec_extend
(
other
.iter
())
}
/// Returns a place for insertion at the back of the `Vec`.
///
/// Using this method with placement syntax is equivalent to [`push`](#method.push),
/// but may be more efficient.
///
/// # Examples
///
/// ```
/// #![feature(collection_placement)]
/// #![feature(placement_in_syntax)]
///
/// let mut vec = vec![1, 2];
/// vec.place_back() <- 3;
/// vec.place_back() <- 4;
/// assert_eq!(&vec, &[1, 2, 3, 4]);
/// ```
#[unstable(feature
=
"collection_placement"
,
reason
=
"placement protocol is subject to change"
,
issue
=
"30172"
)]
pub
fn
place_back
(
&
mut
self
)
->
PlaceBack
<
T
>
{
PlaceBack
{
vec
:
self
}
}
}
// Set the length of the vec when the `SetLenOnDrop` value goes out of scope.
...
...
@@ -2119,3 +2142,52 @@ fn is_empty(&self) -> bool {
#[unstable(feature
=
"fused"
,
issue
=
"35602"
)]
impl
<
'a
,
T
>
FusedIterator
for
Drain
<
'a
,
T
>
{}
/// A place for insertion at the back of a `Vec`.
///
/// See [`Vec::place_back`](struct.Vec.html#method.place_back) for details.
#[must_use
=
"places do nothing unless written to with `<-` syntax"
]
#[unstable(feature
=
"collection_placement"
,
reason
=
"struct name and placement protocol are subject to change"
,
issue
=
"30172"
)]
pub
struct
PlaceBack
<
'a
,
T
:
'a
>
{
vec
:
&
'a
mut
Vec
<
T
>
,
}
#[unstable(feature
=
"collection_placement"
,
reason
=
"placement protocol is subject to change"
,
issue
=
"30172"
)]
impl
<
'a
,
T
>
Placer
<
T
>
for
PlaceBack
<
'a
,
T
>
{
type
Place
=
PlaceBack
<
'a
,
T
>
;
fn
make_place
(
self
)
->
Self
{
// This will panic or abort if we would allocate > isize::MAX bytes
// or if the length increment would overflow for zero-sized types.
if
self
.vec.len
==
self
.vec.buf
.cap
()
{
self
.vec.buf
.double
();
}
self
}
}
#[unstable(feature
=
"collection_placement"
,
reason
=
"placement protocol is subject to change"
,
issue
=
"30172"
)]
impl
<
'a
,
T
>
Place
<
T
>
for
PlaceBack
<
'a
,
T
>
{
fn
pointer
(
&
mut
self
)
->
*
mut
T
{
unsafe
{
self
.vec
.as_mut_ptr
()
.offset
(
self
.vec.len
as
isize
)
}
}
}
#[unstable(feature
=
"collection_placement"
,
reason
=
"placement protocol is subject to change"
,
issue
=
"30172"
)]
impl
<
'a
,
T
>
InPlace
<
T
>
for
PlaceBack
<
'a
,
T
>
{
type
Owner
=
&
'a
mut
T
;
unsafe
fn
finalize
(
mut
self
)
->
&
'a
mut
T
{
let
ptr
=
self
.pointer
();
self
.vec.len
+=
1
;
&
mut
*
ptr
}
}
src/libcollectionstest/lib.rs
浏览文件 @
75fe66e3
...
...
@@ -13,6 +13,7 @@
#![feature(binary_heap_extras)]
#![feature(box_syntax)]
#![feature(btree_range)]
#![feature(collection_placement)]
#![feature(collections)]
#![feature(collections_bound)]
#![feature(const_fn)]
...
...
@@ -20,6 +21,7 @@
#![feature(enumset)]
#![feature(exact_size_is_empty)]
#![feature(pattern)]
#![feature(placement_in_syntax)]
#![feature(rand)]
#![feature(repeat_str)]
#![feature(step_by)]
...
...
src/libcollectionstest/vec.rs
浏览文件 @
75fe66e3
...
...
@@ -12,6 +12,7 @@
use
std
::
borrow
::
Cow
;
use
std
::
iter
::{
FromIterator
,
repeat
};
use
std
::
mem
::
size_of
;
use
std
::
panic
;
use
std
::
vec
::{
Drain
,
IntoIter
};
use
test
::
Bencher
;
...
...
@@ -615,6 +616,24 @@ fn into_iter<'new>(i: IntoIter<&'static str>) -> IntoIter<&'new str> {
}
}
#[test]
fn
test_placement
()
{
let
mut
vec
=
vec!
[
1
];
assert_eq!
(
vec
.place_back
()
<-
2
,
&
2
);
assert_eq!
(
vec
.len
(),
2
);
assert_eq!
(
vec
.place_back
()
<-
3
,
&
3
);
assert_eq!
(
vec
.len
(),
3
);
assert_eq!
(
&
vec
,
&
[
1
,
2
,
3
]);
}
#[test]
fn
test_placement_panic
()
{
let
mut
vec
=
vec!
[
1
,
2
,
3
];
fn
mkpanic
()
->
usize
{
panic!
()
}
let
_
=
panic
::
catch_unwind
(
panic
::
AssertUnwindSafe
(||
{
vec
.place_back
()
<-
mkpanic
();
}));
assert_eq!
(
vec
.len
(),
3
);
}
#[bench]
fn
bench_new
(
b
:
&
mut
Bencher
)
{
b
.iter
(||
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录