参数化结构的运算符依赖于另一个运算符
Operator for parametrized struct is dependant on another operator
我正在用 Rust 编写 LoG 过滤器,我想使用 |
作为元素宽乘法运算符 (a_{i,j} * b_{i,j}
) 的运算符,但编译器抱怨 Output
结果。它说 self[(i, j)] * out[(i, j)]
不等于 Mul<N>::Output
。
impl<N> BitOr<Matrix<N>> for Matrix<N> where N: Mul<N> {
type Output = Matrix<N>;
fn bitor(self, other: Matrix<N>) -> Matrix<N> {
if self.width() != other.width() ||
self.height() != other.height() {
panic!("Matrices need to have equal dimensions");
}
let mut out = Matrix::new(self.width(), self.height());
for i in 0..(self.width()) {
for j in 0..(self.height()) {
out[(i, j)] = self[(i, j)] * out[(i, j)];
}
}
out
}
}
有什么方法可以根据 Mul<N>::Output
类型设置输出吗?
我想这应该可行:
impl<N> BitOr<Matrix<N>> for Matrix<N> where N: Mul<N> {
type Output = Matrix<<N as Mul<N>>::Output>;
fn bitor(self, other: Matrix<N>) -> Matrix<<N as Mul<N>>::Output> {
if self.width() != other.width() ||
self.height() != other.height() {
panic!("Matrices need to have equal dimensions");
}
let mut out = Matrix::new(self.width(), self.height());
for i in 0..(self.width()) {
for j in 0..(self.height()) {
out[(i, j)] = self[(i, j)] * out[(i, j)];
}
}
out
}
}
你没有提供可运行的小例子,所以我自己做了一个。这有效:
use std::ops::{Mul,BitOr};
#[derive(Copy,Show)]
struct Matrix<N>(N, N);
impl<N> BitOr<Matrix<N>> for Matrix<N> where N: Mul<N, Output=N> {
type Output = Matrix<N>;
fn bitor(self, other: Matrix<N>) -> Matrix<N> {
Matrix(self.0 * other.0, self.1 * other.1)
}
}
fn main() {
let a = Matrix(-1,-1);
let b = Matrix(2,3);
let c = a | b;
println!("{:?}", c)
}
我必须做的主要事情是 N: Mul<N, Output=N>
,它指定 N
必须乘以另一个 N
和 将导致在另一个 N
.
我正在用 Rust 编写 LoG 过滤器,我想使用 |
作为元素宽乘法运算符 (a_{i,j} * b_{i,j}
) 的运算符,但编译器抱怨 Output
结果。它说 self[(i, j)] * out[(i, j)]
不等于 Mul<N>::Output
。
impl<N> BitOr<Matrix<N>> for Matrix<N> where N: Mul<N> {
type Output = Matrix<N>;
fn bitor(self, other: Matrix<N>) -> Matrix<N> {
if self.width() != other.width() ||
self.height() != other.height() {
panic!("Matrices need to have equal dimensions");
}
let mut out = Matrix::new(self.width(), self.height());
for i in 0..(self.width()) {
for j in 0..(self.height()) {
out[(i, j)] = self[(i, j)] * out[(i, j)];
}
}
out
}
}
有什么方法可以根据 Mul<N>::Output
类型设置输出吗?
我想这应该可行:
impl<N> BitOr<Matrix<N>> for Matrix<N> where N: Mul<N> {
type Output = Matrix<<N as Mul<N>>::Output>;
fn bitor(self, other: Matrix<N>) -> Matrix<<N as Mul<N>>::Output> {
if self.width() != other.width() ||
self.height() != other.height() {
panic!("Matrices need to have equal dimensions");
}
let mut out = Matrix::new(self.width(), self.height());
for i in 0..(self.width()) {
for j in 0..(self.height()) {
out[(i, j)] = self[(i, j)] * out[(i, j)];
}
}
out
}
}
你没有提供可运行的小例子,所以我自己做了一个。这有效:
use std::ops::{Mul,BitOr};
#[derive(Copy,Show)]
struct Matrix<N>(N, N);
impl<N> BitOr<Matrix<N>> for Matrix<N> where N: Mul<N, Output=N> {
type Output = Matrix<N>;
fn bitor(self, other: Matrix<N>) -> Matrix<N> {
Matrix(self.0 * other.0, self.1 * other.1)
}
}
fn main() {
let a = Matrix(-1,-1);
let b = Matrix(2,3);
let c = a | b;
println!("{:?}", c)
}
我必须做的主要事情是 N: Mul<N, Output=N>
,它指定 N
必须乘以另一个 N
和 将导致在另一个 N
.