These are chat archives for rust-lang/rust

4th
Sep 2017
Lloyd
@lloydmeta
Sep 04 2017 11:43

I've started using the newly stable associated constants feature and found that it works great.

My question is: when does the value of a constant get evaluated in the case of recursive implementations?

e.g.

pub struct HNil;
pub struct HCons<H, T>(H, T);

pub trait HList {
  const LEN: usize;
}

impl HList for HNil {
  const LEN: usize = 0;
}

impl <H, T: HList>  HList for HCons<H, T> {
  // when does this LEN get evaluated?
  const LEN: usize = 1 + <T as HList>::LEN;
}
by "when", I'm wondering whether it's compile-time (yay!) or runtime (ok too)
Michal 'vorner' Vaner
@vorner
Sep 04 2017 11:46
Compile time. All constants are compile-time evaluated.
Lloyd
@lloydmeta
Sep 04 2017 11:46
perfect thanks :)
Dexter
@neoel
Sep 04 2017 13:50
Hi, I'm having some difficulty understanding lifetimes in a scenario. I'm using serde and trying to parse some yaml into a struct.
Denis Lisov
@tanriol
Sep 04 2017 13:55
What are the NewWorkload and NewTask?
Dexter
@neoel
Sep 04 2017 14:03
These are structs -> heres the definition:
#[derive(Insertable,Serialize,Deserialize,Debug)]
#[table_name="workloads"]
pub struct NewWorkload<'a> {
    pub project: &'a str,
    pub image: &'a str,
}

#[derive(Insertable,Serialize,Deserialize,Debug)]
#[table_name="tasks"]
pub struct NewTask<'a> {
    pub workload: i32,
    pub cmd: &'a str,
    // pub env: &'a str, // Is a json type
}
Denis Lisov
@tanriol
Sep 04 2017 15:13
You need to annotate the tasks field with #[serde(borrow)], then it compiles. Also see the example in serde documentation.
Dexter
@neoel
Sep 04 2017 15:15
I tried that, but it wouldn't work
Denis Lisov
@tanriol
Sep 04 2017 15:15
However, note that borrowing strings from the serialized data may not always work (for example, in JSON a string containing escape sequences cannot be zero-copy deserialized into &str).
Dexter
@neoel
Sep 04 2017 15:16
It was fixed by not using static strings.
Denis Lisov
@tanriol
Sep 04 2017 15:17
Yeah, that's usually a fine way too :-)
twissel
@twissel
Sep 04 2017 17:40
Hi, I have some trouble using futures BufferUnordered adapter, maybe you guys can help me: https://stackoverflow.com/questions/46041185/appending-futures-to-bufferunordered-stream
Zakarum
@omni-viral
Sep 04 2017 21:08
I spent some unreasonable amount of time trying to figure out why type T from crate t doesn't implement trait Y from crate y while it obviously should.
After bruteforsing all possibilities I guessed that the reason is that crate t use different version of y. But compiler didn't hinted me. Why? rustc do a good job pointing out that variables have a type from different version of crate when I try to assign one to another.