daily pastebin goal
46%
SHARE
TWEET

Untitled

a guest Mar 26th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #![allow(non_snake_case)]
  2. #![feature(test)]
  3. extern crate test;
  4.  
  5.  
  6. use std::iter::FromIterator;
  7.  
  8. #[cfg(test)]
  9. use test::Bencher;
  10.  
  11. #[derive(Copy, Clone, PartialEq, Eq, Debug)]
  12. struct LastOne<T>(Option<T>);
  13.  
  14. impl<T> FromIterator<T> for LastOne<T> {
  15.     #[inline]
  16.     fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> LastOne<T> {
  17.         LastOne(iter.into_iter().last())
  18.     }
  19. }
  20.  
  21. type Last<T> = LastOne<T>;
  22. impl<T> Last<T> {
  23.     fn new(x: T) -> Last<T> { LastOne(Some(x)) }
  24. }
  25.  
  26. fn from_iter_baseline<C, T, E, I>(iter: I) -> Result<C, E>
  27. where I: Iterator<Item = Result<T, E>>,
  28.       C: FromIterator<T>,
  29. {
  30.     FromIterator::from_iter(iter)
  31. }
  32.  
  33. fn from_iter_adapter<C, T, E, I>(iter: I) -> Result<C, E>
  34. where I: Iterator<Item = Result<T, E>>,
  35.       C: FromIterator<T>,
  36. {
  37.     struct Adapter<Iter, E> {
  38.         iter: Iter,
  39.         err: Option<E>,
  40.     }
  41.  
  42.     impl<T, E, Iter: Iterator<Item=Result<T, E>>> Iterator for Adapter<Iter, E> {
  43.         type Item = T;
  44.  
  45.         #[inline]
  46.         fn next(&mut self) -> Option<T> {
  47.             match self.iter.next() {
  48.                 Some(Ok(value)) => Some(value),
  49.                 Some(Err(err)) => {
  50.                     self.err = Some(err);
  51.                     None
  52.                 }
  53.                 None => None,
  54.             }
  55.         }
  56.  
  57.         fn size_hint(&self) -> (usize, Option<usize>) {
  58.             let (_min, max) = self.iter.size_hint();
  59.             (0, max)
  60.         }
  61.     }
  62.  
  63.     let mut adapter = Adapter { iter, err: None };
  64.     let v: C = FromIterator::from_iter(adapter.by_ref());
  65.  
  66.     match adapter.err {
  67.         Some(err) => Err(err),
  68.         None => Ok(v),
  69.     }
  70. }
  71.  
  72. fn from_iter_with_scan_with_freevar<C, T, E, I>(iter: I) -> Result<C, E>
  73. where I: Iterator<Item = Result<T, E>>,
  74.       C: FromIterator<T>,
  75. {
  76.     let mut err = None;
  77.  
  78.     let v = {
  79.         let adapter = iter.scan((), |_, item| {
  80.             match item {
  81.                 Ok(x) => Some(x),
  82.                 Err(x) => {
  83.                     err = Some(x);
  84.                     None
  85.                 },
  86.             }
  87.         });
  88.  
  89.         FromIterator::from_iter(adapter)
  90.     };
  91.  
  92.     match err {
  93.         Some(err) => Err(err),
  94.         None => Ok(v),
  95.     }
  96. }
  97.  
  98. fn from_iter_with_scan_passing_state<C, T, E, I>(iter: I) -> Result<C, E>
  99. where I: Iterator<Item = Result<T, E>>,
  100.       C: FromIterator<T>,
  101. {
  102.     let mut err = None;
  103.  
  104.     let v = {
  105.         let adapter = iter.scan(&mut err, |err, item| {
  106.             match item {
  107.                 Ok(x) => Some(x),
  108.                 Err(x) => {
  109.                     **err = Some(x);
  110.                     None
  111.                 },
  112.             }
  113.         });
  114.  
  115.         FromIterator::from_iter(adapter)
  116.     };
  117.  
  118.     match err {
  119.         Some(err) => Err(err),
  120.         None => Ok(v),
  121.     }
  122. }
  123.  
  124. fn from_iter_with_filter_map<C, T, E, I>(iter: I) -> Result<C, E>
  125. where I: Iterator<Item = Result<T, E>>,
  126.       C: FromIterator<T>,
  127. {
  128.     let mut err = None;
  129.  
  130.     let v = {
  131.         let adapter = iter.filter_map(|item| {
  132.             match item {
  133.                 Ok(x) => Some(x),
  134.                 Err(x) => {
  135.                     err = Some(x);
  136.                     None
  137.                 },
  138.             }
  139.         });
  140.  
  141.         FromIterator::from_iter(adapter)
  142.     };
  143.  
  144.     match err {
  145.         Some(err) => Err(err),
  146.         None => Ok(v),
  147.     }
  148. }
  149.  
  150. fn main() {
  151.     let one_a: Result<Last<u8>, _> = from_iter_baseline("1,2,3".split(",").map(|x| x.parse()));
  152.     let one_b: Result<Last<u8>, _> = from_iter_adapter("1,2,3".split(",").map(|x| x.parse()));
  153.     let two_a: Result<Last<u8>, _> = from_iter_with_scan_passing_state("1,2,3".split(",").map(|x| x.parse()));
  154.     let two_b: Result<Last<u8>, _> = from_iter_with_scan_with_freevar("1,2,3".split(",").map(|x| x.parse()));
  155.     let three: Result<Last<u8>, _> = from_iter_with_filter_map("1,2,3".split(",").map(|x| x.parse()));
  156.     println!("{:?}", one_a);
  157.     println!("{:?}", one_b);
  158.     println!("{:?}", two_a);
  159.     println!("{:?}", two_b);
  160.     println!("{:?}", three);
  161.  
  162.     let one_a: Result<Last<u8>, _> = from_iter_baseline("1,a,3".split(",").map(|x| x.parse()));
  163.     let one_b: Result<Last<u8>, _> = from_iter_adapter("1,a,3".split(",").map(|x| x.parse()));
  164.     let two_a: Result<Last<u8>, _> = from_iter_with_scan_passing_state("1,a,3".split(",").map(|x| x.parse()));
  165.     let two_b: Result<Last<u8>, _> = from_iter_with_scan_with_freevar("1,a,3".split(",").map(|x| x.parse()));
  166.     let three: Result<Last<u8>, _> = from_iter_with_filter_map("1,a,3".split(",").map(|x| x.parse()));
  167.     println!("{:?}", one_a);
  168.     println!("{:?}", one_b);
  169.     println!("{:?}", two_a);
  170.     println!("{:?}", two_b);
  171.     println!("{:?}", three);
  172. }
  173.  
  174. #[bench]
  175. fn bench__vec_from_iter_using_baseline___(b: &mut Bencher) {
  176.     let expected = vec![1; 1000];
  177.     let v: Vec<Result<isize, String>> = vec![Ok(1); 1000];
  178.     b.iter(|| {
  179.         let result: Result<Vec<isize>, String> = from_iter_baseline(v.iter().cloned());
  180.         assert_eq!(result.unwrap(), expected);
  181.     });
  182. }
  183.  
  184. #[bench]
  185. fn bench__vec_from_iter_using_adapter____(b: &mut Bencher) {
  186.     let expected = vec![1; 1000];
  187.     let v: Vec<Result<isize, String>> = vec![Ok(1); 1000];
  188.     b.iter(|| {
  189.         let result: Result<Vec<isize>, String> = from_iter_adapter(v.iter().cloned());
  190.         assert_eq!(result.unwrap(), expected);
  191.     });
  192. }
  193.  
  194. #[bench]
  195. fn bench__vec_from_iter_with_scan_param__(b: &mut Bencher) {
  196.     let expected = vec![1; 1000];
  197.     let v: Vec<Result<isize, String>> = vec![Ok(1); 1000];
  198.     b.iter(|| {
  199.         let result: Result<Vec<isize>, String> = from_iter_with_scan_passing_state(v.iter().cloned());
  200.         assert_eq!(result.unwrap(), expected);
  201.     });
  202. }
  203.  
  204. #[bench]
  205. fn bench__vec_from_iter_with_scan_freevar(b: &mut Bencher) {
  206.     let expected = vec![1; 1000];
  207.     let v: Vec<Result<isize, String>> = vec![Ok(1); 1000];
  208.     b.iter(|| {
  209.         let result: Result<Vec<isize>, String> = from_iter_with_scan_with_freevar(v.iter().cloned());
  210.         assert_eq!(result.unwrap(), expected);
  211.     });
  212. }
  213.  
  214. #[bench]
  215. fn bench__vec_from_iter_with_filter_map__(b: &mut Bencher) {
  216.     let expected = vec![1; 1000];
  217.     let v: Vec<Result<isize, String>> = vec![Ok(1); 1000];
  218.     b.iter(|| {
  219.         let result: Result<Vec<isize>, String> = from_iter_with_filter_map(v.iter().cloned());
  220.         assert_eq!(result.unwrap(), expected);
  221.     });
  222. }
  223.  
  224. #[bench]
  225. fn bench_last_from_iter_using_baseline___(b: &mut Bencher) {
  226.     let expected = Last::new(1);
  227.     let v: Vec<Result<isize, String>> = vec![Ok(1); 3000];
  228.     b.iter(|| {
  229.         let result: Result<Last<isize>, String> = from_iter_baseline(v.iter().cloned());
  230.         assert_eq!(result.unwrap(), expected);
  231.     });
  232. }
  233.  
  234. #[bench]
  235. fn bench_last_from_iter_using_adapter____(b: &mut Bencher) {
  236.     let expected = Last::new(1);
  237.     let v: Vec<Result<isize, String>> = vec![Ok(1); 3000];
  238.     b.iter(|| {
  239.         let result: Result<Last<isize>, String> = from_iter_adapter(v.iter().cloned());
  240.         assert_eq!(result.unwrap(), expected);
  241.     });
  242. }
  243.  
  244. #[bench]
  245. fn bench_last_from_iter_with_scan_param__(b: &mut Bencher) {
  246.     let expected = Last::new(1);
  247.     let v: Vec<Result<isize, String>> = vec![Ok(1); 3000];
  248.     b.iter(|| {
  249.         let result: Result<Last<isize>, String> = from_iter_with_scan_passing_state(v.iter().cloned());
  250.         assert_eq!(result.unwrap(), expected);
  251.     });
  252. }
  253.  
  254. #[bench]
  255. fn bench_last_from_iter_with_scan_freevar(b: &mut Bencher) {
  256.     let expected = Last::new(1);
  257.     let v: Vec<Result<isize, String>> = vec![Ok(1); 3000];
  258.     b.iter(|| {
  259.         let result: Result<Last<isize>, String> = from_iter_with_scan_with_freevar(v.iter().cloned());
  260.         assert_eq!(result.unwrap(), expected);
  261.     });
  262. }
  263.  
  264. #[bench]
  265. fn bench_last_from_iter_with_filter_map__(b: &mut Bencher) {
  266.     let expected = Last::new(1);
  267.     let v: Vec<Result<isize, String>> = vec![Ok(1); 3000];
  268.     b.iter(|| {
  269.         let result: Result<Last<isize>, String> = from_iter_with_filter_map(v.iter().cloned());
  270.         assert_eq!(result.unwrap(), expected);
  271.     });
  272. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top