Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trait Callback { type Output; fn call(self, arg: u8) -> Self::Output; }
- fn this<'b, T, F>(mut f: F)
- where T: Extend<u8> + 'b,
- for<'a> &'a mut F: Callback<Output=T>, {
- /*f.call(1).extend([1,2,3].iter().cloned());
- f.call(2).extend([1,2,3].iter().cloned())*/
- }
- fn use_that<T: Extend<u8>>(t: &mut T) {
- struct Test<'a, T: Extend<u8>> {
- a: &'a mut T
- }
- impl<'a,'b, T: Extend<u8>> Callback for &'a mut Test<'b, T> {
- type Output = ExtendWithOffset<'a,T>;
- fn call(self, arg: u8) -> Self::Output {
- with_offset(self.a, arg)
- }
- }
- this(Test { a: t })
- }
- fn with_offset<T: Extend<u8>>(inner: &mut T, offset: u8) -> ExtendWithOffset<'_,T> {
- ExtendWithOffset { inner, offset }
- }
- struct ExtendWithOffset<'a, T> {
- inner: &'a mut T,
- offset: u8
- }
- impl<'a, T: Extend<u8>> Extend<u8> for ExtendWithOffset<'a, T> {
- fn extend<I: IntoIterator<Item=u8>>(&mut self, iter: I) {
- let o = self.offset;
- self.inner.extend(iter.into_iter().map(|c| {
- c + o
- }));
- }
- }
- fn main() {
- let mut v = vec![];
- use_that(&mut v);
- assert_eq!(vec![2,3,4], v);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement