Skip to content

Instantly share code, notes, and snippets.

@leshow
Created June 22, 2017 15:31
Show Gist options
  • Select an option

  • Save leshow/075e81aa2c588bc0cfb7eecb64707a5e to your computer and use it in GitHub Desktop.

Select an option

Save leshow/075e81aa2c588bc0cfb7eecb64707a5e to your computer and use it in GitHub Desktop.
use std::ops::Shl;
#[derive(Debug)]
struct V<T>(Vec<T>);
impl<T> Shl<usize> for V<T> {
type Output = Self;
fn shl(mut self, rhs: usize) -> V<T> {
let l = self.0.len();
rotate_by(&mut self.0, 0, rhs-1);
rotate_by(&mut self.0, rhs, l-1);
rotate_by(&mut self.0, 0, l-1);
fn rotate_by<T>(arr: &mut [T], mut left: usize, mut right: usize) {
while left < right {
arr.swap(left, right);
left += 1;
right -= 1;
}
}
self
}
}
fn main() {
let v_ = vec![1,2,3,4,5];
let v = V(v_);
println!("{:?}", (v << 2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment