https://github.com/taiki-e/io-enum
#[derive(Read, Write, Seek, BufRead)] for enums.
https://github.com/taiki-e/io-enum
proc-macro rust
Last synced: 11 months ago
JSON representation
#[derive(Read, Write, Seek, BufRead)] for enums.
- Host: GitHub
- URL: https://github.com/taiki-e/io-enum
- Owner: taiki-e
- License: apache-2.0
- Created: 2019-02-03T08:34:37.000Z (about 7 years ago)
- Default Branch: main
- Last Pushed: 2025-03-09T09:11:24.000Z (11 months ago)
- Last Synced: 2025-03-09T10:19:28.371Z (11 months ago)
- Topics: proc-macro, rust
- Language: Rust
- Homepage: https://docs.rs/io-enum
- Size: 301 KB
- Stars: 8
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# io-enum
[](https://crates.io/crates/io-enum)
[](https://docs.rs/io-enum)
[](#license)
[](https://www.rust-lang.org)
[](https://github.com/taiki-e/io-enum/actions)
\#\[derive(Read, Write, Seek, BufRead)\] for enums.
## Usage
Add this to your `Cargo.toml`:
```toml
[dependencies]
io-enum = "1"
```
## Examples
```rust
use std::{
fs::File,
io::{self, Write},
path::Path,
};
use io_enum::*;
#[derive(Read, Write, Seek, BufRead)]
enum Either {
A(A),
B(B),
}
fn func(path: Option<&Path>) -> impl Write {
if let Some(path) = path {
Either::A(File::open(path).unwrap())
} else {
Either::B(io::stdout())
}
}
```
See [auto_enums] crate for how to automate patterns like this.
## Supported traits
- [`Read`](https://doc.rust-lang.org/std/io/trait.Read.html) - [example](https://github.com/taiki-e/io-enum/blob/HEAD/tests/expand/read.rs) | [generated code](https://github.com/taiki-e/io-enum/blob/HEAD/tests/expand/read.expanded.rs)
- [`BufRead`](https://doc.rust-lang.org/std/io/trait.BufRead.html) - [example](https://github.com/taiki-e/io-enum/blob/HEAD/tests/expand/buf_read.rs) | [generated code](https://github.com/taiki-e/io-enum/blob/HEAD/tests/expand/buf_read.expanded.rs)
- [`Write`](https://doc.rust-lang.org/std/io/trait.Write.html) - [example](https://github.com/taiki-e/io-enum/blob/HEAD/tests/expand/write.rs) | [generated code](https://github.com/taiki-e/io-enum/blob/HEAD/tests/expand/write.expanded.rs)
- [`Seek`](https://doc.rust-lang.org/std/io/trait.Seek.html) - [example](https://github.com/taiki-e/io-enum/blob/HEAD/tests/expand/seek.rs) | [generated code](https://github.com/taiki-e/io-enum/blob/HEAD/tests/expand/seek.expanded.rs)
## Related Projects
- [auto_enums]: A library for to allow multiple return types by automatically generated enum.
- [derive_utils]: A procedural macro helper for easily writing [derives macros][proc-macro-derive] for enums.
- [iter-enum]: \#\[derive(Iterator, DoubleEndedIterator, ExactSizeIterator, FusedIterator, Extend)\] for enums.
[auto_enums]: https://github.com/taiki-e/auto_enums
[derive_utils]: https://github.com/taiki-e/derive_utils
[iter-enum]: https://github.com/taiki-e/iter-enum
[proc-macro-derive]: https://doc.rust-lang.org/reference/procedural-macros.html#derive-macros
## License
Licensed under either of [Apache License, Version 2.0](LICENSE-APACHE) or
[MIT license](LICENSE-MIT) at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you, as defined in the Apache-2.0 license, shall
be dual licensed as above, without any additional terms or conditions.