These are chat archives for rust-lang/rust

6th
Dec 2018
Florian Beeres
@cideM
Dec 06 2018 01:20

I just had the idea of being really fancy and using threads to speed up advent of code day 5. Anyway... I failed to do it with vanilla threads but I really want to as a learning exercise. I have a function fn(s: &str) -> Result<String, Error>. Internally, that function calls a variable number of functions which process s. They should all run in separate threads. What concurrency model would I choose here? I'm a Rust noob and the book explains both channels and shared state. But obviously all exercises use string literals...

I guess I to_string() the &str when passing it into the threads and then I maybe put a HashMap in an Arc and share that with all threads so they can push their results to that HashMap? Does that make any sense? At the end I call the join handles, get my results from the map and call it a day?

Chikirev Sirguy
@cheblin
Dec 06 2018 02:20

stuck implementing Iterator for Items. please help help with lifetimes

#[derive(Copy, Clone)]
struct Cursor { val: i32 }

struct Items<'a> { cur: &'a mut Cursor, max_dim0: usize }

impl<'a> Items<'a> {
    fn item(&mut self, d0: usize) -> Option<Values> {
        Some(Values {
            cur: self.cur,
            origin: 0,
        })
    }
}
//wwwwwwwwwwwwwwwwwwwwwwwwwww
impl<'a> Iterator for Items<'a> {
    type Item = Values<'a>;

    fn next(&mut self) -> Option<Values<'a>> {
        Some(Values::<'a> {
            cur: self.cur,
            origin: 0,
        })
    }
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
struct Values<'a> { cur: &'a mut Cursor, origin: usize }

fn main() {
    let mut cur = Cursor { val: 0 };
    let mut item = Items { cur: &mut cur, max_dim0: 0 };

    item.cur.val = 56;
    {
        let val = item.item(56);
        val.unwrap().cur.val = 89;
    }
    item.cur.val = 56999;
}

lifetime of reference outlives lifetime of borrowed content...
| cur: self.cur,
| ^^^^^^^^
note: ...the reference is valid for the lifetime 'a as defined on the impl
| impl<'a> Iterator for Items<'a> {
| ^^
note: ...but the borrowed content is only valid for the anonymous lifetime '#1' defined on the
|
| fn next(&mut self) -> Option<Values<'a>> { | _____^ | | Some(Values::<'a> {
| | cur: self.cur,
| | origin: 0,
| | })
| | }
| |_^

Tim Robinson
@1tgr
Dec 06 2018 07:07
fn next(&mut self) -> Option<Values<'a>> {
Don't forget to make this &'a mut self
Chikirev Sirguy
@cheblin
Dec 06 2018 07:10
Thank you, but it does not help or... it was kidding?
Tim Robinson
@1tgr
Dec 06 2018 07:12
self needs to at least outlive 'a, but I don't know if that was the only problem
Can you edit your message to put the compiler error in a fixed with font? same as you did with the code
Tim Robinson
@1tgr
Dec 06 2018 07:15
Cool I like playground links
Chikirev Sirguy
@cheblin
Dec 06 2018 07:17
if you comment Iterator implementation, everything become just fine
Tim Robinson
@1tgr
Dec 06 2018 07:17
Switching to nightly and adding #![feature(nll)] gives better error messages
I think it still wants 'a to outlive the lifetime of &mut self
Chikirev Sirguy
@cheblin
Dec 06 2018 07:18
wow. nice idea
Tim Robinson
@1tgr
Dec 06 2018 07:18
But I can't manipulate the lifetime on the &mut self reference because it's part of the Iterator trait
error: unsatisfied lifetime constraints
  --> src/main.rs:22:18
   |
