arrayfire / arrayfire/arrayfire-rust

Improve generics usability for `Array<T>`

未关闭
#264 7 条评论 0 个 reaction 已指派 1 人 已被 @9prady9 认领 在 GitHub 查看
Enhancement
主要语言
Rust
星标
827
派生
59
PR 合并指标
30 天内没有已合并 PR

描述

I encountered difficulty when writing generic functions using `Array` e.g.
```rust
fn test_basic_op(a: &af::Array)
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
```rust
fn test_basic_op(a: &af::Array)
where
T: af::HasAfEnum
+ af::Fromf64
+ af::Convertable
+ af::ImplicitPromote,
af::Array: std::ops::Neg>,
{
...
}
```
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` should be `Convertable` 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` in generic code
1. Generic trait implementation (e.g. `Convertable`), not implementation for `f32`, `f64` and other types individually

What are your thoughts?

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。