These are chat archives for rust-lang/rust

7th
Feb 2019
Kelly Thomas Kline
@kellytk
Feb 07 07:48
@tanriol Pardon the delay, https://github.com/kellytk/x-search
Denis Lisov
@tanriol
Feb 07 08:03
Ok, was a bit tricky. If you look at Message implementations, you may see that all the result types are not futures. Furthermore, there's an interesting impl MessageResponse for ResponseFuture, which expects that <M as Message>::Result is an actual Result.
Looks like the WorkerRequest's return type annotation should be #[rtype(result="Result<Result<Foo, rusqlite::Error>, actix::MailboxError>")]
Kelly Thomas Kline
@kellytk
Feb 07 08:15
Both lines 7 and 33?
Kelly Thomas Kline
@kellytk
Feb 07 08:36
@tanriol I modified both lines you can pull the commit and see the remaining error
Denis Lisov
@tanriol
Feb 07 09:36
No, only line 7
Kelly Thomas Kline
@kellytk
Feb 07 09:41
Oh how surprising. I don't think I've seen that yet in working with actix but you're correct, thank you!
Kelly Thomas Kline
@kellytk
Feb 07 09:57
@tanriol I've fixed that, enabled MainActor's message send to WorkerActor, disabled its message send to DbActor, and you can see an issue in MainActor on line 91 if you pull
It appears that one too many Results is being wrapped on the result no?
Denis Lisov
@tanriol
Feb 07 10:32
It looks like that's perfectly expected. You're working with nested Results. I don't know why :-)
Kelly Thomas Kline
@kellytk
Feb 07 10:40
@tanriol I don't intend to. If you look at main_actor.rs I'd like lines 79 and 81 to work the same as 80 and 82
The reason for the indirection is that MainActor and WorkerActor are on their own Arbiter::start threads and I'd like to offload expensive string processing into the WorkerActor to free up MainActor which is already very hot. DbActor is via a SyncArbiter with its own threads as well FWIW
Kelly Thomas Kline
@kellytk
Feb 07 10:49
When WorkerActor's result type declarations matched those of DbActor (Result<Foo, rusqlite::Error>) other errors were generated
Denis Lisov
@tanriol
Feb 07 10:57
You may want to make an error enum for your errors :-)
Kelly Thomas Kline
@kellytk
Feb 07 11:01
What would that look like?
Kelly Thomas Kline
@kellytk
Feb 07 11:09
Would it be possible to use the same result type as DbActor for WorkerActor? That would be ideal
Tritone
@tritone11
Feb 07 12:32
Hi, I am trying to give a response as a json, but I keep giving it escaped with backslashes like this
"{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":\"0x0\"}"
how can I parse this in a valid json and send it as a json?
Tritone
@tritone11
Feb 07 12:43
This is what happens when I parse the string with the json crate
JsonValue(String("{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":\"0x0\"}"))
Denis Lisov
@tanriol
Feb 07 13:25
How do you return this response?
@kellytk Probably not possible, it cannot contain the MailboxError which is needed.
Ogulcan Erduran
@erduranogulcan
Feb 07 14:11
how to reduce compile time for small changes in development ?
Edvin Malinovskis
@nCrazed
Feb 07 14:23
erduranogulcan: is the whole project recompiled on every change or just the changed crate?
tritone11: somewhat related consider using r## to avoid having to escape all those "
so something like r#"{"attr":"val"}"#
Geoffrey (Wolfie) Wolf
@WolfieWerewolf
Feb 07 14:25

I'm using it like this: const FRAGMENT_SHADER_SRC: &str = r#"#version 300 es
precision mediump float;
uniform vec4 un_color;
out vec4 outColor;

void main() {
outColor = un_color;
}
"#;

