11% 生ポインタ
22<!-- % Raw Pointers -->
33
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
55there are two types that are extra-special. Much of Rust’s safety comes from
66compile-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 ] を使用するとそういった保証が得られません。
89
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
1011writing certain kinds of libraries, you’ll need to get around Rust’s safety
1112guarantees for some reason. In this case, you can use raw pointers to implement
1213your library, while exposing a safe interface for your users. For example, `*`
1314pointers are allowed to alias, allowing them to be used to write
1415shared-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
2530 `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
2834 reason about dangling pointers; and
2935- 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+
3140
32- # Basics
41+ <!-- # Basics -->
42+ # 基本
3343
34- Creating a raw pointer is perfectly safe:
44+ <!-- Creating a raw pointer is perfectly safe: -->
45+ 生ポインタを作成すること自体は絶対に安全です。
3546
3647``` rust
3748let x = 5 ;
@@ -41,7 +52,9 @@ let mut y = 10;
4152let raw_mut = & mut y as * mut i32 ;
4253```
4354
44- However, dereferencing one is not. This won’t work:
55+ <!-- However, dereferencing one is not. This won’t work: -->
56+ しかしながら参照外しは安全ではありません。以下は動作しないでしょう。
57+
4558
4659``` rust,ignore
4760let x = 5;
@@ -50,16 +63,18 @@ let raw = &x as *const i32;
5063println!("raw points at {}", *raw);
5164```
5265
53- It gives this error:
66+ <!-- It gives this error: -->
67+ このコードは以下のエラーが発生します。
5468
5569``` text
5670error: dereference of raw pointer requires unsafe function or block [E0133]
5771 println!("raw points at {}", *raw);
5872 ^~~~
5973```
6074
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 ` を付けなければなりません。
6378
6479``` rust
6580let x = 5 ;
@@ -70,7 +85,8 @@ let points_at = unsafe { *raw };
7085println! (" raw points at {}" , points_at );
7186```
7287
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 ] を参照してください。
7490
7591[ unsafe ] : unsafe.html
7692[ rawapi ] : ../std/primitive.pointer.html
@@ -80,33 +96,39 @@ For more operations on raw pointers, see [their API documentation][rawapi].
8096Raw pointers are useful for FFI: Rust’s ` *const T ` and ` *mut T ` are similar to
8197C’s ` const T* ` and ` T* ` , respectively. For more about this use, consult the
8298[ FFI chapter] [ ffi ] .
99+ 生ポインタはFFIを使う際に役立ちます。Rustの ` *const T ` と ` *mut T ` はそれぞれC言語の ` const T* ` と ` T* ` に似ているからです。これの使い方に関する詳細は、 [ FFIの章] [ ffi ] を参照してください。
83100
84101[ ffi ] : ffi.html
85102
86- # References and raw pointers
103+ <!-- # References and raw pointers -->
104+ # 参照と生ポインタ
87105
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
89107data have an identical representation. In fact, an `&T` reference will
90108implicitly coerce to an `*const T` raw pointer in safe code and similarly for
91109the `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 ` のように、明示的に行うこともできます。)
93112
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
95114`&T` is always valid, and so, at a minimum, the raw pointer `*const T` has to
96115point to a valid instance of type `T`. Furthermore, the resulting pointer must
97116satisfy the aliasing and mutability laws of references. The compiler assumes
98117these properties are true for any references, no matter how they are created,
99118and 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 ` の有効な実体を指さなければならないのです。その上、ポインタは参照のエイリアシングとミュータビリティの規則も満たす必要があります。コンパイラはあらゆる参照についてこれらのプロパティが真であると仮定しており、その生成方法によらず適用するため、生ポインタからのあらゆる変換もまた真であると断言します。プログラマがこのことを保証 _ しなければならない_ のです。
103121
122+ <!-- The recommended method for the conversion is: -->
123+ おすすめの変換の方法は以下のとおりです。
104124``` rust
105- // explicit cast
125+ # // explicit cast
126+ // 明示的キャスト
106127let i : u32 = 1 ;
107128let p_imm : * const u32 = & i as * const u32 ;
108129
109- // implicit coercion
130+ # // implicit coercion
131+ // 暗黙的キャスト
110132let mut m : u32 = 2 ;
111133let p_mut : * mut u32 = & mut m ;
112134
@@ -116,7 +138,8 @@ unsafe {
116138}
117139```
118140
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
120142is far more powerful than necessary, and the more restricted operation is
121143harder 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