#traits #disjoint #either #type #combine #share

coalesce

Combine disjoint types that share common traits

2 releases

Uses old Rust 2015

0.1.1 Aug 23, 2015
0.1.0 Aug 6, 2015

#9 in #disjoint

Download history 13/week @ 2024-02-17 37/week @ 2024-02-24 47/week @ 2024-03-02 19/week @ 2024-03-09

116 downloads per month
Used in stack

MIT license

8KB
110 lines

coalesce

travis-badge release-badge docs-badge license-badge

coalesce allows for easy unification of disjoint types, often useful for combining multiple concrete instances into a common trait object. You can run an arbitrary code block over each item, see the API docs for example usage.


lib.rs:

Coalesce allows you to unify disjoint types on the stack.

It is often useful to return different implementations of a common trait such as Iterator or Read from conditional branches. The coalesce! macro makes it easy to unify them into a common trait object:

#[macro_use]
extern crate coalesce;

use coalesce::Coalesce2;
use std::iter::repeat;

let mut i = if some_condition() {
    Coalesce2::A(repeat(5u32).take(2))
} else {
    Coalesce2::B(0u32..8)
};
let i = coalesce!(2 => |ref mut i| i as &mut Iterator<Item=u32>);

for x in i {
    println!("{}", x);
}

No runtime deps