Coerce

Trait Coerce 

Source
pub trait Coerce<U> {
    // Required method
    fn coerce(self) -> U;
}
Expand description

Trait for performing coercion from one type to another, where the types are identical but the compiler can’t prove it.

§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

Required Methods§

Source

fn coerce(self) -> U

Implementations on Foreign Types§

Source§

impl<'a, T: 'static, U: 'static> Coerce<&'a [U]> for &'a [T]

Source§

fn coerce(self) -> &'a [U]

Source§

impl<'a, T: 'static, U: 'static> Coerce<&'a mut [U]> for &'a mut [T]

Source§

fn coerce(self) -> &'a mut [U]

Source§

impl<'a, T: 'static, U: 'static> Coerce<&'a U> for &'a T

Source§

fn coerce(self) -> &'a U

Source§

impl<'a, T: 'static, U: 'static> Coerce<&'a mut U> for &'a mut T

Implementors§