Search code examples
functiongenericsrustclosurestraits

closures: expected u32 found type parameter


The problem is that my generic type T only accepts arguments from u32 and in this case I am trying to pass a generic value that I store in U.

Is there a way to cast generics to a specific type? What can I do to solve the problem?

#[derive(Debug)]
struct Cacher<T, U> where T: Fn(u32) -> u32 {
    calcular:T,
    valor:Option<U>,
}
impl<T, U> Cacher<T, U> where T: Fn(u32) -> u32 {
    fn nuevo(calcular:T, valor:U) -> Cacher<T, U> {
        Cacher{
            calcular,
            valor:None,
        }
    }
    fn valor(&mut self, arg:U) -> u32 {
        match self.valor {
            Some(v) => v,
            None => {
                let v = (self.calcular)(arg);
                self.valor = Some(arg);
                v
            }
        }
    }
}

fn generar(intensidad:u32){
    let mut resultado = Cacher::nuevo(|num| {
        println!("Trabajando");
        num
    },None);

    let res_a = resultado.valor(Some(4));
}

fn main() {
    generar(3);
}

Solution

  • Either remove the generic type parameter U from your implementation and only use u32s:

    #[derive(Debug)]
    struct Cacher<T>
        where T: Fn(u32) -> u32 
    {
        calcular: T,
        valor: Option<u32>,
    }
    
    impl<T> Cacher<T>
        where T: Fn(u32) -> u32
    {
        fn nuevo(calcular: T) -> Cacher<T> {
            Cacher {
                calcular,
                valor: None,
            }
        }
    
        fn valor(&mut self, arg: u32) -> u32 {
            match self.valor {
                Some(v) => v,
                None => {
                    let v = (self.calcular)(arg);
                    self.valor = Some(arg);
                    v
                }
            }
        }
    }
    
    fn main() {
        let mut resultado = Cacher::nuevo(|num| {
            println!("Trabajando");
            num
        });
    
        // "Trabajando" only printed once
        // hence we know we got a cache hit
        let res_a = resultado.valor(4);
        let res_a = resultado.valor(4);
        
    
        println!("{:?}", res_a);
    }
    

    playground

    Or make your implementation fully-generic:

    #[derive(Debug)]
    struct Cacher<T, U>
        where T: Fn(U) -> U, U: Copy
    {
        calcular: T,
        valor: Option<U>,
    }
    
    impl<T, U> Cacher<T, U>
        where T: Fn(U) -> U, U: Copy
    {
        fn nuevo(calcular: T) -> Cacher<T, U> {
            Cacher {
                calcular,
                valor: None,
            }
        }
    
        fn valor(&mut self, arg: U) -> U {
            match self.valor {
                Some(v) => v,
                None => {
                    let v = (self.calcular)(arg);
                    self.valor = Some(arg);
                    v
                }
            }
        }
    }
    
    fn main() {
        let mut resultado = Cacher::nuevo(|num| {
            println!("Trabajando");
            num
        });
    
        // "Trabajando" only printed once
        // hence we know we got a cache hit
        let res_a = resultado.valor(4);
        let res_a = resultado.valor(4);
        
    
        println!("{:?}", res_a);
    }
    

    playground

    The above implementation will work for any U that implements Copy, so that includes all number primitives, e.g. u8, u16, etc.