Hello,
I had a struct that contained a input : Vec<i32>
and could clone it with self.input.clone()
without any problem.
Now, I cannot clone it with the following struct :
pub struct PermutationIterator<T> {
/// vec that should permute its elements
input: Vec<T>,
// ...
}
Cargo Error :
error[E0599]: no method named `clone` found for type `std::vec::Vec<T>` in the current scope
--> src\lib.rs:118:37
|
118 | return Some( self.input.clone() );
| ^^^^^
|
= note: the method `clone` exists but the following trait bounds were not satisfied:
`std::vec::Vec<T> : std::clone::Clone`
error[E0599]: no method named `clone` found for type `std::vec::Vec<T>` in the current scope
--> src\lib.rs:141:39
|
141 | let result = Some( self.input.clone() );
| ^^^^^
|
= note: the method `clone` exists but the following trait bounds were not satisfied:
`std::vec::Vec<T> : std::clone::Clone`
Please advise.
Here is the link to compare the master branch and the branch that implement generics.
- The master branch is handling i32
- The Impl_generic branch should handle generic type instead of i32
Here is the full code :
pub struct PermutationIterator<T> {
/// vec that should permute its elements
input: Vec<T>,
/// vec that holds mobility information of each element of the `input`array
directions: Vec<Mobility>,
/// internal counter useful to emit the 1st array
counter: u32,
/// result to check if some errors happened
result: Result<(), String>,
/// maximum expected permutation. The iterator should return none if the maximum is reached
max: Option<u32>
}
impl<T> PermutationIterator<T>
where T: PartialOrd + Ord
{
/// Return an instance of an `Iterator` that will provide
/// each permutation when `next` method is invoked
pub fn new( input : Vec<T> ) -> PermutationIterator<T> {
let directions = create_directions( &input );
let mut iterator = PermutationIterator{ input,
directions,
counter: 0,
result: Ok(()),
max: None
};
iterator.input.sort();
iterator
}
/// Return `false`if all permutations occurs well
pub fn has_errors( &self ) -> bool {
return self.result.is_err();
}
/// set the maximum permutation to comput
/// e
/// # Example
///
/// ```
/// use permutation_way::PermutationIterator;
/// // input
/// let input = vec![1, 2, 3];
/// // call
/// let mut iterator = PermutationIterator::new( input );
/// iterator.set_max( 3 );
/// // assertions
/// assert_eq!( Some( vec![1, 2, 3] ), iterator.next() );
/// assert_eq!( Some( vec![1, 3, 2] ), iterator.next() );
/// assert_eq!( Some( vec![3, 1, 2] ), iterator.next() );
/// assert_eq!( None, iterator.next() );
/// assert_eq!( false, iterator.has_errors() );
/// ```
pub fn set_max( &mut self, max : u32 ) {
self.max = Some(max);
}
}
impl<T> Iterator for PermutationIterator<T>
where T: PartialOrd
{
type Item = Vec<T>;
fn next( &mut self ) -> Option<Self::Item> {
if self.input.len() == 0{
return None;
}
// check max is reach
if self.max == Some(self.counter) {
return None;
}
if self.counter == 0 {
self.counter = self.counter + 1;
return Some( self.input.clone() );
} else if self.input.len() == 1 {
// stop
return None;
}
let largest = find_largest_mobile_element(&self.input, &self.directions);
let direction = largest.direction;
let mobile_position = largest.position;
if direction == NotMobile {
self.counter = self.counter + 1;
return None;
}
let swap_result = direction.swap( &mut self.input, &mut self.directions, mobile_position );
if let Err( SwapError(position)) = swap_result {
self.result = Err(format!("swap permutation error at position {}", position));
// stop the iterator
return None;
}
let result = Some( self.input.clone() );
let reset_result = direction.reset( &self.input, &mut self.directions, mobile_position );
if let Err(SwapError(position)) = reset_result {
self.result = Err(format!("swap permutation error at position {}", position));
// stop the iterator
return None;
}
if let Err(ResetError(position)) = reset_result {
self.result = Err(format!( "reset permutation error at position {}", position));
// stop the iterator
return None;
}
self.counter = self.counter + 1;
result
}
}