Ogulcan Erduran
@erduranogulcan
Feb 07 14:27
@nCrazed Yes, i change small things in main.rs and it compiles all packages that i do not use
Edvin Malinovskis
@nCrazed
Feb 07 14:29
that sounds wrong
what version are you on?
also stable or nightly?
incremental compilation has been in stable since 1.24
so only your code should be recompiled, the dependencies should be cached and not recompiled unless a) dependency version was changed b) your compiler version was changed c) build cache was deleted
not sure if there are any other conditions, but those wuld be the main culprits
Tritone
@tritone11
Feb 07 14:58
@tanriol I return with
json!(json_response)
Victor Lopes
@vlopes11
Feb 07 16:04

Rustaceans, is it correct to assume I can't use publish-subscribe pattern in Rust because I can't have instantiation related to generic types?

struct A {}
struct B {}

trait T {}

impl T for A {}
impl T for B {}

struct C<T: Sized> {
    v: Vec<T>
}

impl<T: Sized> C<T> {
    pub fn recv_something(&mut self, e: T) {
        self.v.push(e);
    }
}

fn main() {
    let a = A {};
    let b = B {};
    let mut c = C::<A> {
        v: vec![],
    };
    c.recv_something(b);
}

Will fail because recv_something is expecting concrete type A, not any instance of T.

