Improve generics usability for `Array<T>`

Aperta
#264 7 commenti 0 reazioni 1 assegnatario Vedi su GitHub

@9prady9 ci sta già lavorando.

Dal 28/11/2020.

Valutazione

Questa issue non è ancora stata valutata.

Descrizione

Enhancement

I encountered difficulty when writing generic functions using Array<T> e.g.

fn test_basic_op<T>(a: &af::Array<T>)
where
    // T: af::Fromf64 + af::HasAfEnum + ???
{
    println!("Element-wise arithmetic");
    let b = af::add(&af::sin(a), &T::fromf64(1.5f64), false);

    let b2 = af::add(&af::sin(a), &af::cos(a), false);

    let b3 = !a;
    af::af_print!("sin(a) + 1.5 => ", b);
    af::af_print!("sin(a) + cos(a) => ", b2);
    af::af_print!("!a => ", b3);

    let a_plus_b = a.clone() + b.clone();
    af::af_print!("a + b", a_plus_b);

    let minus_a = -(a.clone());
    af::af_print!("-a ", minus_a);
}

After long fight with compile error, I found that

fn test_basic_op<T>(a: &af::Array<T>)
where
    T: af::HasAfEnum<UnaryOutType = T>
        + af::Fromf64
        + af::Convertable<OutType = T>
        + af::ImplicitPromote<T, Output = T>,
    af::Array<T>: std::ops::Neg<Output = af::Array<T>>,
{
    ...
}

compiles and it's OK.

However, I feel that:

  • Convertable and ImplicitPromote seems implementation detail and I don't want to write them every time when using Array<T>.
  • T should be Convertable<OutType=T> automatically when T: HasAfEnum (i.e. T is supported in arrayfire).

So I want:

  1. Alias-like trait (e.g. AfScalar: HasAfEnum + ...) which enables us ignoring implementation detail when using Array<T> in generic code
  2. Generic trait implementation (e.g. Convertable), not implementation for f32, f64 and other types individually

What are your thoughts?

Lingua principale
Rust
Stelle
827
Fork
59
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di arrayfire/arrayfire-rust

Tutte le issue di arrayfire/arrayfire-rust

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.