Search code examples
rusttraitsgeneric-programming

How to tell Rust that the function `num_traits::pow::Pow` is already implemented for my custom trait


I want to create a trait that says it implements num_traits::pow::Pow - Rust.

My trait is currently defined as:

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 {}

The types of T I care about are u32 and u64, which already have a .pow() function implemented at num_traits::pow::Pow - Rust, and which return u32 and u64. How can I modify the trait definition to get the below function working?

fn test_case<T: PrimeSieveTrait>(p: T, x: u32) -> T {
  let one = T::from_u8(1).unwrap();
  p.pow(x) - one
}

Solution

  • Rust's errors were quite helpful:

    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) {
       |                       ^^^^^^^^^^^^^^^^^^^^^
    

    Trying the second one worked:

    pub trait PrimeSieveTrait:
      AddAssign + MulAssign + DivAssign + integer::Roots + PrimInt + FromPrimitive {}
    impl<T> PrimeSieveTrait for T
      where T:
        AddAssign + MulAssign + DivAssign + integer::Roots + PrimInt + FromPrimitive {}