Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
710f13f0
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,发现更多精彩内容 >>
提交
710f13f0
编写于
3月 10, 2014
作者:
F
Flavio Percoco
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add Unsafe<T> type
上级
b85d5f1f
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
88 addition
and
1 deletion
+88
-1
src/librustc/middle/lang_items.rs
src/librustc/middle/lang_items.rs
+2
-0
src/librustc/middle/ty.rs
src/librustc/middle/ty.rs
+4
-1
src/libstd/lib.rs
src/libstd/lib.rs
+3
-0
src/libstd/ty.rs
src/libstd/ty.rs
+79
-0
未找到文件。
src/librustc/middle/lang_items.rs
浏览文件 @
710f13f0
...
...
@@ -233,6 +233,8 @@ pub fn collect_language_items(krate: &ast::Crate,
ShrTraitLangItem
,
"shr"
,
shr_trait
;
IndexTraitLangItem
,
"index"
,
index_trait
;
UnsafeTypeLangItem
,
"unsafe"
,
unsafe_type
;
DerefTraitLangItem
,
"deref"
,
deref_trait
;
DerefMutTraitLangItem
,
"deref_mut"
,
deref_mut_trait
;
...
...
src/librustc/middle/ty.rs
浏览文件 @
710f13f0
...
...
@@ -1878,7 +1878,8 @@ mod TC {
// Things that are interior to the value (first nibble):
InteriorUnsized
=
0b0000_0000__0000_0000__0001
,
// InteriorAll = 0b0000_0000__0000_0000__1111,
InteriorUnsafe
=
0b0000_0000__0000_0000__0010
,
// InteriorAll = 0b00000000__00000000__1111,
// Things that are owned by the value (second and third nibbles):
OwnsOwned
=
0b0000_0000__0000_0001__0000
,
...
...
@@ -2297,6 +2298,8 @@ fn apply_lang_items(cx: &ctxt,
tc
|
TC
::
OwnsAffine
}
else
if
Some
(
did
)
==
cx
.lang_items
.no_share_bound
()
{
tc
|
TC
::
ReachesNoShare
}
else
if
Some
(
did
)
==
cx
.lang_items
.unsafe_type
()
{
tc
|
TC
::
InteriorUnsafe
}
else
{
tc
}
...
...
src/libstd/lib.rs
浏览文件 @
710f13f0
...
...
@@ -83,6 +83,7 @@
#[cfg(test)]
pub
use
kinds
=
realstd
::
kinds
;
#[cfg(test)]
pub
use
ops
=
realstd
::
ops
;
#[cfg(test)]
pub
use
cmp
=
realstd
::
cmp
;
#[cfg(test)]
pub
use
ty
=
realstd
::
ty
;
pub
mod
macros
;
...
...
@@ -138,6 +139,7 @@
#[cfg(not(test))]
pub
mod
kinds
;
#[cfg(not(test))]
pub
mod
ops
;
#[cfg(not(test))]
pub
mod
cmp
;
#[cfg(not(test))]
pub
mod
ty
;
/* Common traits */
...
...
@@ -226,5 +228,6 @@ mod std {
pub
use
rt
;
pub
use
str
;
pub
use
to_str
;
pub
use
ty
;
pub
use
unstable
;
}
src/libstd/ty.rs
0 → 100644
浏览文件 @
710f13f0
// Copyright 2012-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.
//! Types dealing with unsafe actions.
use
cast
;
use
kinds
::
marker
;
/// Unsafe type that wraps a type T and indicates unsafe interior operations on the
/// wrapped type. Types with an `Unsafe<T>` field are considered to have an *unsafe
/// interior*. The Unsafe type is the only legal way to obtain aliasable data that is
/// considered mutable. In general, transmuting an &T type into an &mut T is considered
/// undefined behavior.
///
/// Although it is possible to put an Unsafe<T> into static item, it is not permitted to
/// take the address of the static item if the item is not declared as mutable. This rule
/// exists because immutable static items are stored in read-only memory, and thus any
/// attempt to mutate their interior can cause segfaults. Immutable static items containing
/// Unsafe<T> instances are still useful as read-only initializers, however, so we do not
/// forbid them altogether.
///
/// Types like `Cell` and `RefCell` use this type to wrap their internal data.
///
/// Unsafe doesn't opt-out from any kind, instead, types with an `Unsafe` interior
/// are expected to opt-out from kinds themselves.
///
/// # Example:
///
/// ```rust
/// use std::ty::Unsafe;
/// use std::kinds::marker;
///
/// struct NotThreadSafe<T> {
/// value: Unsafe<T>,
/// marker1: marker::NoShare
/// }
/// ```
///
/// **NOTE:** Unsafe<T> fields are public to allow static initializers. It is not recommended
/// to access its fields directly, `get` should be used instead.
#[cfg(not(stage0))]
#[lang=
"unsafe"
]
pub
struct
Unsafe
<
T
>
{
/// Wrapped value
value
:
T
,
/// Invariance marker
marker1
:
marker
::
InvariantType
<
T
>
}
/// Unsafe type for stage0
#[cfg(stage0)]
pub
struct
Unsafe
<
T
>
{
/// Wrapped value
value
:
T
,
/// Invariance marker
marker1
:
marker
::
InvariantType
<
T
>
}
impl
<
T
>
Unsafe
<
T
>
{
/// Static constructor
pub
fn
new
(
value
:
T
)
->
Unsafe
<
T
>
{
Unsafe
{
value
:
value
,
marker1
:
marker
::
InvariantType
}
}
/// Gets a mutable pointer to the wrapped value
#[inline]
pub
unsafe
fn
get
(
&
self
)
->
*
mut
T
{
cast
::
transmute
(
&
self
.value
)
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录