2015-11-06 106 views
0

我在這種情況下遇到的問題(這裏的代碼是可建):如何判斷Rust無法推斷哪種泛型?

extern crate rand; 
use rand::{Isaac64Rng, SeedableRng, Rng}; 
pub trait GeneticAlgorithm<R, Ins, C> : Clone where R: Rng { 
    fn mate(parents: (&Self, &Self), rng: &mut R) -> Self; 
    fn mutate<F>(&mut self, rng: &mut R, mutator: F) where F: FnMut(&mut Ins); 
    fn call<F>(&self, program: F) where F: FnOnce(&C); 
} 

pub struct Mep<Ins> { 
    instructions: Vec<Ins>, 
    unit_mutate_size: usize, 
    crossover_points: usize, 
} 

impl<Ins> Mep<Ins> { 
    //Generates a new Mep with a particular size and takes a closure to generate random instructions 
    pub fn new<I>(unit_mutate_size: usize, crossover_points: usize, instruction_iter: I) -> Mep<Ins> 
     where I: Iterator<Item=Ins> { 
     Mep{instructions: instruction_iter.collect(), unit_mutate_size: unit_mutate_size, 
      crossover_points: crossover_points} 
    } 
} 

impl<Ins> Clone for Mep<Ins> 
    where Ins: Clone { 
    fn clone(&self) -> Self { 
     Mep{instructions: self.instructions.clone(), unit_mutate_size: self.unit_mutate_size, 
      crossover_points: self.crossover_points} 
    } 
} 

impl<R, Ins> GeneticAlgorithm<R, Ins, Vec<Ins>> for Mep<Ins> where R: Rng, Ins: Clone { 
    fn mate(parents: (&Mep<Ins>, &Mep<Ins>), rng: &mut R) -> Mep<Ins> {} 
    fn mutate<F>(&mut self, rng: &mut R, mut mutator: F) where F: FnMut(&mut Ins) {} 
    fn call<F>(&self, program: F) where F: FnOnce(&Vec<Ins>) { 
     program(&self.instructions); 
    } 
} 

fn main() { 
    let mut rng = Isaac64Rng::from_seed(&[1, 2, 3, 4]); 
    let (a, b) = { 
     let mut clos = || Mep::new(3, 3, rng.gen_iter::<u32>().map(|x| x % 10).take(10)); 
     (clos(), clos()) 
    }; 
    let mut c = Mep::mate((&a, &b), &mut rng); 
    c.mutate(&mut rng, |ins: &mut u32| *ins = 2); 
    c.call(|x: &Vec<u32>| panic!()); 
} 

鏽聲稱,它可以不顯山露水推斷類型,但我不知道如何指定一個閉合的類型,如果是這樣的的問題,我也不是能夠識別哪些特定的泛型參數導致問題:

main.rs:48:7: 48:36 error: unable to infer enough type information about `_`; type annotations or generic parameter binding required [E0282] 
main.rs:48  c.call(|x: &Vec<u32>| panic!()); 
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

其中泛型參數必須指定,以及如何可以在確定?如果無法推斷,如何指定目標性狀:GeneticAlgorithm<Isaac64Rng, u32, Vec<u32>>

如果有人想自己構建原始代碼,我是hosting it on GitHub (commit b0b24482fb7fc71da9c23cd1481ea09c9edd867e)

+1

請參閱本系列中的[上一個問題](http://stackoverflow.com/questions/33569621/which-generic-parameter-must-be-specified)。 – Shepmaster

回答

1
impl<R, Ins> GeneticAlgorithm<R, Ins, Vec<Ins>> for Mep<Ins> where R: Rng, Ins: Clone { 
    // ... 
} 

impl此塊實現爲GeneticAlgorithmMep<Ins>R所有可能值。這意味着對於特定的Mep<Ins>GeneticAlgorithm特徵的多個實現。當調用matemutate方法時,編譯器能夠從參數中解析特定的實現,但是當調用call時,編譯器無法解析特定的實現,因爲R不受約束。

要解決此問題,請將R通用參數移至matemutate方法。

pub trait GeneticAlgorithm<Ins, C> : Clone { 
    fn mate<R>(parents: (&Self, &Self), rng: &mut R) -> Self where R: Rng; 
    fn mutate<R, F>(&mut self, rng: &mut R, mutator: F) where F: FnMut(&mut Ins), R: Rng; 
    fn call<F>(&self, program: F) where F: FnOnce(&C); 
} 

impl<Ins> GeneticAlgorithm<Ins, Vec<Ins>> for Mep<Ins> where Ins: Clone { 
    fn mate<R>(parents: (&Mep<Ins>, &Mep<Ins>), rng: &mut R) -> Mep<Ins> where R: Rng { panic!() } 
    fn mutate<R, F>(&mut self, rng: &mut R, mut mutator: F) where F: FnMut(&mut Ins), R: Rng { panic!() } 
    fn call<F>(&self, program: F) where F: FnOnce(&Vec<Ins>) { 
     program(&self.instructions); 
    } 
}