These are chat archives for rust-lang/rust

7th
Dec 2016
Jay Looney
@zenware
Dec 07 2016 00:55
My goodness, I took on perhaps too large of a project without knowing enough of rust to be prepared... thank goodness for good compiler errors/warnings
Jay Looney
@zenware
Dec 07 2016 03:24
If I implement a function on a struct which has the goal of modifying a property to a struct (which isn't possible), is it sufficient to output a new instance of the struct?
Jarred Nicholls
@jnicholls
Dec 07 2016 03:28
@zenware why can't the function take &smut self and modify the struct?
Jay Looney
@zenware
Dec 07 2016 03:35
@jnicholls I don't know I just started using rust and decided immediately that I'm going to build a relational algebra engine,
Jarred Nicholls
@jnicholls
Dec 07 2016 07:10
That's a decently hard problem to start with :)
xialvjun
@xialvjun
Dec 07 2016 13:45
@jnicholls thanks, I've chose the gnu version, and setup everything even the debug enviroment. Thanks for replying.
Jarred Nicholls
@jnicholls
Dec 07 2016 14:01
Excellent! Have fun and good luck.
salpalvv
@salpalvv
Dec 07 2016 16:04
So, i realize "fighting the borrow checker" is a normal growing pain. I am not sure what the best way of doing this is though...I want to read in a toml file as a server config, pass it to multiple tcp socket listeners that run on ports designated in the toml in different threads. If i move the ownership of the config into a thread, how do i read it in other threads? If it better to make it static if there are multiple threads that need to read it?
Aleksey Kladov
@matklad
Dec 07 2016 16:06
I would just copy the config, because it does not look like a very expensive operation.
To handle the ownership story super correct, I would look at crossbeam and scoped threads.
They would allow you to write code like
fn main() {
    let config = read_config();
    crossbeam::scope(|scope| {
        for _ in 0..config.n_threads {
          scope.spawn(|| { /* can read config here */ })
       }
    })
}
salpalvv
@salpalvv
Dec 07 2016 16:08
It isn't a mutable file. Just something for the app to read in. Would this still be necessary?
Aleksey Kladov
@matklad
Dec 07 2016 16:09
If you share a value across the threads, it's not only a question of mutability. You need to answer "who will free the memory" of the config.
Possible answers are
1) everybody gets his own config and cleans up after himself (.clone config for each thread)
2) the last thread to exit frees the memory (share Arc<Config> instead)
3) the supervisor thread which is guaranteed to exit last cleans the mess (crossbeam solution)
Does this help?
salpalvv
@salpalvv
Dec 07 2016 16:11
haha That was a perfect explanation.
I didn't even consider the freeing part.
salpalvv
@salpalvv
Dec 07 2016 16:17
Thank you for the detailed answer.
Magnus Andersson
@magnusart
Dec 07 2016 21:06
Hi, I have a n00b question. I have a project with both a main.rs and a lib.rs. I import the same crates in main.rs as I do in server.rs (which is a module). I get compile errors for server.rs saying for example: use futures::stream::Stream; Did you mean `self::futures::stream`?. What did I miss?
Magnus Andersson
@magnusart
Dec 07 2016 21:28
Can I only put extern crate in main.rs and lib.rs files?
salpalvv
@salpalvv
Dec 07 2016 21:41
Yeah
you add extern crates and mods in main.rs and use functions in modules
Jay Looney
@zenware
Dec 07 2016 21:46
@matklad What are the pros & cons of Arc vs crossbeam?
salpalvv
@salpalvv
Dec 07 2016 21:49
From what I asked a little earlier ago, I ended up using Arc because my configs are only accessed once to read the toml and stored in a struct. crossbeam looked more like sharing data structures actively and live.
If the toml is mutated live and configs are read often during runtime, i would look into crossbeam. That and if my toml gets massive, i wouldn't want to copy it for every thread as i am doing now.
Jay Looney
@zenware
Dec 07 2016 21:57
Are you planning on trapping a user defined signal or a hangup signal to reload the config at runtime? Idk what you're building but I always love it when I can keep stuff running but change config. like kill -s HUP $(cat /usr/local/nginx/logs/nginx.pid)
salpalvv
@salpalvv
Dec 07 2016 22:11
I wasn't...but that sounds awesome! It's a toy backend game server. I'm not sure how that would be done without disrupting services.
Jay Looney
@zenware
Dec 07 2016 23:36
@salpalvv I'm sure it's done with black magic, there's quite a few server daemons which work like that though so perhaps we could look at some open source code and find out