1- % Raw Pointers
1+ % 生ポインタ
2+ <!-- % Raw Pointers -->
23
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
45there are two types that are extra-special. Much of Rust’s safety comes from
56compile-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 ] です。
79
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
911writing certain kinds of libraries, you’ll need to get around Rust’s safety
1012guarantees for some reason. In this case, you can use raw pointers to implement
1113your library, while exposing a safe interface for your users. For example, `*`
1214pointers are allowed to alias, allowing them to be used to write
1315shared-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
2428 `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
2731 reason about dangling pointers; and
2832- 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 ` を直接介した変更は拒むが、それ以外のエイリアシングやミュータビリティに関する保証はない
3039
31- # Basics
3240
33- Creating a raw pointer is perfectly safe:
41+ <!-- # Basics -->
42+ # 基本
43+
44+ <!-- Creating a raw pointer is perfectly safe: -->
45+ 生ポインタを作成すること自体は絶対に安全です。
3446
3547``` rust
3648let x = 5 ;
@@ -40,7 +52,9 @@ let mut y = 10;
4052let raw_mut = & mut y as * mut i32 ;
4153```
4254
43- However, dereferencing one is not. This won’t work:
55+ <!-- However, dereferencing one is not. This won’t work: -->
56+ しかしながら参照外しは安全ではありません。以下は動作しないでしょう。
57+
4458
4559``` rust,ignore
4660let x = 5;
@@ -49,16 +63,18 @@ let raw = &x as *const i32;
4963println!("raw points at {}", *raw);
5064```
5165
52- It gives this error:
66+ <!-- It gives this error: -->
67+ このコードは以下のエラーが発生します。
5368
5469``` text
5570error: dereference of raw pointer requires unsafe function or block [E0133]
5671 println!("raw points at {}", *raw);
5772 ^~~~
5873```
5974
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 ` を付けなければなりません。
6278
6379``` rust
6480let x = 5 ;
@@ -69,43 +85,51 @@ let points_at = unsafe { *raw };
6985println! (" raw points at {}" , points_at );
7086```
7187
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 ] を参照してください。
7390
7491[ unsafe ] : unsafe.html
7592[ rawapi ] : ../std/primitive.pointer.html
7693
7794# FFI
7895
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
8097C’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 ] を参照してください。
82100
83101[ ffi ] : ffi.html
84102
85- # References and raw pointers
103+ <!-- # References and raw pointers -->
104+ # 参照と生ポインタ
86105
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
88107data have an identical representation. In fact, an `&T` reference will
89108implicitly coerce to an `*const T` raw pointer in safe code and similarly for
90109the `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 ` のように、明示的に行うこともできます。)
92112
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
94114`&T` is always valid, and so, at a minimum, the raw pointer `*const T` has to
95115point to a valid instance of type `T`. Furthermore, the resulting pointer must
96116satisfy the aliasing and mutability laws of references. The compiler assumes
97117these properties are true for any references, no matter how they are created,
98118and 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 ` の有効な実体を指さなければならないのです。その上、ポインタは参照のエイリアシングとミュータビリティの規則も満たす必要があります。コンパイラはあらゆる参照についてこれらの性質が真であると仮定しており、その生成方法に依らず適用するため、生ポインタからのいかなる変換も、参照先の値が上記の性質を満たすと表明していることになります。プログラマがこのことを保証 * しなければならない* のです。
100121
101- The recommended method for the conversion is:
122+ <!-- The recommended method for the conversion is: -->
123+ おすすめの変換の方法は以下のとおりです。
102124
103125``` rust
104- // explicit cast
126+ # // explicit cast
127+ // 明示的キャスト
105128let i : u32 = 1 ;
106129let p_imm : * const u32 = & i as * const u32 ;
107130
108- // implicit coercion
131+ # // implicit coercion
132+ // 暗黙的キャスト
109133let mut m : u32 = 2 ;
110134let p_mut : * mut u32 = & mut m ;
111135
@@ -115,7 +139,8 @@ unsafe {
115139}
116140```
117141
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
119143is far more powerful than necessary, and the more restricted operation is
120144harder 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