-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 50fddee
Showing
5 changed files
with
685 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/target | ||
Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "munge" | ||
version = "0.1.0" | ||
authors = ["David Koloski <[email protected]>"] | ||
edition = "2021" | ||
description = "Macro for easily initializing `MaybeUninit`s" | ||
license = "MIT" | ||
documentation = "https://docs.rs/munge" | ||
repository = "https://github.com/djkoloski/munge" | ||
keywords = ["munge", "macro", "initialize"] | ||
categories = ["no-std", "rust-patterns"] | ||
readme = "crates-io.md" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# `munge` | ||
|
||
`munge` makes it easy to initialize `MaybeUninit`s. | ||
|
||
Just use the `munge!` macro to destructure `MaybeUninit`s the same way you'd destructure a value. | ||
Initialize all the fields, then call `assume_init` to unwrap it. | ||
|
||
`munge` has no features and is always `#![no_std]`. | ||
|
||
## Example | ||
|
||
```rust | ||
use { | ||
::core::mem::MaybeUninit, | ||
::munge::munge, | ||
}; | ||
|
||
pub struct Example { | ||
a: u32, | ||
b: (char, f32), | ||
} | ||
|
||
let mut mu = MaybeUninit::<Example>::uninit(); | ||
|
||
munge!(let Example { a, b: (c, mut f) } = mu); | ||
assert_eq!(a.write(10), &10); | ||
assert_eq!(c.write('x'), &'x'); | ||
assert_eq!(f.write(3.14), &3.14); | ||
f = &mut MaybeUninit::uninit(); | ||
|
||
// SAFETY: `mu` is completely initialized. | ||
let init = unsafe { mu.assume_init() }; | ||
assert_eq!(init.a, 10); | ||
assert_eq!(init.b.0, 'x'); | ||
assert_eq!(init.b.1, 3.14); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
`munge` makes it easy to initialize `MaybeUninit`s. | ||
|
||
Just use the `munge!` macro to destructure `MaybeUninit`s the same way you'd destructure a value. | ||
Initialize all the fields, then call `assume_init` to unwrap it. | ||
|
||
`munge` has no features and is always `#![no_std]`. | ||
|
||
## Example | ||
|
||
```rust | ||
use { | ||
::core::mem::MaybeUninit, | ||
::munge::munge, | ||
}; | ||
|
||
pub struct Example { | ||
a: u32, | ||
b: (char, f32), | ||
} | ||
|
||
let mut mu = MaybeUninit::<Example>::uninit(); | ||
|
||
munge!(let Example { a, b: (c, mut f) } = mu); | ||
assert_eq!(a.write(10), &10); | ||
assert_eq!(c.write('x'), &'x'); | ||
assert_eq!(f.write(3.14), &3.14); | ||
f = &mut MaybeUninit::uninit(); | ||
|
||
// SAFETY: `mu` is completely initialized. | ||
let init = unsafe { mu.assume_init() }; | ||
assert_eq!(init.a, 10); | ||
assert_eq!(init.b.0, 'x'); | ||
assert_eq!(init.b.1, 3.14); | ||
``` |
Oops, something went wrong.