Референтные черты: нет реализации для `& T * & T` - PullRequest
0 голосов
/ 25 июня 2019

Я пытаюсь написать универсальный метод и указать некоторый тип T: num_traits::float::Float из num-traits crate .Как мне попросить &T реализовать базовые числовые методы, такие как умножение?

use splines::{Interpolation, Key, Spline, interpolate};
use num_traits::{float::Float, identities};
use conv::prelude::*;


struct Interpolator<T>
where T: Float + interpolate::One + interpolate::Trigo {
    target_x: Vec<T>,
}

impl<T> Interpolator<T>
where T: Float + interpolate::One + interpolate::Trigo {
    fn interpolate<U>(&self, x: &[T], y: &[U]) -> Vec<U>
    where U: Float + identities::Zero {
        assert_eq!(x.len(), y.len());
        let key_iter = x.iter().zip(y).map(|(x, y)| Key::new(x, y, Interpolation::Linear));
        let spline = Spline::from_iter(key_iter);
        let result: Vec<U> = self.target_x.iter().map(|x| spline.sample(x).unwrap_or(identities::zero())).collect();
        result
    }
}


fn main() {
}

Cargo.toml

[package]
name = "name"
version = "0.1.0"
edition = "2018"

[dependencies]
conv = "0.3.2"
splines = "1.0.0-rc.3"
num-traits = "0.2"

Ошибки компиляции:

error[E0277]: the trait bound `&T: splines::interpolate::One` is not satisfied
  --> src/main.rs:18:66
   |
18 |         let result: Vec<U> = self.target_x.iter().map(|x| spline.sample(x).unwrap_or(identities::zero())).collect();
   |                                                                  ^^^^^^ the trait `splines::interpolate::One` is not implemented for `&T`

error[E0277]: the trait bound `&T: splines::interpolate::Trigo` is not satisfied
  --> src/main.rs:18:66
   |
18 |         let result: Vec<U> = self.target_x.iter().map(|x| spline.sample(x).unwrap_or(identities::zero())).collect();
   |                                                                  ^^^^^^ the trait `splines::interpolate::Trigo` is not implemented for `&T`

error[E0277]: cannot multiply `&T` to `&T`
  --> src/main.rs:18:66
   |
18 |         let result: Vec<U> = self.target_x.iter().map(|x| spline.sample(x).unwrap_or(identities::zero())).collect();
   |                                                                  ^^^^^^ no implementation for `&T * &T`
   |
   = help: the trait `std::ops::Mul` is not implemented for `&T`
   = help: consider adding a `where &T: std::ops::Mul` bound

error[E0277]: cannot divide `&T` by `&T`
  --> src/main.rs:18:66
   |
18 |         let result: Vec<U> = self.target_x.iter().map(|x| spline.sample(x).unwrap_or(identities::zero())).collect();
   |                                                                  ^^^^^^ no implementation for `&T / &T`
   |
   = help: the trait `std::ops::Div` is not implemented for `&T`
   = help: consider adding a `where &T: std::ops::Div` bound

error[E0277]: the trait bound `&U: splines::interpolate::Interpolate<&T>` is not satisfied
  --> src/main.rs:18:66
   |
18 |         let result: Vec<U> = self.target_x.iter().map(|x| spline.sample(x).unwrap_or(identities::zero())).collect();
   |                                                                  ^^^^^^ the trait `splines::interpolate::Interpolate<&T>` is not implemented for `&U`

error[E0277]: cannot add `&T` to `&T`
  --> src/main.rs:18:66
   |
18 |         let result: Vec<U> = self.target_x.iter().map(|x| spline.sample(x).unwrap_or(identities::zero())).collect();
   |                                                                  ^^^^^^ no implementation for `&T + &T`
   |
   = help: the trait `std::ops::Add` is not implemented for `&T`
   = help: consider adding a `where &T: std::ops::Add` bound
   = note: required because of the requirements on the impl of `splines::interpolate::Additive` for `&T`

error[E0277]: cannot subtract `&T` from `&T`
  --> src/main.rs:18:66
   |
18 |         let result: Vec<U> = self.target_x.iter().map(|x| spline.sample(x).unwrap_or(identities::zero())).collect();
   |                                                                  ^^^^^^ no implementation for `&T - &T`
   |
   = help: the trait `std::ops::Sub` is not implemented for `&T`
   = help: consider adding a `where &T: std::ops::Sub` bound
   = note: required because of the requirements on the impl of `splines::interpolate::Additive` for `&T`

error[E0277]: the trait bound `&U: num_traits::identities::Zero` is not satisfied
  --> src/main.rs:18:86
   |
18 |         let result: Vec<U> = self.target_x.iter().map(|x| spline.sample(x).unwrap_or(identities::zero())).collect();
   |                                                                                      ^^^^^^^^^^^^^^^^ the trait `num_traits::identities::Zero` is not implemented for `&U`
   |
   = note: required by `num_traits::identities::zero`

error[E0277]: a collection of type `std::vec::Vec<U>` cannot be built from an iterator over elements of type `&U`
  --> src/main.rs:18:107
   |
18 |         let result: Vec<U> = self.target_x.iter().map(|x| spline.sample(x).unwrap_or(identities::zero())).collect();
   |                                                                                                           ^^^^^^^ a collection of type `std::vec::Vec<U>` cannot be built from `std::iter::Iterator<Item=&U>`
   |
   = help: the trait `std::iter::FromIterator<&U>` is not implemented for `std::vec::Vec<U>`

error: aborting due to 9 previous errors

For more information about this error, try `rustc --explain E0277`.
error: Could not compile `trait_generic`.

1 Ответ

1 голос
/ 25 июня 2019

Может быть, вы могли бы просто рассмотреть возможность использования Key::new(*x, *y, Interpolation::Linear) и spline.sample(*x)?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...