1
- % Raw Pointers
1
+ % 生ポインタ
2
+ <!-- % Raw Pointers -->
2
3
3
- Rust has a number of different smart pointer types in its standard library, but
4
+ <!-- Rust has a number of different smart pointer types in its standard library, but
4
5
there are two types that are extra-special. Much of Rust’s safety comes from
5
6
compile-time checks, but raw pointers don’t have such guarantees, and are
6
- [ unsafe] [ unsafe ] to use.
7
+ [unsafe][unsafe] to use. -->
8
+ Rustは標準ライブラリに異なるスマートポインタの型を幾つか用意していますが、更に特殊な型が2つあります。Rustの安全性の多くはコンパイル時のチェックに依るものですが、生ポインタを用いるとそういった保証が得られないため [ unsafe] [ unsafe ] です。
7
9
8
- ` *const T ` and ` *mut T ` are called ‘raw pointers’ in Rust. Sometimes, when
10
+ <!-- `*const T` and `*mut T` are called ‘raw pointers’ in Rust. Sometimes, when
9
11
writing certain kinds of libraries, you’ll need to get around Rust’s safety
10
12
guarantees for some reason. In this case, you can use raw pointers to implement
11
13
your library, while exposing a safe interface for your users. For example, `*`
12
14
pointers are allowed to alias, allowing them to be used to write
13
15
shared-ownership types, and even thread-safe shared memory types (the `Rc<T>`
14
- and ` Arc<T> ` types are both implemented entirely in Rust).
15
-
16
- Here are some things to remember about raw pointers that are different than
17
- other pointer types. They:
18
-
19
- - are not guaranteed to point to valid memory and are not even
20
- guaranteed to be non-null (unlike both ` Box ` and ` & ` );
21
- - do not have any automatic clean-up, unlike ` Box ` , and so require
22
- manual resource management;
23
- - are plain-old-data, that is, they don't move ownership, again unlike
16
+ and `Arc<T>` types are both implemented entirely in Rust). -->
17
+ ` *const T ` と ` *mut T ` はRustにおいて「生ポインタ」と呼ばれます。時々、ある種ののライブラリを書く際に、あなたは何らかの理由でRustが行う安全性の保証を避けなければならないこともあります。このようなケースでは、ユーザに安全なインターフェースを提供しつつ、ライブラリの実装に生ポインタを使用できます。例えば、 ` * ` ポインタはエイリアスとして振る舞うこともできるので、所有権を共有する型を書くのに用いたり、スレッドセーフな共有メモリ型でさえも実装できます。( ` Rc<T> ` と ` Arc<T> ` 型は完全にRustのみで実装されています)
18
+
19
+ <!-- Here are some things to remember about raw pointers that are different than
20
+ other pointer types. They: -->
21
+ 以下は覚えておくべき生ポインタとその他のポインタ型との違いです。
22
+
23
+ <!-- - are not guaranteed to point to valid memory and are not even
24
+ guaranteed to be non-null (unlike both `Box` and `&`); -->
25
+ <!-- - do not have any automatic clean-up, unlike `Box`, and so require
26
+ manual resource management; -->
27
+ <!-- - are plain-old-data, that is, they don't move ownership, again unlike
24
28
`Box`, hence the Rust compiler cannot protect against bugs like
25
- use-after-free;
26
- - lack any form of lifetimes, unlike ` & ` , and so the compiler cannot
29
+ use-after-free; -->
30
+ <!-- - lack any form of lifetimes, unlike `&`, and so the compiler cannot
27
31
reason about dangling pointers; and
28
32
- have no guarantees about aliasing or mutability other than mutation
29
- not being allowed directly through a ` *const T ` .
33
+ not being allowed directly through a `*const T`. -->
34
+ - 有効なメモリを指していることが保証されないどころか、nullでないことも保証されない( ` Box ` と ` & ` では保証される)
35
+ - ` Box ` とは異なり、自動的な後処理が一切行われないため、手動のリソース管理が必要
36
+ - plain-old-dataであるため、Rustコンパイラはuse-after-freeのようなバグから保護できない
37
+ - ` & ` と異なり、ライフタイムの機能が無効化されるため、コンパイラはダングリングポインタを推論できない
38
+ - また、 ` *const T ` を直接介した変更は拒むが、それ以外のエイリアシングやミュータビリティに関する保証はない
30
39
31
- # Basics
32
40
33
- Creating a raw pointer is perfectly safe:
41
+ <!-- # Basics -->
42
+ # 基本
43
+
44
+ <!-- Creating a raw pointer is perfectly safe: -->
45
+ 生ポインタを作成すること自体は絶対に安全です。
34
46
35
47
``` rust
36
48
let x = 5 ;
@@ -40,7 +52,9 @@ let mut y = 10;
40
52
let raw_mut = & mut y as * mut i32 ;
41
53
```
42
54
43
- However, dereferencing one is not. This won’t work:
55
+ <!-- However, dereferencing one is not. This won’t work: -->
56
+ しかしながら参照外しは安全ではありません。以下は動作しないでしょう。
57
+
44
58
45
59
``` rust,ignore
46
60
let x = 5;
@@ -49,16 +63,18 @@ let raw = &x as *const i32;
49
63
println!("raw points at {}", *raw);
50
64
```
51
65
52
- It gives this error:
66
+ <!-- It gives this error: -->
67
+ このコードは以下のエラーが発生します。
53
68
54
69
``` text
55
70
error: dereference of raw pointer requires unsafe function or block [E0133]
56
71
println!("raw points at {}", *raw);
57
72
^~~~
58
73
```
59
74
60
- When you dereference a raw pointer, you’re taking responsibility that it’s not
61
- pointing somewhere that would be incorrect. As such, you need ` unsafe ` :
75
+ <!-- When you dereference a raw pointer, you’re taking responsibility that it’s not
76
+ pointing somewhere that would be incorrect. As such, you need `unsafe`: -->
77
+ 生ポインタを参照外しする時、ポインタが間違った場所を指していないことに対して責任を負うことになります。そういう時は、 ` unsafe ` を付けなければなりません。
62
78
63
79
``` rust
64
80
let x = 5 ;
@@ -69,43 +85,51 @@ let points_at = unsafe { *raw };
69
85
println! (" raw points at {}" , points_at );
70
86
```
71
87
72
- For more operations on raw pointers, see [ their API documentation] [ rawapi ] .
88
+ <!-- For more operations on raw pointers, see [their API documentation][rawapi]. -->
89
+ 生ポインタの操作に関する詳細は、 [ APIドキュメント] [ rawapi ] を参照してください。
73
90
74
91
[ unsafe ] : unsafe.html
75
92
[ rawapi ] : ../std/primitive.pointer.html
76
93
77
94
# FFI
78
95
79
- Raw pointers are useful for FFI: Rust’s ` *const T ` and ` *mut T ` are similar to
96
+ <!-- Raw pointers are useful for FFI: Rust’s `*const T` and `*mut T` are similar to
80
97
C’s `const T*` and `T*`, respectively. For more about this use, consult the
81
- [ FFI chapter] [ ffi ] .
98
+ [FFI chapter][ffi]. -->
99
+ 生ポインタはFFIを使う際に役立ちます。Rustの ` *const T ` と ` *mut T ` はそれぞれC言語の ` const T* ` と ` T* ` に似ているからです。これの使い方に関する詳細は、 [ FFIの章] [ ffi ] を参照してください。
82
100
83
101
[ ffi ] : ffi.html
84
102
85
- # References and raw pointers
103
+ <!-- # References and raw pointers -->
104
+ # 参照と生ポインタ
86
105
87
- At runtime, a raw pointer ` * ` and a reference pointing to the same piece of
106
+ <!-- At runtime, a raw pointer `*` and a reference pointing to the same piece of
88
107
data have an identical representation. In fact, an `&T` reference will
89
108
implicitly coerce to an `*const T` raw pointer in safe code and similarly for
90
109
the `mut` variants (both coercions can be performed explicitly with,
91
- respectively, ` value as *const T ` and ` value as *mut T ` ).
110
+ respectively, `value as *const T` and `value as *mut T`). -->
111
+ 実行時において、同じデータを指す生ポインタ ` * ` と参照は内部的に同一です。事実、 ` unsafe ` 外の安全なコードにおいて ` &T ` 参照は ` *const T ` 生ポインタへ暗黙的に型強制されますし、 ` mut ` の場合でも同様です。(これら型強制は、それぞれ ` value as *const T ` と ` value as *mut T ` のように、明示的に行うこともできます。)
92
112
93
- Going the opposite direction, from ` *const ` to a reference ` & ` , is not safe. A
113
+ <!-- Going the opposite direction, from `*const` to a reference `&`, is not safe. A
94
114
`&T` is always valid, and so, at a minimum, the raw pointer `*const T` has to
95
115
point to a valid instance of type `T`. Furthermore, the resulting pointer must
96
116
satisfy the aliasing and mutability laws of references. The compiler assumes
97
117
these properties are true for any references, no matter how they are created,
98
118
and so any conversion from raw pointers is asserting that they hold. The
99
- programmer * must* guarantee this.
119
+ programmer *must* guarantee this. -->
120
+ 逆に、 ` *const ` から 参照 ` & ` へ遡るのは安全ではありません。 ` &T ` は常に有効であるため、最低でも ` *const T ` は型 ` T ` の有効な実体を指さなければならないのです。その上、ポインタは参照のエイリアシングとミュータビリティの規則も満たす必要があります。コンパイラはあらゆる参照についてこれらの性質が真であると仮定しており、その生成方法に依らず適用するため、生ポインタからのいかなる変換も、参照先の値が上記の性質を満たすと表明していることになります。プログラマがこのことを保証 * しなければならない* のです。
100
121
101
- The recommended method for the conversion is:
122
+ <!-- The recommended method for the conversion is: -->
123
+ おすすめの変換の方法は以下のとおりです。
102
124
103
125
``` rust
104
- // explicit cast
126
+ # // explicit cast
127
+ // 明示的キャスト
105
128
let i : u32 = 1 ;
106
129
let p_imm : * const u32 = & i as * const u32 ;
107
130
108
- // implicit coercion
131
+ # // implicit coercion
132
+ // 暗黙的キャスト
109
133
let mut m : u32 = 2 ;
110
134
let p_mut : * mut u32 = & mut m ;
111
135
@@ -115,7 +139,8 @@ unsafe {
115
139
}
116
140
```
117
141
118
- The ` &*x ` dereferencing style is preferred to using a ` transmute ` . The latter
142
+ <!-- The `&*x` dereferencing style is preferred to using a `transmute`. The latter
119
143
is far more powerful than necessary, and the more restricted operation is
120
144
harder to use incorrectly; for example, it requires that `x` is a pointer
121
- (unlike ` transmute ` ).
145
+ (unlike `transmute`). -->
146
+ ` &*x ` 参照外し方式は ` transmute ` を用いるよりも好ましいです。後者は必要以上に強力ですから、より用途が限定されている操作の方が間違って使いにくいでしょう。例えば、前者の方法は ` x ` がポインタである必要があります。( ` transmute ` とは異なります)
0 commit comments