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