These are chat archives for rust-lang/rust

17th
Dec 2016
Matanel Levi
@matanelevi
Dec 17 2016 00:31
Is it possible to have a single rs file who is shared between multiple crates?
Let's say a server & client are separated crates, but they share some protocol information who lives in another .rs file - there is a way to do this?
Alexander Ronald Altman
@pthariensflame
Dec 17 2016 00:45
Use the path attribute on import?
Matanel Levi
@matanelevi
Dec 17 2016 01:07
@pthariensflame what do you mean?
Alexander Ronald Altman
@pthariensflame
Dec 17 2016 01:13

Like, put

#[path = "path/to/shared.rs"]
use shared;

in both client.rs and server.rs.

Matanel Levi
@matanelevi
Dec 17 2016 01:24
Oh, nice!
@pthariensflame thanks man!
Matanel Levi
@matanelevi
Dec 17 2016 02:32
There is a solution for rust-lang/rust#12249
?
Matanel Levi
@matanelevi
Dec 17 2016 14:43

Let's say I have this files hierarchy:
module: generators.rs mod.rs

generators.rs looks like:

#[macro_export]
macro_rules! my_macro {
    blablabla
}

How do I use my_macro in mod.rs?

Nevermind, I got it
David McGillicuddy
@djmcgill
Dec 17 2016 18:42
I've started on my underhanded rust submission, and I'm getting weird behaviour that I'm not sure is due to my mutex use, or the http library I'm using.

If I call the endpoints as shown:

$ curl -X POST localhost:8000/user
added new user
$ curl localhost:8000/user/0
gotten user

Then the server console output is:

Now listening on localhost:8000
received "POST" "/user"
chosen_user to return is: Some(Account { id: 0, user_name: "user_1", password: "pass_1" })
responding with status 200
received "GET" "/user/0"
chosen_user to return is: None
responding with status 200
i.e. the state of my mutex is somehow reset between the two calls. As far as I can see it's used the same as the session data mutex in the example: https://github.com/tomaka/rouille/blob/master/examples/login-session.rs
David McGillicuddy
@djmcgill
Dec 17 2016 19:09
The type isn't Copy, so I have no idea how I could make an operation on it that's then lost.
Matanel Levi
@matanelevi
Dec 17 2016 19:44

Why is it impossible for Rust to infer the size of static array?

let arr = [0u32, 0, 0, 0, 0]

The above must be written as:

let arr: [u32; 5] = [0u32, 0, 0, 0, 0]
Matanel Levi
@matanelevi
Dec 17 2016 20:32
type SomeFunc = fn(&mut TcpStream) -> ExerciseResult;

let mut stream = TcpStream::connect("127.0.0.1:6567").unwrap();

let exercises: Vec<(&'static str, ExerciseFunc)> =
    vec![("hello_rust", exercises::exercises_impls::ex1),
              ("fib", exercises::exercises_impls::ex2)];

for &(name, ex_func) in &exercises { ... }
Why the & is required before (name, ex_func)?

If I remove it, I get this error:
`= note: expected type &(&str, fn(&mut std::net::TcpStream) -> exercises::exercise_result::ExerciseResult)

= note: found type ( , ) `

Tibor Benke
@ihrwein
Dec 17 2016 20:37
@matanelevi I believe if you iterate over something without moving out the items (&exercises) you get references to the items (otherwise you would move the values). The for &(name, ex_func) syntax shows that you get references.
Matanel Levi
@matanelevi
Dec 17 2016 20:38

@ihrwein so what is the difference here:

let nums = vec![1, 2, 3];

for num in &nums {
    println!("{}", num);
}

The above works

David McGillicuddy
@djmcgill
Dec 17 2016 20:38
Oh I figured out the solution to my problem - I was creating the mutex for each request.
Tibor Benke
@ihrwein
Dec 17 2016 20:39
@matanelevi integers are Copy types. Could you try your example with a vector of Strings?
Matanel Levi
@matanelevi
Dec 17 2016 20:40
let nums = vec!["dd".to_string(), "cc".to_string()];

    for num in &nums {
        println!("{}", num);
    }
@ihrwein this compiles
@ihrwein I'm prettry sure that &nums creates an iterator, so each element in it is a reference already
Tibor Benke
@ihrwein
Dec 17 2016 20:44
You are right. I don't know then :(
Alexander Ronald Altman
@pthariensflame
Dec 17 2016 21:29
@matanelevi nums is of type &String (or &i32 in the original example), both of which implement Display. You should be able to pattern match on them with & like any other reference.
You have to for the tulle case, because you're pattern matching anyway and you need access to the structure under the reference.
s/tulle/tuple/
Matanel Levi
@matanelevi
Dec 17 2016 21:31
type SomeFunc = fn(&mut TcpStream) -> ExerciseResult;

let mut stream = TcpStream::connect("127.0.0.1:6567").unwrap();

let exercises: Vec<(&'static str, ExerciseFunc)> =
    vec![("hello_rust", exercises::exercises_impls::ex1),
              ("fib", exercises::exercises_impls::ex2)];

for &(name, ex_func) in &exercises { ... }
Why the & is required before (name, ex_func)?
@pthariensflame I don't understand this
Alexander Ronald Altman
@pthariensflame
Dec 17 2016 21:32
Because it's always required when pattern matching on a reference.
Matanel Levi
@matanelevi
Dec 17 2016 21:32
Ohhh
I get you now
Alexander Ronald Altman
@pthariensflame
Dec 17 2016 21:32
You can get away with not using it when you don't pattern match at all here because printing works on references.
:)
Matanel Levi
@matanelevi
Dec 17 2016 21:34
I think I do ^_^
@pthariensflame thank you :)
Matanel Levi
@matanelevi
Dec 17 2016 23:10

Is is possible to do a #[cfg(feature = "x")] on multiple statements?
Like this:

#[cfg(feature = "x")]
use std::io::{Read, Write};
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};

So it will affect the both uses?

Peter Atashian
@retep998
Dec 17 2016 23:21
@matanelevi not like that no
Matanel Levi
@matanelevi
Dec 17 2016 23:21
@retep998 But this functionality exists?
Peter Atashian
@retep998
Dec 17 2016 23:47
@matanelevi Technically you can wrap it in a macro invocation that just forwards on items and then put a #[cfg] on the macro invocation
Matanel Levi
@matanelevi
Dec 17 2016 23:48
Well, there is no point then..