File tree Expand file tree Collapse file tree 5 files changed +685
-0
lines changed Expand file tree Collapse file tree 5 files changed +685
-0
lines changed Original file line number Diff line number Diff line change
1
+ /target
2
+ Cargo.lock
Original file line number Diff line number Diff line change
1
+ [package ]
2
+ name = " munge"
3
+ version = " 0.1.0"
4
+ authors = [
" David Koloski <[email protected] >" ]
5
+ edition = " 2021"
6
+ description = " Macro for easily initializing `MaybeUninit`s"
7
+ license = " MIT"
8
+ documentation = " https://docs.rs/munge"
9
+ repository = " https://github.com/djkoloski/munge"
10
+ keywords = [" munge" , " macro" , " initialize" ]
11
+ categories = [" no-std" , " rust-patterns" ]
12
+ readme = " crates-io.md"
13
+
14
+ # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
15
+
16
+ [dependencies ]
Original file line number Diff line number Diff line change
1
+ # ` munge `
2
+
3
+ ` munge ` makes it easy to initialize ` MaybeUninit ` s.
4
+
5
+ Just use the ` munge! ` macro to destructure ` MaybeUninit ` s the same way you'd destructure a value.
6
+ Initialize all the fields, then call ` assume_init ` to unwrap it.
7
+
8
+ ` munge ` has no features and is always ` #![no_std] ` .
9
+
10
+ ## Example
11
+
12
+ ``` rust
13
+ use {
14
+ :: core :: mem :: MaybeUninit ,
15
+ :: munge :: munge,
16
+ };
17
+
18
+ pub struct Example {
19
+ a : u32 ,
20
+ b : (char , f32 ),
21
+ }
22
+
23
+ let mut mu = MaybeUninit :: <Example >:: uninit ();
24
+
25
+ munge! (let Example { a , b : (c , mut f ) } = mu );
26
+ assert_eq! (a . write (10 ), & 10 );
27
+ assert_eq! (c . write ('x' ), & 'x' );
28
+ assert_eq! (f . write (3.14 ), & 3.14 );
29
+ f = & mut MaybeUninit :: uninit ();
30
+
31
+ // SAFETY: `mu` is completely initialized.
32
+ let init = unsafe { mu . assume_init () };
33
+ assert_eq! (init . a, 10 );
34
+ assert_eq! (init . b. 0 , 'x' );
35
+ assert_eq! (init . b. 1 , 3.14 );
36
+ ```
Original file line number Diff line number Diff line change
1
+ ` munge ` makes it easy to initialize ` MaybeUninit ` s.
2
+
3
+ Just use the ` munge! ` macro to destructure ` MaybeUninit ` s the same way you'd destructure a value.
4
+ Initialize all the fields, then call ` assume_init ` to unwrap it.
5
+
6
+ ` munge ` has no features and is always ` #![no_std] ` .
7
+
8
+ ## Example
9
+
10
+ ``` rust
11
+ use {
12
+ :: core :: mem :: MaybeUninit ,
13
+ :: munge :: munge,
14
+ };
15
+
16
+ pub struct Example {
17
+ a : u32 ,
18
+ b : (char , f32 ),
19
+ }
20
+
21
+ let mut mu = MaybeUninit :: <Example >:: uninit ();
22
+
23
+ munge! (let Example { a , b : (c , mut f ) } = mu );
24
+ assert_eq! (a . write (10 ), & 10 );
25
+ assert_eq! (c . write ('x' ), & 'x' );
26
+ assert_eq! (f . write (3.14 ), & 3.14 );
27
+ f = & mut MaybeUninit :: uninit ();
28
+
29
+ // SAFETY: `mu` is completely initialized.
30
+ let init = unsafe { mu . assume_init () };
31
+ assert_eq! (init . a, 10 );
32
+ assert_eq! (init . b. 0 , 'x' );
33
+ assert_eq! (init . b. 1 , 3.14 );
34
+ ```
You can’t perform that action at this time.
0 commit comments