Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. trait Callback { type Output; fn call(self, arg: u8) -> Self::Output; }
  2.  
  3.  
  4. fn this<F>(mut f: F)
  5. where for<'a> &'a mut F: Callback,
  6. /*for<'a> <&'a mut F as Callback>::Output: Extend<u8>*/ {
  7. /*f.call(1).extend([1,2,3].iter().cloned());
  8. f.call(2).extend([1,2,3].iter().cloned())*/
  9. }
  10.  
  11.  
  12.  
  13. fn use_that<T: Extend<u8>>(t: &mut T) {
  14. struct Test<'a, T: Extend<u8>> {
  15. a: &'a mut T
  16. }
  17. impl<'a,'b, T: Extend<u8>> Callback for &'a mut Test<'b, T> {
  18. type Output = ExtendWithOffset<'a,T>;
  19. fn call(self, arg: u8) -> Self::Output {
  20. with_offset(self.a, arg)
  21. }
  22. }
  23. this(Test { a: t })
  24. }
  25.  
  26.  
  27. fn with_offset<T: Extend<u8>>(inner: &mut T, offset: u8) -> ExtendWithOffset<'_,T> {
  28. ExtendWithOffset { inner, offset }
  29. }
  30.  
  31. struct ExtendWithOffset<'a, T> {
  32. inner: &'a mut T,
  33. offset: u8
  34. }
  35.  
  36. impl<'a, T: Extend<u8>> Extend<u8> for ExtendWithOffset<'a, T> {
  37. fn extend<I: IntoIterator<Item=u8>>(&mut self, iter: I) {
  38. let o = self.offset;
  39. self.inner.extend(iter.into_iter().map(|c| {
  40. c + o
  41. }));
  42. }
  43. }
  44.  
  45. fn main() {
  46. let mut v = vec![];
  47. use_that(&mut v);
  48. assert_eq!(vec![2,3,4], v);
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement