These are chat archives for rust-lang/rust

24th
Jun 2018
Robert
@rw
Jun 24 2018 00:51
@jamessan thank you!
Nicholas
@Bujiraso
Jun 24 2018 01:06
That's a great article.
Kelly Thomas Kline
@kellytk
Jun 24 2018 04:06
If you were to implement a websocket server now would you use https://github.com/housleyjk/ws-rs, https://github.com/snapview/tungstenite-rs, or something else?
Talden
@Talden
Jun 24 2018 04:53
I have a Rocket app I want to add websockets to. I'd love to see an example accepting and upgrading a websocket request. I've found questions on the same topic but no answers.
Kelly Thomas Kline
@kellytk
Jun 24 2018 05:04
actix web is another option for websocket servers
Michal 'vorner' Vaner
@vorner
Jun 24 2018 07:40
Hello. I want to store a callback as a Box<Fn()>. But having to call the function to set it as Stuff::new(Box::new(move || { ... })) is annoying. I could implement a trait that would pass Box<Fn()> as is and box an unboxed closure. But is there something ready-made? I tried playing with Into<Box<Fn()>>, several Borrow and AndRef attempts, but didn't come up with anything.
Yuji Kanagawa
@kngwyu
Jun 24 2018 07:53
@vorner
It's not exactly you want, but I think FnBox would help you.
Ah but if you want Fn it's completely unrelated, sorry.
trsh
@trsh
Jun 24 2018 08:16
Hi!
Im a slow internet and getting these

Downloading void v1.0.2 warning: spurious network error (2 tries remaining): failed to get 200 response fromhttps://crates.io/api/v1/crates/void/1.0.2/download`, got 500
warning: spurious network error (1 tries remaining): [28] Timeout was reached (Operation too slow. Less than 10 bytes/sec transferred the last 30 seconds)
error: unable to get packages from source