Zakarum
@omni-viral
Feb 07 16:05
This will fail
But you can use dyn Trait
Ingvar Stepanyan
@RReverser
Feb 07 16:05
Box<dyn Trait> to be precise (as you can't push dynamically-sized items into an array)
Denis Lisov
@tanriol
Feb 07 16:05
@tritone11 If this is already a raw json, you should return it as a string, not with json!, I guess.
Zakarum
@omni-viral
Feb 07 16:05
Not necessary Box
Ingvar Stepanyan
@RReverser
Feb 07 16:05
But this code is a bit hard to follow as it has trait T which is unused but also generic T which is arbitrary type...
And then you are saying you want any instance of T but are defining your type with concrete type param
So I'm not totally sure what you're trying to do
Zakarum
@omni-viral
Feb 07 16:07
I'm pretty sure he tried to make Vec<T> without T as generic parameter and rustc complained that T isn't sized
Ingvar Stepanyan
@RReverser
Feb 07 16:08
well if T in Vec<T> is meant to be a trait, then you do need Box too like Vec<Box<dyn T>>
Zakarum
@omni-viral
Feb 07 16:08
struct A {}
struct B {}

trait T {}

impl T for A {}
impl T for B {}

struct C {
    v: Vec<Box<dyn T>>
}

impl C {
    pub fn recv_something(&mut self, e: impl T) {
        self.v.push(Box::new(e));
    }
}

fn main() {
    let a = A {};
    let b = B {};
    let mut c = C {
        v: vec![],
    };
    c.recv_something(b);
}
This should work
Ingvar Stepanyan
@RReverser
Feb 07 16:09
or one of the exotic crates that allow you to push arbitrary data (there was a discussion about these here few days ago)
Zakarum
@omni-viral
Feb 07 16:09
Simple linked list can accept dyn Trait actually. But I wouldn't advise to use it unless you need properties of linked lists
Ingvar Stepanyan
@RReverser
Feb 07 16:10
@vlopes11 I'm not sure what do you mean by publish-subscribe pattern if you're pushing to array though? Maybe you want to subscribe using callbacks? That could be done differently and more efficiently.
Ogulcan Erduran
@erduranogulcan
Feb 07 16:14
@nCrazed 2018-stable, I added diesel. Command is "cargo run" I did not use diesel just added but when I run it it compiles diesel too
Ingvar Stepanyan
@RReverser
Feb 07 16:15
@erduranogulcan It does, but only once, right?
Cargo can't know what you're using or not inside of your code in advance, so it compiles everything you have in Cargo.toml, this is expected
Ogulcan Erduran
@erduranogulcan
Feb 07 16:15
@RReverser no
Ingvar Stepanyan
@RReverser
Feb 07 16:15
I mean, if you run cargo run again, it won't recompile diesel
Or does it for you?
Victor Lopes
@vlopes11
Feb 07 16:16
@RReverser Oh, this is just a minimum example. The idea is to receive different concrete types for a single attribute (Vec or anything)
Let me test
Ogulcan Erduran
@erduranogulcan
Feb 07 16:16
@RReverser what command I should use? It compiles
Ingvar Stepanyan
@RReverser
Feb 07 16:17
@erduranogulcan Can you show your logs from console when you run cargo run twice in a row?
Ogulcan Erduran
@erduranogulcan
Feb 07 16:18
It compiles diesel and other packages that are in cargo.toml even I do not use them currently
Ingvar Stepanyan
@RReverser
Feb 07 16:19
@erduranogulcan Yes, that one I replied to, this is per design. But it should happen only on the first run, and you seemed to suggest it doesn't? If so, then there is a problem which we're trying to help with.
Ogulcan Erduran
@erduranogulcan
Feb 07 16:19
@RReverser I will paste it here in 20 mins
Ogulcan Erduran
@erduranogulcan
Feb 07 17:00

@RReverser

root@2fd92e9bb135:/rust/demoproject# cargo run
    Blocking waiting for file lock on build directory
   Compiling proc-macro2 v0.4.27
   Compiling backtrace v0.3.13
   Compiling rand_chacha v0.1.1
   Compiling rand v0.6.5
   Compiling quote v0.6.11
   Compiling syn v0.15.26
   Compiling uuid v0.7.2
   Compiling synstructure v0.10.1
   Compiling diesel_derives v1.4.0
   Compiling serde_derive v1.0.87
   Compiling failure_derive v0.1.5
   Compiling diesel v1.4.1
   Compiling failure v0.1.5
   Compiling dotenv v0.13.0
   Compiling demoproject v0.1.0 (/rust/demoproject)
    Finished dev [unoptimized + debuginfo] target(s) in 1m 01s
     Running `target/debug/demoproject`
blablabla
root@2fd92e9bb135:/rust/demoproject# cargo run
   Compiling proc-macro2 v0.4.27
   Compiling rand_chacha v0.1.1
   Compiling backtrace v0.3.13
   Compiling rand v0.6.5
   Compiling quote v0.6.11
   Compiling syn v0.15.26
   Compiling uuid v0.7.2
   Compiling synstructure v0.10.1
   Compiling diesel_derives v1.4.0
   Compiling serde_derive v1.0.87
   Compiling failure_derive v0.1.5
   Compiling diesel v1.4.1
   Compiling failure v0.1.5
   Compiling dotenv v0.13.0
   Compiling demoproject v0.1.0 (/rust/demoproject)
    Finished dev [unoptimized + debuginfo] target(s) in 55.36s
     Running `target/debug/demoproject`
blablabla

Cargo.toml

[dependencies]
regex = "*"
uuid = { version = "*", features = ["v4"] }
serde = "*"
serde_json = "*"
serde_derive = "*"
diesel = { version = "*", features = ["postgres"] }
dotenv = "*"
Ingvar Stepanyan
@RReverser
Feb 07 17:01
That looks bad indeed. Did you change something between these two runs?
it's also interesting that the first one says "Blocking waiting for file lock on build directory" which would mean that there is another build process in parallel
could that be RLS from editor integration or something? it shouldn't cause this, but can you try closing your editors and doing same two runs?
Ogulcan Erduran
@erduranogulcan
Feb 07 17:04
actually i removed and created docker container again then copied project files to in container
Ingvar Stepanyan
@RReverser
Feb 07 17:04
ah well
that would cause it
Ogulcan Erduran
@erduranogulcan
Feb 07 17:05
but i worked in same container for hours and it compiled it all the time
is there any other run command?
Ingvar Stepanyan
@RReverser
Feb 07 17:06
no, this should be it
but I can't help with that much, as there's plenty of potential reasons why this would break in container, especially on Windows / Mac where it's running via VM
Ogulcan Erduran
@erduranogulcan
Feb 07 17:06
maybe because of package versions? i used "*"
Ingvar Stepanyan
@RReverser
Feb 07 17:06
like wrong clocks etc.
no, that shouldn't affect it
as you still have Cargo.lock generated
Ogulcan Erduran
@erduranogulcan
Feb 07 17:08
there are like 74 packages in compile time. it skips 64 of them just compile 10 of them and it takes 10 secs
because of diesel
but i did not use diesel in my project
@RReverser is there a problem with compile some spesific packages every run command?
Ingvar Stepanyan
@RReverser
Feb 07 17:11
no, as I said, command is fine
there are very few possible reasons to this, and most are external to what you're doing
Ogulcan Erduran
@erduranogulcan
Feb 07 17:11
i thought maybe diesel should compile every time
i just used serde not diesel
Ingvar Stepanyan
@RReverser
Feb 07 17:12
yes, you said you are not using diesel, but that's unrelated

there are very few possible reasons to this, and most are external to what you're doing

1) improper container integration so that clock returns skewed results or something and Cargo thinks files are newer and have to be rebuilt
2) some external process (like editor integration) kills your build cache
3) some misbehaving crate has build.rs that forces it to be rebuilt all the time, but then it sounds like it would be inside diesel and there doesn't seem to be any reports about this problem from anyone else

