These are chat archives for rust-lang/rust

30th
Sep 2017
Walter Chang
@weihsiu
Sep 30 2017 05:42
i have a question regarding lifetime parameter. i have code like the following, when i compile it, the compiler tells me "bs doesn't live long enough". i need the Deserialize<'a> constraint on T for the retrieve() function. how could i make this work?
fn retrieve<'a, T: Deserialize<'a>>(&self, key: &str) -> Result<T, String> {
    let bs = ...; //retrieve a Vec<u8> somewhere
    Ok(serde_json::from_slice(&bs).unwrap())
}
CrLF0710
@crlf0710
Sep 30 2017 06:26
@weihsiu you'll write it this way.
fn retrieve<'a, 'b, T: Deserialize<'a>>(&'b self, key: &str) -> Result<T, String> where 'b : 'a {
    let bs_ref = &self.bs; //retrieve a Vec<u8> somewhere
    Ok(serde_json::from_slice(bs_ref).unwrap())
}
i'm assuming you'll get the bs reference from self somehow.
Walter Chang
@weihsiu
Sep 30 2017 06:56
@crlf0710 nope, it still tells me the part after "&"(in the above case, "self.bs") does not live long enough.
Walter Chang
@weihsiu
Sep 30 2017 07:05

oh, i am sorry, the bs does not come directly from self, we can look at it as created on the stack:

 let bs_ref = &vec![1, 2, 3];

the compiler tells me "vec![1, 2, 3]" does not live long enough.

xhallix
@xhallix
Sep 30 2017 07:31

Hello
I'd like to read data from a tcp stream (there is a mysql db behind)
here is what I'm doing

    let tcp = TcpBuilder::new_v4().unwrap();
    let mut stream = tcp.connect("127.0.0.1:3306").unwrap();
    let mut buf = Vec::with_capacity(74);
    buf.reserve(74);
    stream.read_exact(&mut buf);    
    println!("{:?}", buf);

The buf is empty in this case. But when I use stream.read_to_end it takes (a long) time and I get the buffer filled.

Why does read exact not fill my buf in this case?

unreadable
@unreadable
Sep 30 2017 09:52
what's the hype with that non lexical lifetime rfc commit?
Denis Lisov
@tanriol
Sep 30 2017 09:59
NLL will make borrowing behave much more intuitive. The compiler will accept some obviously correct constructions that it rejects now.
unreadable
@unreadable
Sep 30 2017 10:22
sounds great!
Andrey Lesnikov
@ozkriff
Sep 30 2017 10:24
is there a live stream of http://zurich.rustfest.eu ? :(
Anton Ugryumov
@dvec
Sep 30 2017 10:28
Hi there! I'm trying to compile rust program for my Raspberry Pi 3. But cargo prints this. What's the matter of this problem? ~/.cargo/config - https://pastebin.com/xidwekAX, arm-linux-gnueabihf-gcc is installed. OS - Arch Linux
Lukas Kalbertodt
@LukasKalbertodt
Sep 30 2017 10:30
@xhallix reserve after with_capacity() is not doing what you expect. But neither of those functions actually creates elements in the vector. So &mut buf is an empty slice! read_exact() takes a slice and not a vector as argument, so read_exact cannot push elements, just write over the existing buffer. But since the buffer is empty, it can't write any elements. Try let mut buf = vec![0; 74]; instead.
xhallix
@xhallix
Sep 30 2017 10:46
@LukasKalbertodt yep - but it is still very slow
Moggers
@Moggers
Sep 30 2017 11:11
Are there any implementations of graphplan or similar in rust?
Lukas Kalbertodt
@LukasKalbertodt
Sep 30 2017 11:17
@dvec You need to install the standard library for each target you want to compile to. This can be done easily with rustup: rustup target add arm-unknown-linux-gnueabihf. Please ping me if that helped or if you need more help:)
Anton Ugryumov
@dvec
Sep 30 2017 11:21
@LukasKalbertodt thanks for your answer. I solved the problem by reinstalling rustup
Lukas Kalbertodt
@LukasKalbertodt
Sep 30 2017 11:22
@dvec :+1:
Liel Fridman
@lielfr
Sep 30 2017 19:19
Hi! I'm trying to implement a Drop trait for a module which runs in a thread but it doesn't seem to work. Is there a different trait for a condition like this? or maybe something else? Thanks!
Michal 'vorner' Vaner
@vorner
Sep 30 2017 19:25
@lielfr
For a module?
Can you share your code and what it does or doesn't?
Liel Fridman
@lielfr
Sep 30 2017 19:26
@vorner Yes but it's too long. Is the calling code enough?
Michal 'vorner' Vaner
@vorner
Sep 30 2017 19:26
How can I know that before I see it? :innocent:
It's purpose is monitoring my internal network and possibly blocking clients which use too much traffic
My router can probably do that but I also want to learn a bit about raw sockets and rust
Michal 'vorner' Vaner
@vorner
Sep 30 2017 19:29
I see no drop anywhere. I don't know what you want to drop, why, and so on. And what do you mean by a module?
Liel Fridman
@lielfr
Sep 30 2017 19:31
@vorner I use an internal module in lines 19-20. I uploaded its code as well: https://gist.github.com/lielfr/8a5076921f9d74242919cdbcd1dd8515
It's not the cleanest and most idiomatic, I suppose :\
Michal 'vorner' Vaner
@vorner
Sep 30 2017 19:34
I think I can guess what the problem is. As the ARP spoofer is written, the run function never ends on its own, right? That one just runs forever.
The drop would be run after run terminated.
Liel Fridman
@lielfr
Sep 30 2017 19:35
Is there a way to run some code after terminating with Ctrl+C?
Michal 'vorner' Vaner
@vorner
Sep 30 2017 19:35
You can register the signal handler and do something when it is run. Like set some global atomic bool that everything checks in each iteration and then voluntarily terminates or something.
Liel Fridman
@lielfr
Sep 30 2017 19:36
@vorner thanks :)
Michal 'vorner' Vaner
@vorner
Sep 30 2017 19:36
But the default Ctrl+C just murders the whole program without much care.