I encountered an issue in this situation (code here is buildable):
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!());
}
Rust claims that it cannot infer a type somewhere, but I am not sure how to specify the type of a closure if that is the issue, nor am I able to identify which specific generic parameter is causing the issue:
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!());
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Which generic parameter is necessary to specify and how can that be determined? If it cannot be inferred, how is it possible to specify the intended trait: GeneticAlgorithm<Isaac64Rng, u32, Vec<u32>>
If anyone would like to build the original code itself, I am hosting it on GitHub (commit b0b24482fb7fc71da9c23cd1481ea09c9edd867e) .
impl<R, Ins> GeneticAlgorithm<R, Ins, Vec<Ins>> for Mep<Ins> where R: Rng, Ins: Clone {
// ...
}
This impl
block implements GeneticAlgorithm
for Mep<Ins>
for all possible values of R
. This means that there are multiple implementations of the GeneticAlgorithm
trait for a particular Mep<Ins>
. When you invoke the mate
and mutate
methods, the compiler is able to resolve a specific implementation from the arguments, but when you invoke call
, the compiler is unable to resolve a specific implementation, since R
is unconstrained.
To solve this, move the R
generic parameter to the mate
and mutate
methods.
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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.