Caused by:
[28] Timeout was reached (Operation too slow. Less than 10 bytes/sec transferred the last 30 seconds)`

How do I increase timeout?
Michal 'vorner' Vaner
@vorner
Jun 24 2018 08:19
I think this is not caused by your connection. Crates.io throws 500s at me today as well, but it sometimes succeeds.
trsh
@trsh
Jun 24 2018 08:24
Mkay
Ehh.. im getting those all over
Downloading kernel32-sys v0.2.2
warning: spurious network error (2 tries remaining): failed to get 200 response from https://crates.io/api/v1/crates/kernel32-sys/0.2.2/download, got 500
warning: spurious network error (1 tries remaining): failed to get 200 response from https://crates.io/api/v1/crates/kernel32-sys/0.2.2/download, got 500
error: unable to get packages from source
Michal 'vorner' Vaner
@vorner
Jun 24 2018 08:28
Yes. It seems ill today. I did get my downloads in like 3 attempts, though.
trsh
@trsh
Jun 24 2018 08:28
Where can we report?
cargo is panicing
Michal 'vorner' Vaner
@vorner
Jun 24 2018 08:36
Ugly-looking panic from cargo probably here: https://github.com/rust-lang/cargo/. The crates.io thing ‒ not sure, but I think there's the infrastructure team IRC channel somewhere.
Michal 'vorner' Vaner
@vorner
Jun 24 2018 08:48
I'm not sure… if there's no better place. But this sounds more like operational issue than code problem.
trsh
@trsh
Jun 24 2018 08:50
RED LIGHTS! ALARM!
We need to special forces to fixit
Maik Klein
@MaikKlein
Jun 24 2018 08:59
I have the same problem
trsh
@trsh
Jun 24 2018 09:06
@MaikKlein FIX IT! :D
trsh
@trsh
Jun 24 2018 09:12
"note: please ensure that VS 2013 or VS 2015 was installed with the Visual C++ option"
How about 2017? Any exp?
Dylan DPC
@Dylan-DPC
Jun 24 2018 09:44
@vorner it has already been reported to the rust-infra team
trsh
@trsh
Jun 24 2018 10:08
Cool!
Anybody using Rust in windows with VS 2017?
For stable
ok never mind
JasonKleban
@JasonKleban
Jun 24 2018 11:36
I'm on windows, I think I've successfully compiled the libcore for arm-none-eabi as liblib.a from my rust nightly source (with the stdsimd submodule also cloned). Trying to cross-compile for raspberry pi 3 b (~following https://medium.com/@thiagopnts/raspberry-pi-bare-metal-programming-with-rust-a6f145e84024) but now how can I USE the liblib.a when compiling my kernel.rs? Do I need to register that somewhere?
JasonKleban
@JasonKleban
Jun 24 2018 12:43
I tried xargo too, which succeeds in saving something in the targets folder, but I don't know how to get it to make kernel.o for further processing, and I don't like the obfuscation (as I currently feel it) of xargo - if I have liblib.a and all I need to do is hook that up to get back on track, I'd prefer that way
tsoernes
@tsoernes
Jun 24 2018 15:34
Say you have a program that uses the first 100 fibonacci numbers everywhere. You don't want to be generating them every time you need them, although generating them once is inexpensive enough so that you don't want to bother saving/loading them from file. How can you generate these numbers once at program startup and make them (easily) available anywhere in the program?
Michal 'vorner' Vaner
@vorner
Jun 24 2018 15:35
@tsoernes lazy-static would probably work and generate them on the first use. Or you can generate them as part of your build.rs during compile time and compile them in as a constant.
Denis Lisov
@tanriol
Jun 24 2018 15:35
If doing that on first access is acceptable, look at lazy_static
tsoernes
@tsoernes
Jun 24 2018 15:36
that looks fine. Thank you
tsoernes
@tsoernes
Jun 24 2018 15:53
any idea how to make lazy_static work in this scenario?
lazy_static! {
    static ref (NEIGHS1, NEIGHS2, NEIGHS3, NEIGHS4, N_NEIGHS) = generate_neighs();
}
main.rs 17 16 error no rules expected the token((rust-cargo)
Michal 'vorner' Vaner
@vorner
Jun 24 2018 15:54
Can you return an array or vec instead?
Also, you need to give it a type (I think)
tsoernes
@tsoernes
Jun 24 2018 15:54
no, they are different types
Michal 'vorner' Vaner
@vorner
Jun 24 2018 15:55
Then you probably want to do something like static ref NEIGHS: (Type1, Type2, …) = generate_neighs();. Because the lazy_static plays with the type a bit and I fear it doesn't know how to unpack the tuple.
tsoernes
@tsoernes
Jun 24 2018 16:24

Given the ndarrays inuse and grid, why does the first bitwise_or work but not the second?

fn eligible_map(grid: &Grid, cell: &Cell) -> Array<bool, Ix1> {
    // Channels that are not in use at cell or its neighbors with distance of 2 or less
    let mut inuse = inuse_neighs(grid, cell);
    inuse = inuse.bitor(grid.slice(s![cell.row, cell.col, ..]));
    inuse.bitor_assign(grid.slice(s![cell.row, cell.col, ..]));
    return inuse.not()
}

(the latter gives a 10 line error message about type mismatch)

 main.rs   127  11 error    E0277  the trait bound `ndarray::ViewRepr<&bool>: ndarray::DataMut` is not satisfied (the trait `ndarray::DataMut` is not implemented for `ndarray::ViewRepr<&bool>`) (rust-cargo)
 main.rs   127  11 info     E0277  the following implementations were found:
   <ndarray::ViewRepr<&'a mut A> as ndarray::DataMut> (rust-cargo)
 main.rs   127  11 info     E0277  required because of the requirements on the impl of `std::ops::BitOrAssign` for `ndarray::ArrayBase<ndarray::ViewRepr<&bool>, ndarray::Dim<[usize; 1]>>` (rust-cargo)
 main.rs   127  11 info     E0277  required because of the requirements on the impl of `std::ops::BitOrAssign<ndarray::ArrayBase<ndarray::ViewRepr<&bool>, ndarray::Dim<[usize; 1]>>>` for `ndarray::ArrayBase<ndarray::OwnedRepr<bool>, ndarray::Dim<[usize; 1]>>` (rust-cargo)
 main.rs   127  11 error    E0271  type mismatch resolving `<ndarray::ViewRepr<&bool> as ndarray::Data>::Elem == ndarray::ArrayBase<ndarray::ViewRepr<&bool>, ndarray::Dim<[usize; 1]>>` (expected bool, found struct `ndarray::ArrayBase`) (rust-cargo)
 main.rs   127  11 info     E0271  expected type `bool`
    found type `ndarray::ArrayBase<ndarray::ViewRepr<&bool>, ndarray::Dim<[usize; 1]>>` (rust-cargo)
 main.rs   127  11 info     E0271  required because of the requirements on the impl of `std::ops::BitOrAssign` for `ndarray::ArrayBase<ndarray::ViewRepr<&bool>, ndarray::Dim<[usize; 1]>>` (rust-cargo)
 main.rs   127  11 info     E0271  required because of the requirements on the impl of `std::ops::BitOrAssign<ndarray::ArrayBase<ndarray::ViewRepr<&bool>, ndarray::Dim<[usize; 1]>>>` for `ndarray::ArrayBase<ndarray::OwnedRepr<bool>, ndarray::Dim<[usize; 1]>>` (rust-cargo)
 main.rs   127  11 error    E0277  the trait bound `ndarray::ArrayBase<ndarray::ViewRepr<&bool>, ndarray::Dim<[usize; 1]>>: ndarray::ScalarOperand` is not satisfied (the trait `ndarray::ScalarOperand` is not implemented for `ndarray::ArrayBase<ndarray::ViewRepr<&bool>, ndarray::Dim<[usize; 1]>>`) (rust-cargo)
 main.rs   127  11 info     E0277  required because of the requirements on the impl of `std::ops::BitOrAssign<ndarray::ArrayBase<ndarray::ViewRepr<&bool>, ndarray::Dim<[usize; 1]>>>` for `ndarray::ArrayBase<ndarray::OwnedRepr<bool>, ndarray::Dim<[usize; 1]>>` (rust-cargo)
 main.rs   127  11 error    E0271  type mismatch resolving `<ndarray::OwnedRepr<bool> as ndarray::Data>::Elem == ndarray::ArrayBase<ndarray::ViewRepr<&bool>, ndarray::Dim<[usize; 1]>>` (expected bool, found struct `ndarray::ArrayBase`) (rust-cargo)
 main.rs   127  11 info     E0271  expected type `bool`
    found type `ndarray::ArrayBase<ndarray::ViewRepr<&bool>, ndarray::Dim<[usize; 1]>>` (rust-cargo)
 main.rs   127  11 info     E0271  required because of the requirements on the impl of `std::ops::BitOrAssign<ndarray::ArrayBase<ndarray::ViewRepr<&bool>, ndarray::Dim<[usize; 1]>>>` for `ndarray::ArrayBase<ndarray::OwnedRepr<bool>, ndarray::Dim<[usize; 1]>>` (rust-cargo)
am I misunderstanding the use of bitor_assign vs bitor?
Denis Lisov
@tanriol
Jun 24 2018 17:02
@tsoernes Looks like the ndarray bitor_assign takes the argument by reference... have you tried adding a &?
tsoernes
@tsoernes
Jun 24 2018 17:22
@tanriol Yes that compiles. Was that obvious from the error message somehow?
I should have read the docs more closely, obviously, but without the docs could you have figured it out from the errors?
Denis Lisov
@tanriol
Jun 24 2018 17:29
Certainly not obvious, took some minutes before I noticed this fine difference in the documentation.
tsoernes
@tsoernes
Jun 24 2018 21:41

I'm a bit confused by the examples from the lazy-static package:

lazy_static! {
    static ref HASHMAP: HashMap<u32, &'static str> = {
        let mut m = HashMap::new();
        m.insert(0, "foo");
        m
    };
}

The hashmap is populated with "foo" which is a str, so why is the type declared as a reference, i.e. &'static str?
I'm getting borrowing errors in my own code, where lazy_static is used as such:

    static ref NEIGHS: (Array<usize, Ix4>, Array<usize, Ix4>,
                        Array<usize, Ix4>, Array<usize, Ix3>) = generate_neighs();

Should the arrays be declared with &'static somehow?

Denis Lisov
@tanriol
Jun 24 2018 21:43
The "foo" string lives somewhere in the data segment, only a reference to it is stored in the HashMap in the example.
What are the errors you're getting?
Also, what's the signature of generate_neighs?
tsoernes
@tsoernes
Jun 24 2018 21:46
fn generate_neighs() -> (Array<usize, Ix4>, Array<usize, Ix4>,
                        Array<usize, Ix4>, Array<usize, Ix3>)
Errors are in comment above where they happen:
// borrowed value must be valid for the anonymous lifetime #1 defined on the function body
fn neighbors_sep(dist: usize, cell: &Cell, include_self: bool) -> ArrayView<usize, Ix2> {
    // [[row1, row2, .., rowN], [col1, col2, .., colN]] for N neighbors
    // cannot move out of static item (cannot move out of static item) (rust-cargo)
    // using a reference instead: `&NEIGHS` (rust-cargo)
    let k = NEIGHS;
    let allneighs = match dist {
        //  cannot move out of borrowed content (cannot move out of borrowed content) (rust-cargo)
        1 => k.0,
        2 => k.1,
        4 => k.2,
        _ => panic!("Neighbors for distances other than 1, 2 or 4 should never be needed")
    };

    let j = &NEIGHS;
    let allneighs = match dist {
        //  cannot move out of borrowed content (cannot move out of borrowed content) (rust-cargo)
        1 => j.0,
        2 => j.1,
        4 => j.2,
        _ => panic!("Neighbors for distances other than 1, 2 or 4 should never be needed")
    };

    let start = if include_self { 0 } else { 1 };
    let end = NEIGHS.3[[dist - 1, cell.row, cell.col]];

    //  `allneighs` does not live long enough (borrowed value does not live long enough) (rust-cargo)
    return allneighs.slice(s![cell.row, cell.col, start..end, ..]).t()
}
Denis Lisov
@tanriol
Jun 24 2018 22:01
Several problems in here.
First, your signature is likely wrong (do you really want the ArrayView to live no longer than the Cell reference passed as argument?)
The .t() in the end won't work as it creates a view into temporary, use .reversed_axes() instead.
k.0 and others like it should probably be &k.0
And let k = &NEIGHS;
tsoernes
@tsoernes
Jun 24 2018 22:11

this compiles, and gives no errors/warnings:

fn neighbors_sep(dist: usize, cell: &Cell, include_self: bool) -> ArrayView<usize, Ix2> {
    // [[row1, row2, .., rowN], [col1, col2, .., colN]] for N neighbors
    let j = &NEIGHS;
    let allneighs = match dist {
        1 => &j.0,
        2 => &j.1,
        4 => &j.2,
        _ => panic!("Neighbors for distances other than 1, 2 or 4 should never be needed")
    };
    let start = if include_self { 0 } else { 1 };
    let end = NEIGHS.3[[dist - 1, cell.row, cell.col]];
    return allneighs.slice(s![cell.row, cell.col, start..end, ..]).reversed_axes()
}

You're saying the arrayview will go out of scope once the Cell goes out of scope (cell.row is usize)?

Denis Lisov
@tanriol
Jun 24 2018 22:13
Once the cell reference dies. If you want it to live indefinitely, you may want to specify the return type as ArrayView<'static, usize, Ix2>
tsoernes
@tsoernes
Jun 24 2018 22:15
One more thing, why should j = &NEIGHS but end = NEIGHS.3? Does not j = &NEIGHS create a reference to what is already a reference?
given that the lazy_static's are declared static ref ..
Denis Lisov
@tanriol
Jun 24 2018 22:18
The lazy_static item is actually something that derefs to the underlying data.
You need something to trigger the dereference, for example attribute access.