Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
d14a647d
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,发现更多精彩内容 >>
提交
d14a647d
编写于
11月 08, 2013
作者:
B
bors
浏览文件
操作
浏览文件
下载
差异文件
auto merge of #10376 : thestinger/rust/clone, r=huonw
Closes #10240
上级
a594a999
6943ca8d
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
71 addition
and
3 deletion
+71
-3
src/libstd/clone.rs
src/libstd/clone.rs
+48
-2
src/libstd/vec.rs
src/libstd/vec.rs
+23
-1
未找到文件。
src/libstd/clone.rs
浏览文件 @
d14a647d
...
...
@@ -29,12 +29,27 @@ pub trait Clone {
/// are copied to maintain uniqueness, while the contents of
/// managed pointers are not copied.
fn
clone
(
&
self
)
->
Self
;
/// Perform copy-assignment from `source`.
///
/// `a.clone_from(&b)` is equivalent to `a = b.clone()` in functionality,
/// but can be overriden to reuse the resources of `a` to avoid unnecessary
/// allocations.
#[inline(always)]
fn
clone_from
(
&
mut
self
,
source
:
&
Self
)
{
*
self
=
source
.clone
()
}
}
impl
<
T
:
Clone
>
Clone
for
~
T
{
/// Return a
deep
copy of the owned box.
/// Return a copy of the owned box.
#[inline]
fn
clone
(
&
self
)
->
~
T
{
~
(
**
self
)
.clone
()
}
/// Perform copy-assignment from `source` by reusing the existing allocation.
fn
clone_from
(
&
mut
self
,
source
:
&~
T
)
{
**
self
=
(
**
source
)
.clone
()
}
}
impl
<
T
>
Clone
for
@
T
{
...
...
@@ -118,16 +133,31 @@ fn clone(&self) -> extern "Rust" fn($($A),*) -> ReturnType { *self }
/// A trait distinct from `Clone` which represents "deep copies" of things like
/// managed boxes which would otherwise not be copied.
pub
trait
DeepClone
{
pub
trait
DeepClone
:
Clone
{
/// Return a deep copy of the value. Unlike `Clone`, the contents of shared pointer types
/// *are* copied.
fn
deep_clone
(
&
self
)
->
Self
;
/// Perform deep copy-assignment from `source`.
///
/// `a.deep_clone_from(&b)` is equivalent to `a = b.deep_clone()` in
/// functionality, but can be overriden to reuse the resources of `a` to
/// avoid unnecessary allocations.
#[inline(always)]
fn
deep_clone_from
(
&
mut
self
,
source
:
&
Self
)
{
*
self
=
source
.deep_clone
()
}
}
impl
<
T
:
DeepClone
>
DeepClone
for
~
T
{
/// Return a deep copy of the owned box.
#[inline]
fn
deep_clone
(
&
self
)
->
~
T
{
~
(
**
self
)
.deep_clone
()
}
/// Perform deep copy-assignment from `source` by reusing the existing allocation.
fn
deep_clone_from
(
&
mut
self
,
source
:
&~
T
)
{
**
self
=
(
**
source
)
.deep_clone
()
}
}
// FIXME: #6525: should also be implemented for `T: Send + DeepClone`
...
...
@@ -234,6 +264,22 @@ fn test_borrowed_clone() {
assert_eq!
(
*
z
,
5
);
}
#[test]
fn
test_clone_from
()
{
let
a
=
~
5
;
let
mut
b
=
~
10
;
b
.clone_from
(
&
a
);
assert_eq!
(
*
b
,
5
);
}
#[test]
fn
test_deep_clone_from
()
{
let
a
=
~
5
;
let
mut
b
=
~
10
;
b
.deep_clone_from
(
&
a
);
assert_eq!
(
*
b
,
5
);
}
#[test]
fn
test_extern_fn_clone
()
{
trait
Empty
{}
...
...
src/libstd/vec.rs
浏览文件 @
d14a647d
...
...
@@ -2029,7 +2029,7 @@ impl<'self, T:Clone> MutableCloneableVector<T> for &'self mut [T] {
#[inline]
fn
copy_from
(
self
,
src
:
&
[
T
])
->
uint
{
for
(
a
,
b
)
in
self
.mut_iter
()
.zip
(
src
.iter
())
{
*
a
=
b
.clone
(
);
a
.clone_from
(
b
);
}
cmp
::
min
(
self
.len
(),
src
.len
())
}
...
...
@@ -2282,6 +2282,17 @@ impl<A: Clone> Clone for ~[A] {
fn
clone
(
&
self
)
->
~
[
A
]
{
self
.iter
()
.map
(|
item
|
item
.clone
())
.collect
()
}
fn
clone_from
(
&
mut
self
,
source
:
&~
[
A
])
{
if
self
.len
()
<
source
.len
()
{
*
self
=
source
.clone
()
}
else
{
self
.truncate
(
source
.len
());
for
(
x
,
y
)
in
self
.mut_iter
()
.zip
(
source
.iter
())
{
x
.clone_from
(
y
);
}
}
}
}
impl
<
A
:
DeepClone
>
DeepClone
for
~
[
A
]
{
...
...
@@ -2289,6 +2300,17 @@ impl<A: DeepClone> DeepClone for ~[A] {
fn
deep_clone
(
&
self
)
->
~
[
A
]
{
self
.iter
()
.map
(|
item
|
item
.deep_clone
())
.collect
()
}
fn
deep_clone_from
(
&
mut
self
,
source
:
&~
[
A
])
{
if
self
.len
()
<
source
.len
()
{
*
self
=
source
.deep_clone
()
}
else
{
self
.truncate
(
source
.len
());
for
(
x
,
y
)
in
self
.mut_iter
()
.zip
(
source
.iter
())
{
x
.deep_clone_from
(
y
);
}
}
}
}
// This works because every lifetime is a sub-lifetime of 'static
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录