Skip to content

Commit 50fddee

Browse files
committed
Initial commit
0 parents  commit 50fddee

File tree

5 files changed

+685
-0
lines changed

5 files changed

+685
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target
2+
Cargo.lock

Cargo.toml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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]

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
```

crates-io.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
```

0 commit comments

Comments
 (0)