Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. use std::collections::{vec_deque, VecDeque};
  2. use std::fmt;
  3. use std;
  4.  
  5. #[derive(Debug)]
  6. pub struct LimitedFifoQueue<T> {
  7. size: usize,
  8. store: VecDeque<T>,
  9. }
  10.  
  11. impl<T> LimitedFifoQueue<T> where T: fmt::Display {
  12. pub fn new(size: usize) -> LimitedFifoQueue<T> {
  13. LimitedFifoQueue {
  14. size: size,
  15. store: VecDeque::with_capacity(size),
  16. }
  17. }
  18. pub fn push(&mut self, elem: T) {
  19. self.store.push_front(elem);
  20. if self.store.len() > self.size {
  21. self.store.pop_back();
  22. }
  23. }
  24. pub fn clear(&mut self) {
  25. self.store.clear();
  26. }
  27. }
  28.  
  29. impl<T> IntoIterator for LimitedFifoQueue<T> where T: fmt::Display {
  30. type Item = T;
  31. type IntoIter = vec_deque::IntoIter<T>;
  32. fn into_iter(self) -> Self::IntoIter {
  33. self.store.into_iter()
  34. }
  35. }
  36.  
  37. fn print_all<I>(lines: &I) where I: IntoIterator {
  38. for string in lines.into_iter() {
  39. println!("{}", string);
  40. }
  41. }
  42.  
  43. fn print_all<I>(lines: I)
  44. where I: IntoIterator,
  45. I::Item: fmt::Display,
  46. {
  47. for string in lines.into_iter() {
  48. println!("{}", string);
  49. }
  50. }
  51.  
  52. fn print_all<I>(lines: I)
  53. where I: IntoIterator,
  54. I::Item: fmt::Display,
  55. {
  56. for string in lines {
  57. println!("{}", string);
  58. }
  59. }
  60.  
  61. fn print_all<I>(lines: &I) where I: IntoIterator {
  62. for string in lines.into_iter() {
  63. println!("{}", string);
  64. }
  65. }
  66.  
  67. fn print_all<I>(lines: &I)
  68. where I: IntoIterator,
  69. I::Item: fmt::Display,
  70. { ... }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement