Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
dca9ff9a
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,发现更多精彩内容 >>
提交
dca9ff9a
编写于
7月 03, 2013
作者:
E
Erick Tryzelaar
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
std: remove str::NullTerminatedStr
上级
fd293dfb
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
2 addition
and
76 deletion
+2
-76
src/libstd/prelude.rs
src/libstd/prelude.rs
+1
-1
src/libstd/str.rs
src/libstd/str.rs
+1
-54
src/test/compile-fail/static-slice-not-null-terminated.rs
src/test/compile-fail/static-slice-not-null-terminated.rs
+0
-21
未找到文件。
src/libstd/prelude.rs
浏览文件 @
dca9ff9a
...
...
@@ -63,7 +63,7 @@
pub
use
path
::
WindowsPath
;
pub
use
ptr
::
RawPtr
;
pub
use
ascii
::{
Ascii
,
AsciiCast
,
OwnedAsciiCast
,
AsciiStr
,
ToBytesConsume
};
pub
use
str
::{
Str
,
StrVector
,
StrSlice
,
OwnedStr
,
NullTerminatedStr
};
pub
use
str
::{
Str
,
StrVector
,
StrSlice
,
OwnedStr
};
pub
use
from_str
::{
FromStr
};
pub
use
to_bytes
::
IterBytes
;
pub
use
to_str
::{
ToStr
,
ToStrConsume
};
...
...
src/libstd/str.rs
浏览文件 @
dca9ff9a
...
...
@@ -1981,35 +1981,6 @@ fn as_c_str<T>(&self, f: &fn(*libc::c_char) -> T) -> T {
}
}
#[allow(missing_doc)]
pub
trait
NullTerminatedStr
{
fn
as_bytes_with_null
<
'a
>
(
&
'a
self
)
->
&
'a
[
u8
];
}
impl
NullTerminatedStr
for
~
str
{
/// Work with the byte buffer of a string as a byte slice.
///
/// The byte slice does include the null terminator.
#[inline]
fn
as_bytes_with_null
<
'a
>
(
&
'a
self
)
->
&
'a
[
u8
]
{
let
ptr
:
&
'a
~
[
u8
]
=
unsafe
{
cast
::
transmute
(
self
)
};
let
slice
:
&
'a
[
u8
]
=
*
ptr
;
slice
}
}
impl
NullTerminatedStr
for
@
str
{
/// Work with the byte buffer of a string as a byte slice.
///
/// The byte slice does include the null terminator.
#[inline]
fn
as_bytes_with_null
<
'a
>
(
&
'a
self
)
->
&
'a
[
u8
]
{
let
ptr
:
&
'a
@
[
u8
]
=
unsafe
{
cast
::
transmute
(
self
)
};
let
slice
:
&
'a
[
u8
]
=
*
ptr
;
slice
}
}
#[allow(missing_doc)]
pub
trait
OwnedStr
{
fn
push_str_no_overallocate
(
&
mut
self
,
rhs
:
&
str
);
...
...
@@ -2979,30 +2950,6 @@ fn test_as_bytes() {
assert_eq!
(
"ศไทย中华Việt Nam"
.as_bytes
(),
v
);
}
#[test]
fn
test_as_bytes_with_null
()
{
// has null
let
v
=
[
224
,
184
,
168
,
224
,
185
,
132
,
224
,
184
,
151
,
224
,
184
,
162
,
228
,
184
,
173
,
229
,
141
,
142
,
86
,
105
,
225
,
187
,
135
,
116
,
32
,
78
,
97
,
109
,
0
];
let
s1
=
@
""
;
let
s2
=
@
"abc"
;
let
s3
=
@
"ศไทย中华Việt Nam"
;
assert_eq!
(
s1
.as_bytes_with_null
(),
&
[
0
]);
assert_eq!
(
s2
.as_bytes_with_null
(),
&
[
'a'
as
u8
,
'b'
as
u8
,
'c'
as
u8
,
0
]);
assert_eq!
(
s3
.as_bytes_with_null
(),
v
);
let
s1
=
~
""
;
let
s2
=
~
"abc"
;
let
s3
=
~
"ศไทย中华Việt Nam"
;
assert_eq!
(
s1
.as_bytes_with_null
(),
&
[
0
]);
assert_eq!
(
s2
.as_bytes_with_null
(),
&
[
'a'
as
u8
,
'b'
as
u8
,
'c'
as
u8
,
0
]);
assert_eq!
(
s3
.as_bytes_with_null
(),
v
);
}
#[test]
fn
test_to_bytes_with_null
()
{
let
s
=
~
"ศไทย中华Việt Nam"
;
...
...
@@ -3024,7 +2971,7 @@ fn test_as_bytes_fail() {
// Don't double free. (I'm not sure if this exercises the
// original problem code path anymore.)
let
s
=
~
""
;
let
_
bytes
=
s
.as_bytes
_with_null
();
let
_
bytes
=
s
.as_bytes
();
fail
!
();
}
...
...
src/test/compile-fail/static-slice-not-null-terminated.rs
已删除
100644 → 0
浏览文件 @
fd293dfb
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
fn
main
()
{
let
_
=
(
~
"foo"
)
.as_bytes_with_null
();
let
_
=
(
@
"foo"
)
.as_bytes_with_null
();
// a plain static slice is null terminated, but such a slice can
// be sliced shorter (i.e. become non-null terminated) and still
// have the static lifetime
let
foo
:
&
'static
str
=
"foo"
;
let
_
=
foo
.as_bytes_with_null
();
//~^ ERROR does not implement any method in scope named `as_bytes_with_null`
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录