Ogulcan Erduran
@erduranogulcan
Feb 07 17:13
@RReverser i am using vs code but codes in docker
Ingvar Stepanyan
@RReverser
Feb 07 17:14
as I said above, maybe try closing all editors, waiting few seconds and then doing these runs in docker (while not copying files)... at least it would allow to rule out this as the cause.
I mean, if you're copying all files into docker container, that would also cause rebuilds, but you said most of the time you're not doing it?
Ogulcan Erduran
@erduranogulcan
Feb 07 17:16
@RReverser first i crated project in docker container everthing works well. then i added packages and tried to run. it compiles everything once then other run commands skiped 64 of 74 packages but 10 of them compiled every time. Then i removed container copied files to local. created again 10 mins ago and it is still same
i could not get it why it compiles diesel every time
it took 10+ secs its annoying
@RReverser everything aside, i really want to say thanks for your effort to explanation for this problem
Geoffrey (Wolfie) Wolf
@WolfieWerewolf
Feb 07 17:34
Guys.. I'm a total rust noob and I'd like to ask a question about lifetimes which are kicking my butt.. so please forgive me if it takes me a minute to explain exactly what I am asking.. and if you would be so kind as to offer some advice I would really appreciate it.. here goes
I have this fn
fn get_shaders(_callback: &'static dyn Fn(JsValue)) {

    let fetch = & | _val: JsValue| {
        //_callback(_val);
        let console_ = JsValue::get_global("console");
        console_.call_method_with_arg("log", _val);
    };

    JsValue::get_global("wolf")
        .call_method_with_args("fetch", &[&"shaders/cube.glsl", &wrap(fetch)]);
}
and I am calling it like this
const FETCH_RES_PROMISE:&dyn Fn(JsValue) -> () = &|_val: JsValue| {

    };

    get_shaders(FETCH_RES_PROMISE);
There's nothing in the callback I'm passing in right now for simplicity
The above works until I attempt to use the _callback
I get the following compiler error
error[E0716]: temporary value dropped while borrowed
   --> src/lib.rs:157:19
    |
157 |       let fetch = & | _val: JsValue| {
    |  ___________________^
158 | |         _callback(_val);
159 | |         //let console_ = JsValue::get_global("console");
160 | |         //console_.call_method_with_arg("log", _val);
161 | |     };
    | |_____^ creates a temporary which is freed while still in use
...
164 |           .call_method_with_args("fetch", &[&"shaders/cube.glsl", &wrap(fetch)]);
    |                                                                    ----------- argument requires that borrow lasts for `'static`
