Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
b65ebc40
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,发现更多精彩内容 >>
提交
b65ebc40
编写于
3月 13, 2015
作者:
J
Jorge Aparicio
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
better document the hacks required to test libcollections
上级
8256241d
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
133 addition
and
103 deletion
+133
-103
src/libcollections/macros.rs
src/libcollections/macros.rs
+4
-2
src/libcollections/slice.rs
src/libcollections/slice.rs
+116
-95
src/libcollections/string.rs
src/libcollections/string.rs
+6
-4
src/libcollections/vec.rs
src/libcollections/vec.rs
+7
-2
未找到文件。
src/libcollections/macros.rs
浏览文件 @
b65ebc40
...
@@ -84,8 +84,10 @@
...
@@ -84,8 +84,10 @@
(
$
(
$x:expr
,)
*
)
=>
(
vec!
[
$
(
$x
),
*
])
(
$
(
$x:expr
,)
*
)
=>
(
vec!
[
$
(
$x
),
*
])
}
}
// HACK: `impl [T]` is not available in cfg(test), use `::slice::into_vec`, instead of
// HACK(japaric): with cfg(test) the inherent `[T]::into_vec` method, which is required for this
// `<[T]>::to_vec`
// macro definition, is not available. Instead use the `slice::into_vec` function which is only
// available with cfg(test)
// NB see the slice::hack module in slice.rs for more information
#[cfg(not(stage0))]
#[cfg(not(stage0))]
#[cfg(test)]
#[cfg(test)]
macro_rules!
vec
{
macro_rules!
vec
{
...
...
src/libcollections/slice.rs
浏览文件 @
b65ebc40
...
@@ -1083,30 +1083,113 @@ fn into_vec(mut self: Box<Self>) -> Vec<T> {
...
@@ -1083,30 +1083,113 @@ fn into_vec(mut self: Box<Self>) -> Vec<T> {
}
}
}
}
// HACK: With cfg(test) `impl [T]` is not available, these three functions are actually methods
// HACK(japaric) needed for the implementation of `vec!` macro during testing
// that are in `impl [T]` but not in `core::slice::SliceExt` - this is only need for testing
// NB see the hack module in this file for more details
#[cfg(not(stage0))]
#[cfg(test)]
#[cfg(test)]
pub
fn
into_vec
<
T
>
(
mut
b
:
Box
<
[
T
]
>
)
->
Vec
<
T
>
{
pub
use
self
::
hack
::
into_vec
;
unsafe
{
let
xs
=
Vec
::
from_raw_parts
(
b
.as_mut_ptr
(),
b
.len
(),
b
.len
());
mem
::
forget
(
b
);
xs
}
}
// HACK(japaric) needed for the implementation of `Vec::clone` during testing
// NB see the hack module in this file for more details
#[cfg(not(stage0))]
#[cfg(test)]
#[cfg(test)]
pub
fn
permutations
<
T
>
(
s
:
&
[
T
])
->
Permutations
<
T
>
where
T
:
Clone
{
pub
use
self
::
hack
::
to_vec
;
Permutations
{
swaps
:
ElementSwaps
::
new
(
s
.len
()),
// HACK(japaric): With cfg(test) `impl [T]` is not available, these three functions are actually
v
:
::
slice
::
to_vec
(
s
),
// methods that are in `impl [T]` but not in `core::slice::SliceExt` - we need to supply these
// functions for the `test_permutations` test
#[cfg(not(stage0))]
mod
hack
{
use
alloc
::
boxed
::
Box
;
use
core
::
clone
::
Clone
;
#[cfg(test)]
use
core
::
iter
::{
Iterator
,
IteratorExt
};
use
core
::
mem
;
#[cfg(test)]
use
core
::
option
::
Option
::{
Some
,
None
};
#[cfg(test)]
use
string
::
ToString
;
use
vec
::
Vec
;
use
super
::{
ElementSwaps
,
Permutations
};
pub
fn
into_vec
<
T
>
(
mut
b
:
Box
<
[
T
]
>
)
->
Vec
<
T
>
{
unsafe
{
let
xs
=
Vec
::
from_raw_parts
(
b
.as_mut_ptr
(),
b
.len
(),
b
.len
());
mem
::
forget
(
b
);
xs
}
}
}
}
#[cfg(test)]
pub
fn
permutations
<
T
>
(
s
:
&
[
T
])
->
Permutations
<
T
>
where
T
:
Clone
{
pub
fn
to_vec
<
T
>
(
s
:
&
[
T
])
->
Vec
<
T
>
where
T
:
Clone
{
Permutations
{
let
mut
vector
=
Vec
::
with_capacity
(
s
.len
());
swaps
:
ElementSwaps
::
new
(
s
.len
()),
vector
.push_all
(
s
);
v
:
to_vec
(
s
),
vector
}
}
#[inline]
pub
fn
to_vec
<
T
>
(
s
:
&
[
T
])
->
Vec
<
T
>
where
T
:
Clone
{
let
mut
vector
=
Vec
::
with_capacity
(
s
.len
());
vector
.push_all
(
s
);
vector
}
// NB we can remove this hack if we move this test to libcollectionstest - but that can't be
// done right now because the test needs access to the private fields of Permutations
#[test]
fn
test_permutations
()
{
{
let
v
:
[
i32
;
0
]
=
[];
let
mut
it
=
permutations
(
&
v
);
let
(
min_size
,
max_opt
)
=
it
.size_hint
();
assert_eq!
(
min_size
,
1
);
assert_eq!
(
max_opt
.unwrap
(),
1
);
assert_eq!
(
it
.next
(),
Some
(
to_vec
(
&
v
)));
assert_eq!
(
it
.next
(),
None
);
}
{
let
v
=
[
"Hello"
.to_string
()];
let
mut
it
=
permutations
(
&
v
);
let
(
min_size
,
max_opt
)
=
it
.size_hint
();
assert_eq!
(
min_size
,
1
);
assert_eq!
(
max_opt
.unwrap
(),
1
);
assert_eq!
(
it
.next
(),
Some
(
to_vec
(
&
v
)));
assert_eq!
(
it
.next
(),
None
);
}
{
let
v
=
[
1
,
2
,
3
];
let
mut
it
=
permutations
(
&
v
);
let
(
min_size
,
max_opt
)
=
it
.size_hint
();
assert_eq!
(
min_size
,
3
*
2
);
assert_eq!
(
max_opt
.unwrap
(),
3
*
2
);
assert_eq!
(
it
.next
()
.unwrap
(),
[
1
,
2
,
3
]);
assert_eq!
(
it
.next
()
.unwrap
(),
[
1
,
3
,
2
]);
assert_eq!
(
it
.next
()
.unwrap
(),
[
3
,
1
,
2
]);
let
(
min_size
,
max_opt
)
=
it
.size_hint
();
assert_eq!
(
min_size
,
3
);
assert_eq!
(
max_opt
.unwrap
(),
3
);
assert_eq!
(
it
.next
()
.unwrap
(),
[
3
,
2
,
1
]);
assert_eq!
(
it
.next
()
.unwrap
(),
[
2
,
3
,
1
]);
assert_eq!
(
it
.next
()
.unwrap
(),
[
2
,
1
,
3
]);
assert_eq!
(
it
.next
(),
None
);
}
{
// check that we have N! permutations
let
v
=
[
'A'
,
'B'
,
'C'
,
'D'
,
'E'
,
'F'
];
let
mut
amt
=
0
;
let
mut
it
=
permutations
(
&
v
);
let
(
min_size
,
max_opt
)
=
it
.size_hint
();
for
_
perm
in
it
.by_ref
()
{
amt
+=
1
;
}
assert_eq!
(
amt
,
it
.swaps.swaps_made
);
assert_eq!
(
amt
,
min_size
);
assert_eq!
(
amt
,
2
*
3
*
4
*
5
*
6
);
assert_eq!
(
amt
,
max_opt
.unwrap
());
}
}
}
}
#[cfg(not(stage0))]
#[cfg(not(stage0))]
...
@@ -1715,9 +1798,8 @@ pub fn as_mut_ptr(&mut self) -> *mut T {
...
@@ -1715,9 +1798,8 @@ pub fn as_mut_ptr(&mut self) -> *mut T {
#[stable(feature
=
"rust1"
,
since
=
"1.0.0"
)]
#[stable(feature
=
"rust1"
,
since
=
"1.0.0"
)]
#[inline]
#[inline]
pub
fn
to_vec
(
&
self
)
->
Vec
<
T
>
where
T
:
Clone
{
pub
fn
to_vec
(
&
self
)
->
Vec
<
T
>
where
T
:
Clone
{
let
mut
vector
=
Vec
::
with_capacity
(
self
.len
());
// NB see hack module in this file
vector
.push_all
(
self
);
hack
::
to_vec
(
self
)
vector
}
}
/// Creates an iterator that yields every possible permutation of the
/// Creates an iterator that yields every possible permutation of the
...
@@ -1745,11 +1827,10 @@ pub fn to_vec(&self) -> Vec<T> where T: Clone {
...
@@ -1745,11 +1827,10 @@ pub fn to_vec(&self) -> Vec<T> where T: Clone {
/// assert_eq!(Some(vec![3, 1, 2]), perms.next());
/// assert_eq!(Some(vec![3, 1, 2]), perms.next());
/// ```
/// ```
#[unstable(feature
=
"collections"
)]
#[unstable(feature
=
"collections"
)]
#[inline]
pub
fn
permutations
(
&
self
)
->
Permutations
<
T
>
where
T
:
Clone
{
pub
fn
permutations
(
&
self
)
->
Permutations
<
T
>
where
T
:
Clone
{
Permutations
{
// NB see hack module in this file
swaps
:
ElementSwaps
::
new
(
self
.len
()),
hack
::
permutations
(
self
)
v
:
self
.to_vec
(),
}
}
}
/// Copies as many elements from `src` as it can into `self` (the
/// Copies as many elements from `src` as it can into `self` (the
...
@@ -1931,12 +2012,10 @@ pub fn ends_with(&self, needle: &[T]) -> bool where T: PartialEq {
...
@@ -1931,12 +2012,10 @@ pub fn ends_with(&self, needle: &[T]) -> bool where T: PartialEq {
/// Convert `self` into a vector without clones or allocation.
/// Convert `self` into a vector without clones or allocation.
#[stable(feature
=
"rust1"
,
since
=
"1.0.0"
)]
#[stable(feature
=
"rust1"
,
since
=
"1.0.0"
)]
pub
fn
into_vec
(
mut
self
:
Box
<
Self
>
)
->
Vec
<
T
>
{
#[inline]
unsafe
{
pub
fn
into_vec
(
self
:
Box
<
Self
>
)
->
Vec
<
T
>
{
let
xs
=
Vec
::
from_raw_parts
(
self
.as_mut_ptr
(),
self
.len
(),
self
.len
());
// NB see hack module in this file
mem
::
forget
(
self
);
hack
::
into_vec
(
self
)
xs
}
}
}
}
}
...
@@ -2052,10 +2131,12 @@ impl<T: Clone> ToOwned for [T] {
...
@@ -2052,10 +2131,12 @@ impl<T: Clone> ToOwned for [T] {
#[cfg(not(test))]
#[cfg(not(test))]
fn
to_owned
(
&
self
)
->
Vec
<
T
>
{
self
.to_vec
()
}
fn
to_owned
(
&
self
)
->
Vec
<
T
>
{
self
.to_vec
()
}
// HACK: `impl [T]` is not available in cfg(test), use `::slice::to_vec` instead of
// HACK(japaric): with cfg(test) the inherent `[T]::to_vec`, which is required for this method
// `<[T]>::to_vec`
// definition, is not available. Since we don't require this method for testing purposes, I'll
// just stub it
// NB see the slice::hack module in slice.rs for more information
#[cfg(test)]
#[cfg(test)]
fn
to_owned
(
&
self
)
->
Vec
<
T
>
{
::
slice
::
to_vec
(
self
)
}
fn
to_owned
(
&
self
)
->
Vec
<
T
>
{
panic!
(
"not available with cfg(test)"
)
}
}
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
...
@@ -2372,63 +2453,3 @@ unsafe fn step<T>(ptr: &mut *mut T) -> *mut T {
...
@@ -2372,63 +2453,3 @@ unsafe fn step<T>(ptr: &mut *mut T) -> *mut T {
old
old
}
}
}
}
#[cfg(test)]
mod
test
{
use
core
::
iter
::{
Iterator
,
IteratorExt
};
use
core
::
option
::
Option
::{
None
,
Some
};
use
string
::
ToString
;
#[test]
fn
test_permutations
()
{
{
let
v
:
[
i32
;
0
]
=
[];
let
mut
it
=
::
slice
::
permutations
(
&
v
);
let
(
min_size
,
max_opt
)
=
it
.size_hint
();
assert_eq!
(
min_size
,
1
);
assert_eq!
(
max_opt
.unwrap
(),
1
);
assert_eq!
(
it
.next
(),
Some
(::
slice
::
to_vec
(
&
v
)));
assert_eq!
(
it
.next
(),
None
);
}
{
let
v
=
[
"Hello"
.to_string
()];
let
mut
it
=
::
slice
::
permutations
(
&
v
);
let
(
min_size
,
max_opt
)
=
it
.size_hint
();
assert_eq!
(
min_size
,
1
);
assert_eq!
(
max_opt
.unwrap
(),
1
);
assert_eq!
(
it
.next
(),
Some
(::
slice
::
to_vec
(
&
v
)));
assert_eq!
(
it
.next
(),
None
);
}
{
let
v
=
[
1
,
2
,
3
];
let
mut
it
=
::
slice
::
permutations
(
&
v
);
let
(
min_size
,
max_opt
)
=
it
.size_hint
();
assert_eq!
(
min_size
,
3
*
2
);
assert_eq!
(
max_opt
.unwrap
(),
3
*
2
);
assert_eq!
(
it
.next
()
.unwrap
(),
[
1
,
2
,
3
]);
assert_eq!
(
it
.next
()
.unwrap
(),
[
1
,
3
,
2
]);
assert_eq!
(
it
.next
()
.unwrap
(),
[
3
,
1
,
2
]);
let
(
min_size
,
max_opt
)
=
it
.size_hint
();
assert_eq!
(
min_size
,
3
);
assert_eq!
(
max_opt
.unwrap
(),
3
);
assert_eq!
(
it
.next
()
.unwrap
(),
[
3
,
2
,
1
]);
assert_eq!
(
it
.next
()
.unwrap
(),
[
2
,
3
,
1
]);
assert_eq!
(
it
.next
()
.unwrap
(),
[
2
,
1
,
3
]);
assert_eq!
(
it
.next
(),
None
);
}
{
// check that we have N! permutations
let
v
=
[
'A'
,
'B'
,
'C'
,
'D'
,
'E'
,
'F'
];
let
mut
amt
=
0
;
let
mut
it
=
::
slice
::
permutations
(
&
v
);
let
(
min_size
,
max_opt
)
=
it
.size_hint
();
for
_
perm
in
it
.by_ref
()
{
amt
+=
1
;
}
assert_eq!
(
amt
,
it
.swaps.swaps_made
);
assert_eq!
(
amt
,
min_size
);
assert_eq!
(
amt
,
2
*
3
*
4
*
5
*
6
);
assert_eq!
(
amt
,
max_opt
.unwrap
());
}
}
}
src/libcollections/string.rs
浏览文件 @
b65ebc40
...
@@ -118,12 +118,14 @@ pub fn from_str(string: &str) -> String {
...
@@ -118,12 +118,14 @@ pub fn from_str(string: &str) -> String {
String
{
vec
:
<
[
_
]
>
::
to_vec
(
string
.as_bytes
())
}
String
{
vec
:
<
[
_
]
>
::
to_vec
(
string
.as_bytes
())
}
}
}
// HACK: `impl [T]` is not available in cfg(test), use `::slice::to_vec` instead of
// HACK(japaric): with cfg(test) the inherent `[T]::to_vec` method, which is required for this
// `<[T]>::to_vec`
// method definition, is not available. Since we don't require this method for testing
// purposes, I'll just stub it
// NB see the slice::hack module in slice.rs for more information
#[inline]
#[inline]
#[cfg(test)]
#[cfg(test)]
pub
fn
from_str
(
string
:
&
str
)
->
String
{
pub
fn
from_str
(
_
:
&
str
)
->
String
{
String
{
vec
:
::
slice
::
to_vec
(
string
.as_bytes
())
}
panic!
(
"not available with cfg(test)"
);
}
}
/// Returns the vector as a string buffer, if possible, taking care not to
/// Returns the vector as a string buffer, if possible, taking care not to
...
...
src/libcollections/vec.rs
浏览文件 @
b65ebc40
...
@@ -1290,10 +1290,15 @@ fn clone(&self) -> Vec<T> { ::slice::SliceExt::to_vec(&**self) }
...
@@ -1290,10 +1290,15 @@ fn clone(&self) -> Vec<T> { ::slice::SliceExt::to_vec(&**self) }
#[cfg(not(test))]
#[cfg(not(test))]
fn
clone
(
&
self
)
->
Vec
<
T
>
{
<
[
T
]
>
::
to_vec
(
&**
self
)
}
fn
clone
(
&
self
)
->
Vec
<
T
>
{
<
[
T
]
>
::
to_vec
(
&**
self
)
}
// HACK: `impl [T]` not available in cfg(test), use `::slice::to_vec` instead of `<[T]>::to_vec`
// HACK(japaric): with cfg(test) the inherent `[T]::to_vec` method, which is required for this
// method definition, is not available. Instead use the `slice::to_vec` function which is only
// available with cfg(test)
// NB see the slice::hack module in slice.rs for more information
#[cfg(not(stage0))]
#[cfg(not(stage0))]
#[cfg(test)]
#[cfg(test)]
fn
clone
(
&
self
)
->
Vec
<
T
>
{
::
slice
::
to_vec
(
&**
self
)
}
fn
clone
(
&
self
)
->
Vec
<
T
>
{
::
slice
::
to_vec
(
&**
self
)
}
fn
clone_from
(
&
mut
self
,
other
:
&
Vec
<
T
>
)
{
fn
clone_from
(
&
mut
self
,
other
:
&
Vec
<
T
>
)
{
// drop anything in self that will not be overwritten
// drop anything in self that will not be overwritten
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录