1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
//! 未初期化の値。
//!
//! ## Preliminaries
//!
//! 実行時の所望のタイミングで初期化されるような値を作りたいことがある。
//! 次のような Safe Rust のコードはコンパイルエラーとなる。
//!
//! ```compile_fail
//! let a = 123;
//! let mut b;
//! if a % 5 <= 3 { b = "foo"; }
//! if a % 7 <= 4 { b = "bar"; }
//! let _invalid = b; // CE
//! ```
//!
//! [`MaybeUninit`][`std::mem::MaybeUninit`] を使うことで、下記のように書くことができる。
//!
//! ```
//! use std::mem::MaybeUninit;
//!
//! let a = 123;
//! let mut b_uninit = MaybeUninit::<&str>::uninit();
//! let ptr = b_uninit.as_mut_ptr();
//! if a % 5 <= 3 { unsafe { ptr.write("foo") } };
//! if a % 7 <= 4 { unsafe { ptr.write("bar") } };
//! let b = unsafe { b_uninit.assume_init() };
//! assert_eq!(b, "bar");
//! ```
//!
//! `.assume_init()` を呼んだ時点で実際には未初期化だった場合、当然未定義動作となる。
//!
//! ```ignore
//! use std::mem::MaybeUninit;
//!
//! let a = MaybeUninit::<i32>::uninit();
//! let _invalid = unsafe { a.assume_init() };
//! ```
//!
//! ```text
//!  --> src/lib.rs:6:25
//!   |
//! 6 | let _invalid = unsafe { a.assume_init() };
//!   |                         ^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
//! ```
//!
//! `MaybeUninit<T>` をメンバに持つ型 `U` に対する `MaybeUninit<U>` や、`[MaybeUninit<T>; N]`
//! や、`Box<MaybeUninit<T>>` などの例を見てみる。
//!
//! ```
//! use std::{mem::MaybeUninit, ptr};
//!
//! struct Foo {
//!     vector: Vec<i32>,
//!     string: MaybeUninit<String>,
//! }
//!
//! let mut foo = unsafe {
//!     let mut foo_uninit = MaybeUninit::<Foo>::uninit();
//!     let ptr = foo_uninit.as_mut_ptr();
//!     // (*ptr).vector = vec![]; // UB
//!     ptr::addr_of_mut!((*ptr).vector).write(vec![0]);
//!     foo_uninit.assume_init()
//! };
//! assert_eq!(foo.vector, [0]);
//!
//! let s_ptr = foo.string.as_mut_ptr();
//! unsafe {
//!     s_ptr.write("init".to_owned());
//!     assert_eq!(foo.string.assume_init_ref(), "init");
//!     drop(foo.string.assume_init_drop());
//! }
//! ```
//!
//! ```
//! use std::mem::MaybeUninit;
//!
//! // let mut foo_uninit = [MaybeUninit::<String>::uninit(); 3]; // !Copy
//! let mut foo_uninit = unsafe {
//!     MaybeUninit::<[MaybeUninit<String>; 3]>::uninit().assume_init()
//! };
//! unsafe {
//!     foo_uninit[0].write("uno".to_owned());
//!     foo_uninit[1].write("dos".to_owned());
//!
//!     let foo = {
//!         &*(&foo_uninit[..2] as *const [MaybeUninit<_>] as *const [String])
//!     };
//!     assert_eq!(foo[0], "uno");
//!     assert_eq!(foo[1], "dos");
//!
//!     foo_uninit[0].assume_init_drop();
//!     foo_uninit[1].assume_init_drop();
//! }
//! ```
//!
//! ```
//! use std::{mem::MaybeUninit, ptr};
//!
//! struct Foo {
//!     vector: Vec<i32>,
//!     string: String,
//! }
//!
//! let mut foo_uninit = Box::new(MaybeUninit::<Foo>::uninit());
//! let foo: Box<Foo> = unsafe {
//!     let ptr = foo_uninit.as_mut_ptr();
//!     ptr::addr_of_mut!((*ptr).vector).write(vec![0]);
//!     ptr::addr_of_mut!((*ptr).string).write("init".to_owned());
//!     Box::from_raw(Box::leak(foo_uninit).as_mut_ptr())
//! };
//! assert_eq!(foo.vector, [0]);
//! assert_eq!(foo.string, "init");
//! ```
//!
//! レイアウトに関して、`T` と `MaybeUninit<T>` は同様の size および alignment を持つことは保証されている ([ref](https://doc.rust-lang.org/nightly/core/mem/union.MaybeUninit.html#layout-1))。
//! `T` を含む型と `MaybeUninit<T>` を含む型が同様のレイアウトを持つとは限らないが、一旦は忘れておく。
//!
//! 未初期化な領域を含む状態で参照 `&mut T` を取得すると未定義動作となる。`ptr::addr_of_mut!`
//! などを用いて回避する必要がある。
//!
//! ```
//! use std::{mem::MaybeUninit, ptr};
//!
//! struct Foo {
//!     vector: Vec<i32>,
//!     string: String,
//! }
//!
//! let mut foo = unsafe {
//!     let mut foo_uninit = MaybeUninit::<Foo>::uninit();
//!     let ptr = foo_uninit.as_mut_ptr();
//!     // (*ptr).vector = vec![0]; // UB
//!     ptr::addr_of_mut!((*ptr).vector).write(vec![0]);
//!     // (*ptr).string = "init".to_owned(); // UB
//!     ptr::addr_of_mut!((*ptr).string).write("init".to_owned());
//!     (*ptr).string = "re-init".to_owned(); // OK
//!     
//!     foo_uninit.assume_init()
//! };
//! ```
//!
//! 初期化済みの領域に `.write()` するとメモリリークが起きる。
//! あるいは、未初期化の領域を `=` で書き込もうとすると、`*mut T` の dereference なり
//! `&mut T` の取得なりをする必要があるため未定義動作となる。
//!
//! ```
//! use std::mem::MaybeUninit;
//!
//! struct Foo(MaybeUninit<String>);
//!
//! let mut foo = Foo(MaybeUninit::uninit());
//! // unsafe { *foo.0.as_mut_ptr() = "init".to_owned() } // UB
//! // unsafe { *foo.0.assume_init_mut() = "init".to_owned() } // UB
//! foo.0.write("init once".to_owned());
//!
//! unsafe { *foo.0.as_mut_ptr() = "init twice".to_owned() } // OK
//! unsafe { *foo.0.assume_init_mut() = "init thrice".to_owned() } // OK
//! // foo.0.write("init".to_owned()); // memory leak
//!
//! unsafe { foo.0.assume_init_drop() }
//! ```
//!
//! `*mut ()` であれば、未初期化 (dangling) の領域を dereference しても大丈夫。
//!
//! ```
//! use std::mem::MaybeUninit;
//!
//! let unit_uninit = MaybeUninit::<()>::uninit();
//! unsafe {
//!     assert_eq!(unit_uninit.assume_init(), ());
//! }
//! ```

