SHARE
TWEET

Untitled

a guest Jul 18th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #![feature(unboxed_closures)]
  2.  
  3. fn this<F>(mut f: F)
  4. where
  5.     F: FnMut<()>,
  6.     F::Output: Extend<u8>,
  7. {
  8.     f().extend([1,2,3].iter().cloned());
  9.     f().extend([1,2,3].iter().cloned());
  10. }
  11.  
  12. fn use_that<T: Extend<u8>>(t: &mut T) {
  13.     this(|| with_offset(t, 1))
  14. }
  15.  
  16. fn with_offset<T: Extend<u8>>(inner: &mut T, offset: u8) -> impl Extend<u8> + '_ {
  17.     ExtendWithOffset { inner, offset }
  18. }
  19.  
  20. struct ExtendWithOffset<'a, T> {
  21.     inner: &'a mut T,
  22.     offset: u8
  23. }
  24.  
  25. impl<'a, T: Extend<u8>> Extend<u8> for ExtendWithOffset<'a, T> {
  26.     fn extend<I: IntoIterator<Item=u8>>(&mut self, iter: I) {
  27.         let o = self.offset;
  28.         self.inner.extend(iter.into_iter().map(|c| {
  29.             c + o
  30.         }));
  31.     }
  32. }
  33.  
  34. fn main() {
  35.     let mut v = vec![];
  36.     use_that(&mut v);
  37.     assert_eq!(vec![2,3,4,2,3,4], v);
  38. }
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top