17 | impl<'a> Iterator for Items<'a> {
   |      -- lifetime `'a` defined here
...
20 |     fn next(&mut self) -> Option<Values<'a>> {
   |             - let's call the lifetime of this reference `'1`
21 |         Some(Values::<'a> {
22 |             cur: self.cur,
   |                  ^^^^^^^^ requires that `'1` must outlive `'a`
Chikirev Sirguy
@cheblin
Dec 06 2018 07:19
right! found the same
Tim Robinson
@1tgr
Dec 06 2018 07:31
Normally with fn next<'b: 'a>(&'b mut self), but I can’t add constraints to next as it’s on a trait
Chikirev Sirguy
@cheblin
Dec 06 2018 07:34
Is it bug or feature ? ;)
Tim Robinson
@1tgr
Dec 06 2018 07:37
Ok
Your must return mutable references with a shorter lifetime than the iterator itself
Otherwise you would have two mutable references to the same thing
Chikirev Sirguy
@cheblin
Dec 06 2018 07:39
thanks . I am studying
Tim Robinson
@1tgr
Dec 06 2018 07:40
I wonder if you need &'b &'a mut Cursor
I’m not at a PC right now
Chikirev Sirguy
@cheblin
Dec 06 2018 07:45
thanks guys for your help. I need some time to elaborate stuff
Paul Masurel
@fulmicoton
Dec 06 2018 09:35
that's a common problem with Iterator.
Ayush Prashar
@ayushprashar
Dec 06 2018 10:27
hey guys... I was working with CDRS which is a rust driver for Cassandra DB. In it there's a trait which converts self to cdrs::types::value::Value. This Value happens to be Cassandra value which could be an array of bytes, null and non-set values. This trait hasn't been implemented to work with an Enumeration, which is my requirement and I'm kind of lost how to proceed with that. Can anyone suggest some help?
octave99
@octave99
Dec 06 2018 10:57
Hi, I am resolving a future from the main using .wait and its resulting into this error:
thread 'main' has overflowed its stack
fatal runtime error: stack overflow
Aborted
The future however resolves, and am able to see the result using print! in .map(...
any inputs?
octave99
@octave99
Dec 06 2018 16:08
I have further checked and found that its happening in the poll due to
try_ready!(self.io.poll_read_ready(mio::Ready::readable()));
If I comment it out, there is no panic, and at the same time, no polling happens and event loop rests in peace
André E. Veltstra
@aev-mambro2
Dec 06 2018 16:23
@polygdev sounds like an infinite recursion problem.
octave99
@octave99
Dec 06 2018 16:25
@aev-mambro2 Any tips to troubleshoot, code is same as here with tokio udp implementation: https://tokio-rs.github.io/tokio/src/tokio_udp/recv_dgram.rs.html#84
Only difference is that I am trying to do it on RawSocket which has been registered from Tokio to mio with PollEvented2 and Evented has been implemented for mio
André E. Veltstra
@aev-mambro2
Dec 06 2018 16:42
@polygdev Sorry, I'm quite new to the rust language. I would check whether mio::Ready and self.io.poll_read_ready overlap or call each other, causing infinite recursion.
octave99
@octave99
Dec 06 2018 16:50
@aev-mambro2 Sure. Thanks for your inputs.
octave99
@octave99
Dec 06 2018 16:59
Just in case someone wish to participate. Rest of the code appears to be working perfectly, when I change the poll method from udp example above to below, it print outs the received data accurately. But its not expected code for tokio/mio:
impl Future for Server {
    type Item = usize;
    type Error = std::io::Error;

    fn poll(&mut self) -> Poll<Self::Item, std::io::Error> {
        loop {
            let v = try_nb!(self.inner.recv(&mut self.buf));
            print!("{:?}", self.buf);
        }
    }
}
D'Silva
@evnix
Dec 06 2018 18:56

does anyone know how to use the bytes ( &[u8] ) from the following code

hasher.variable_result( |bytes| {
        bytes
    });

i want to do something like

let my_bytes = hasher.variable_result( |bytes| {
        (bytes)
    });
Ingvar Stepanyan
@RReverser
Dec 06 2018 19:54
depends on type of the closure really
but if it provides callback like this it usually means that bytes are alive only inside of that closure
so you'll have to clone

however you can try to do something like

let bytes = None;
hasher.variable_result(|bytes2| {
  bytes = Some(bytes2);
  bytes2
});

and see if compiler complains

D'Silva
@evnix
Dec 06 2018 20:55
yup that did it thanks!!
Kelly Thomas Kline
@kellytk
Dec 06 2018 21:35
Can a HashMap be printed as an opaque object? I'm debugging something and I need to ensure I'm using the same object across runs of a function