These are chat archives for rust-lang/rust

4th
Nov 2017
Kan-Ru Chen
@kanru
Nov 04 2017 01:10
Hi, question about Future and Stream
I have a infinite Stream and I want to collect items from it. Can I terminate the Stream with a time out so it will finish eventually?
Michal 'vorner' Vaner
@vorner
Nov 04 2017 07:15
@kanru You create a second stream (for example tokio::core::Timeout::new(…).unwrap().into_stream()), map one of them to Some(item), the other to None, select them together and then use something like take_until.
Kan-Ru Chen
@kanru
Nov 04 2017 08:52
@vorner yeah, I was looking for something like take_until. It looks like take_while will do.
Kan-Ru Chen
@kanru
Nov 04 2017 09:21
ok, this works
        let responses = framed.send(ProtoVer(PROTOCOL_VERSION)).and_then(move |s| {
            s.map(|response| Some(response))
                .select(timeout.into_stream().map(|_| None))
                .take_while(|item| future::ok(item.is_some()))
                .map(|response| response.unwrap())
                .collect()
        });
David Harvey-Macaulay
@alteous
Nov 04 2017 11:22
Is there an easier way to take 3 items from an Iterator at a time than this?
let mut iter = ...;
while let (Some(x), Some(y), Some(z)) = (iter.next(), iter.next(), iter.next()) {
    ...
}
Aleksey Kladov
@matklad
Nov 04 2017 11:23
@alteous yep! next_tuple() from itertools
David Harvey-Macaulay
@alteous
Nov 04 2017 11:26
@matklad Perfect, thanks!
while let Some((a, b, c)) = iter.next_tuple() {
    faces.push([a, b, c]);
}
Aleksey Kladov
@matklad
Nov 04 2017 11:31
@alteous just learned about it a week ago myself! It's just super neat =P
Michal 'vorner' Vaner
@vorner
Nov 04 2017 11:33
@alteous You may even want something like faces.extend(iter.tuples()). (I haven't looked up the name of the method). Like, instead of doing the iteration yourself.
David Harvey-Macaulay
@alteous
Nov 04 2017 11:42
@vorner Better still!
if let Some(iter) = primitive.indices_u32(buffers) {
     faces.extend(iter.tuples().map(|(a, b, c)| [a, b, c]));
}
Ryan
@rnleach
Nov 04 2017 17:01
I have a function
`` fn op_on_iter<I: Iterator<Item=i32>>(iter: I){ .... } ''' When I pass a small array[i32; 2].iter()` in, it complains about looking for a reference but getting a value. When I pass in a long chain of iterators, it works fine.
Aleksey Kladov
@matklad
Nov 04 2017 17:02
[i32;2].inter is Iterator<Item=&i32>, and not Iterator<Item=&32>. Try [i32; 2].iter().cloned().
Ryan
@rnleach
Nov 04 2017 17:04
Wow, I really thought I tried that and it didn't work, but I tried again and it did. Thanks.