Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/willcrichton/simple-bind
One-line non-exhaustive binds in Rust
https://github.com/willcrichton/simple-bind
Last synced: 2 months ago
JSON representation
One-line non-exhaustive binds in Rust
- Host: GitHub
- URL: https://github.com/willcrichton/simple-bind
- Owner: willcrichton
- License: apache-2.0
- Created: 2018-03-26T23:35:47.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-05-13T03:55:48.000Z (over 6 years ago)
- Last Synced: 2024-09-14T13:14:03.388Z (3 months ago)
- Language: Rust
- Size: 22.5 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# simple-bind: one-line non-exhaustive binds in Rust
[![Build Status](https://travis-ci.org/willcrichton/simple-bind.svg?branch=master)](https://travis-ci.org/willcrichton/simple-bind)
[![](https://img.shields.io/crates/v/simple-bind.svg)](https://crates.io/crates/simple-bind)
[![](https://docs.rs/simple-bind/badge.svg)](https://docs.rs/simple-bind/)**Nightly-only crate.**
```rust
// Here's a brief example that demonstrates how simple-bind works.#![feature(proc_macro, pattern_parentheses, stmt_expr_attributes)]
extern crate simple_bind;
use simple_bind::bind;fn main() {
enum A { Foo(i32), Bar };// Let's say you have a variant of an enum.
let x = A::Foo(10);// Previously, if you knew `x` was `Foo` and just wanted to access the inside,
// you had to do either:
let y = match x { A::Foo(y) => y, _ => unreachable!() };
// or...
let y = if let A::Foo(y) = x { y } else { unreachable!() };// With simple-bind, you can instead do:
bind!{let A::Foo(y) = x;}// No more nested match/if statements!
assert_eq!(y, 10);
}
```## Setup
Use of this crate uses the unstable `proc_macro` API, so it requires nightly and a few feature gates.
Enable nightly on your repository:
```
rustup override set nightly
```Add this line to your `cargo.toml`:
```toml
[dependencies]
simple-bind = "0.1.5"
```To your main module file (`lib.rs` or `main.rs`), add:
```rust
#![feature(proc_macro, pattern_parentheses, stmt_expr_attributes)]
extern crate simple_bind;
```Then wherever you want to use the macro, use normal imports (i.e. not `#[macro_use]`):
```rust
use simple_bind::bind;
```## Examples
```rust
fn main() {
enum B { Quux(i32) };
enum A { Foo(i32), Bar{y: i32}, Baz(B) };// Simple binds
bind!{let A::Foo(x) = A::Foo(10);}// Struct binds
let s = A::Bar{y: 1};
bind!{let A::Bar{y} = s;}// Nested binds
bind!{let A::Baz(B::Quux(x)) = A::Baz(B::Quux(10));}// Reference binds
let mut s = A::Foo(10);
bind!{let &mut A::Foo(ref mut y) = &mut s;}
*y = 3;
}
```## Issues
This implementation just covers cases I've run in to, and is not exhaustive to all possible binds. If you find a problem, feel free to submit an issue or a PR!