如何告诉 Rust 函数 `num_traits::pow::Pow` 已经为我的自定义特征实现
How to tell Rust that the function `num_traits::pow::Pow` is already implemented for my custom trait
我想创建一个特征,说它实现了 num_traits::pow::Pow - Rust。
我的特质目前定义为:
pub trait PrimeSieveTrait:
AddAssign + MulAssign + integer::Roots + FromPrimitive + ToPrimitive + PartialOrd + Copy {}
impl<T> PrimeSieveTrait for T
where T:
AddAssign + MulAssign + integer::Roots + FromPrimitive + ToPrimitive + PartialOrd + Copy {}
我关心的 T
类型是 u32
和 u64
,它们已经在 num_traits::pow::Pow - Rust 实现了 .pow()
功能,并且 return u32
和 u64
。我怎样才能修改特征定义以使下面的函数工作?
fn test_case<T: PrimeSieveTrait>(p: T, x: u32) -> T {
let one = T::from_u8(1).unwrap();
p.pow(x) - one
}
Rust 的错误非常有用:
error[E0599]: no method named `pow` found for type parameter `T` in the current scope
--> src/common/divisor.rs:30:19
|
30 | let p_power = p.pow(x);
| ^^^ method not found in `T`
|
= help: items from traits can only be used if the type parameter is bounded by the trait
help: the following traits define an item `pow`, perhaps you need to restrict type parameter `T` with one of them:
|
26 | fn step_calc_divisors<T: rug::ops::Pow + PrimeSieveTrait>(divisors: &mut Vec<T>, strict_lower_bound: T, upper_bound: T, p: T, x: u32) {
| ^^^^^^^^^^^^^^^^^^
26 | fn step_calc_divisors<T: num::PrimInt + PrimeSieveTrait>(divisors: &mut Vec<T>, strict_lower_bound: T, upper_bound: T, p: T, x: u32) {
| ^^^^^^^^^^^^^^^^^
26 | fn step_calc_divisors<T: num::traits::Pow + PrimeSieveTrait>(divisors: &mut Vec<T>, strict_lower_bound: T, upper_bound: T, p: T, x: u32) {
| ^^^^^^^^^^^^^^^^^^^^^
尝试第二个成功:
pub trait PrimeSieveTrait:
AddAssign + MulAssign + DivAssign + integer::Roots + PrimInt + FromPrimitive {}
impl<T> PrimeSieveTrait for T
where T:
AddAssign + MulAssign + DivAssign + integer::Roots + PrimInt + FromPrimitive {}
我想创建一个特征,说它实现了 num_traits::pow::Pow - Rust。
我的特质目前定义为:
pub trait PrimeSieveTrait:
AddAssign + MulAssign + integer::Roots + FromPrimitive + ToPrimitive + PartialOrd + Copy {}
impl<T> PrimeSieveTrait for T
where T:
AddAssign + MulAssign + integer::Roots + FromPrimitive + ToPrimitive + PartialOrd + Copy {}
我关心的 T
类型是 u32
和 u64
,它们已经在 num_traits::pow::Pow - Rust 实现了 .pow()
功能,并且 return u32
和 u64
。我怎样才能修改特征定义以使下面的函数工作?
fn test_case<T: PrimeSieveTrait>(p: T, x: u32) -> T {
let one = T::from_u8(1).unwrap();
p.pow(x) - one
}
Rust 的错误非常有用:
error[E0599]: no method named `pow` found for type parameter `T` in the current scope
--> src/common/divisor.rs:30:19
|
30 | let p_power = p.pow(x);
| ^^^ method not found in `T`
|
= help: items from traits can only be used if the type parameter is bounded by the trait
help: the following traits define an item `pow`, perhaps you need to restrict type parameter `T` with one of them:
|
26 | fn step_calc_divisors<T: rug::ops::Pow + PrimeSieveTrait>(divisors: &mut Vec<T>, strict_lower_bound: T, upper_bound: T, p: T, x: u32) {
| ^^^^^^^^^^^^^^^^^^
26 | fn step_calc_divisors<T: num::PrimInt + PrimeSieveTrait>(divisors: &mut Vec<T>, strict_lower_bound: T, upper_bound: T, p: T, x: u32) {
| ^^^^^^^^^^^^^^^^^
26 | fn step_calc_divisors<T: num::traits::Pow + PrimeSieveTrait>(divisors: &mut Vec<T>, strict_lower_bound: T, upper_bound: T, p: T, x: u32) {
| ^^^^^^^^^^^^^^^^^^^^^
尝试第二个成功:
pub trait PrimeSieveTrait:
AddAssign + MulAssign + DivAssign + integer::Roots + PrimInt + FromPrimitive {}
impl<T> PrimeSieveTrait for T
where T:
AddAssign + MulAssign + DivAssign + integer::Roots + PrimInt + FromPrimitive {}