#[cfg(test)]
mod playground {
    use std::{mem::MaybeUninit, ptr};

    #[test]
    fn nested() {
        struct Foo {
            i: i32,
            s: String,
            a: [MaybeUninit<i32>; 3],
        }

        let mut foo = unsafe {
            let mut foo_uninit = MaybeUninit::<Foo>::uninit();
            let ptr = foo_uninit.as_mut_ptr();
            ptr::addr_of_mut!((*ptr).i).write(1);
            // (*foo.as_mut_ptr()).s = "s".to_owned(); // UB
            ptr::addr_of_mut!((*ptr).s).write("s".to_owned());
            foo_uninit.assume_init()
        };

        foo.a[0].write(1);
        assert_eq!(unsafe { foo.a[0].assume_init() }, 1);
    }

    #[test]
    fn array() {
        struct Foo<T, const N: usize> {
            a: [MaybeUninit<T>; N],
        }

        let mut foo =
            unsafe { MaybeUninit::<Foo<String, 3>>::uninit().assume_init() };

        foo.a[0].write("one".to_owned());
        // unsafe { *(foo.a[1].as_mut_ptr()) = "_".to_owned() }; // bad
        // unsafe { *foo.a[1].assume_init_mut() = "_".to_owned() }; // also bad
        foo.a[1].write("two".to_owned());

        assert_eq!(unsafe { foo.a[0].assume_init_ref() }, "one");
        assert_eq!(unsafe { foo.a[1].assume_init_ref() }, "two");

        // foo.a[1].write("_".to_owned()); // bad
        unsafe { *(foo.a[1].as_mut_ptr()) = "zwei".to_owned() }; // ok
        unsafe { *foo.a[1].assume_init_mut() = "deux".to_owned() }; // also ok

        let a = unsafe {
            &mut *(&mut foo.a[..2] as *mut [MaybeUninit<_>] as *mut [String])
        };
        a[0] = "uno".to_owned();

        let a = unsafe {
            &*(&foo.a[..2] as *const [MaybeUninit<_>] as *const [String])
        };
        assert_eq!(a, ["uno", "deux"]);

        unsafe { foo.a[0].assume_init_drop() };
        unsafe { foo.a[1].assume_init_drop() };
    }

    #[test]
    fn boxed() {
        struct Foo {
            a: String,
        }

        let mut foo_uninit = Box::new(MaybeUninit::<Foo>::uninit());
        let foo = unsafe {
            let ptr = foo_uninit.as_mut_ptr();
            ptr::addr_of_mut!((*ptr).a).write("a".to_owned());
            Box::from_raw(Box::leak(foo_uninit).as_mut_ptr())
        };

        assert_eq!(foo.a, "a");
    }
}