165 |   }
    |   - temporary value is freed at the end of this statement
And I really don't understand enough of that to ask a better question than... does anyone have an idea why?
apprecaite any insights in advance
Ichoran
@Ichoran
Feb 07 17:39
Please use ``` on their own lines above and below your code to get code formatting.
Geoffrey (Wolfie) Wolf
@WolfieWerewolf
Feb 07 17:39
I see
let me reformat that thanks
Sorry I should have realized it was like MD
Ichoran
@Ichoran
Feb 07 17:45
Ah, yeah, that's kind of tricky. The closure itself (not the ref to it) has an existence (has to, since it captures _callback); it needs something to own it. I don't have time to give you a proper answer or fix, sorry! Hopefully someone else can.
Geoffrey (Wolfie) Wolf
@WolfieWerewolf
Feb 07 17:46
@Ichoran ... Well you have helped actually. To know that this is "kinda tricky" makes me feel better at least ;-)
Thank you
Denis Lisov
@tanriol
Feb 07 18:31
@WolfieWerewolf What are you trying to do with that callback?
Geoffrey (Wolfie) Wolf
@WolfieWerewolf
Feb 07 18:32
@tanriol I just want to return the results of the inner closure when they are available.. it's an async op
Maybe I am duplicating callback H**l here unnecessarily?
Wil Hunt
@huntwj
Feb 07 19:38

Apologies if this isn’t the correct channel…

I’m attempting to create a proof of concept application with Rocket 0.4 and Diesel 1.4.1 using the current nightly build on Mac.

When I add diesel to my list of dependencies, one of its child dependencies, dotenv v0.10.1 fails to compile. I don’t know if this is a bug in the nightly Rust or an issue with a pre version 1 dotenv, but here it is:

  Compiling dotenv v0.10.1
error[E0659]: `error_chain` is ambiguous (derive helper attribute vs any other name)
  --> /Users/wil/.cargo/registry/src/github.com-1ecc6299db9ec823/dotenv-0.10.1/src/lib.rs:23:40
   |
23 | #[cfg_attr(not(feature = "backtrace"), error_chain(backtrace = "false"))]
   |                                        ^^^^^^^^^^^ ambiguous name
   |
note: `error_chain` could refer to the derive helper attribute defined here
  --> /Users/wil/.cargo/registry/src/github.com-1ecc6299db9ec823/dotenv-0.10.1/src/lib.rs:22:17
   |
22 | #[derive(Debug, error_chain)]
   |                 ^^^^^^^^^^^
note: `error_chain` could also refer to the derive macro imported here
  --> /Users/wil/.cargo/registry/src/github.com-1ecc6299db9ec823/dotenv-0.10.1/src/lib.rs:10:1
   |
10 | #[macro_use]
   | ^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0659`.
error: Could not compile `dotenv`.
Is this a situation I need to address with the nightly Rust folk or the dotenv folk?
➜  project-dir git:(master) ✗ rustc --version
rustc 1.34.0-nightly (b139669f3 2019-02-06)
Denis Lisov
@tanriol
Feb 07 20:49
@WolfieWerewolf Why not use Promises on JS side and Futures on Rust side?
If that sounds like a good idea, take a look at wasm_bindgen_futures
Geoffrey (Wolfie) Wolf
@WolfieWerewolf
Feb 07 20:50
@tanriol I was actually just looking at futures and went down a rabit hole with async / await which seems pretty new.
I want to avoid wasm_bindgen and std web if I can help it... They both seem to be moving targets
I took a fork of this
It looks very good and I've chatted with the author a couple of times. I'm familiar with emscripten's API and this is low level like that
But that's for the suggestion, I am looking into futures now
Kelly Thomas Kline
@kellytk
Feb 07 22:59
@tanriol I updated the problem demo https://github.com/kellytk/x-search elaborating WorkerActor to make it more realistic. I'm back swimming in errors over my head would you help?