coe-rs

Crates.iocoe-rs
lib.rscoe-rs
version0.1.2
sourcesrc
created_at2023-03-20 22:34:15.940785
updated_at2023-03-20 22:41:13.327649
descriptionType coercion
homepage
repositoryhttps://github.com/sarah-ek/coe-rs/
max_upload_size
id815650
size6,942
sarah quiƱones (sarah-quinones)

documentation

README

Documentation Crate

coe-rs is a Rust library for coercing a value of a given type into the same type, in cases where the compiler can't prove the two types are equal.
This can be used to emulate specialization in to a limited extent.

Example

use coe::{Coerce, is_same};
use core::ops::Add;
fn foo<T: 'static + Copy + Add<Output = T>>(slice: &mut [T]) {
    if is_same::<f64, T>() {
        // use some optimized SIMD implementation
        // ...
        println!("using SIMD operations");
        let slice: &mut [f64] = slice.coerce();
    } else {
        for value in slice {
            println!("using fallback implementation");
            *value = *value + *value;
        }
    }
}
foo(&mut [1, 2, 3u64]); // calls fallback implementation
foo(&mut [1.0, 2.0, 3.0f64]); // calls SIMD implementation
Commit count: 7

cargo fmt