These are chat archives for rust-lang/rust

7th
Sep 2018
David James
@xpe
Sep 07 2018 05:37
I realize that the code below is wrong:
pub fn parse_toml_file<'de, P, D>(path: P) -> D
    where P: AsRef<Path>, D: Deserialize<'de> {
    let s = read_to_string(path).unwrap();
    toml::from_str(&s).unwrap()
}
Because s does not live long enough.
So, I've been trying other ways, such as changing to where P: Into<PathBuf>.
David James
@xpe
Sep 07 2018 06:03

I've been going with:

pub fn parse_toml_file<'d, D>(path: &'d Path) -> D
    where D: Deserialize<'d> {
    let s = read_to_string(path).unwrap();
    toml::from_str(&s).unwrap()
}

...and making other changes to accommodate this.

Denis Lisov
@tanriol
Sep 07 2018 06:07
How about bounding with D: DeserializeOwned instead?
David James
@xpe
Sep 07 2018 06:17
@tanriol Thanks, didn't know about that trait -- looks like a better way (turns out my example doesn't compile)
Gives me
error[E0596]: cannot borrow immutable borrowed content as mutable
    --> manu/manu-derive/src/lib.rs:1338:25
     |
1338 |             }*/get_type(&mut json);            
     |                         ^^^^^^^^^ cannot borrow as mutable
Don't understand, what is immutable
Zakarum
@omni-viral
Sep 07 2018 09:35
json is reference to immutable Value
Function takes reference to mutable Value
let json: &mut Value
trsh
@trsh
Sep 07 2018 10:56
Having troubles with mutables today
let val =  { 
                    let bb = json.clone();
                    let d = bb.get("type").expect("63").as_str().expect("64");
                    let s = d.clone();
                    s
                };
                *json.get_mut("type").unwrap() = json!([val, "null"]);
    --> manu/manu-derive/src/lib.rs:1318:29
     |
1318 |                     let d = bb.get("type").expect("63").as_str().expect("64");
     |                             ^^ borrowed value does not live long enough
...
1321 |                 };
     |                 - `bb` dropped here while still borrowed
...
1324 |             };  
     |             - borrowed value needs to live until here
why does's bb nee to live until there, if I take String from it and clone?
Denis Lisov
@tanriol
Sep 07 2018 11:13
Looks like you're cloning &str, not converting it to a String
trsh
@trsh
Sep 07 2018 11:17
fkc, true
Jan Hlavatý
@hlavaatch
Sep 07 2018 11:21
What Rust synchronization primitive should I use to have a bunch of threads wait for an event to happen? I want them all to sleep and then all wake up at once...
std::sync::Condvar maybe?
Kelly Thomas Kline
@kellytk
Sep 07 2018 11:42
If I need to use the variant of an enum as a string value ("Foo" from Myenum::Foo) can that be automatically extracted or must I create a match block which returns String values for variants?
Lyle Mantooth
@IslandUsurper
Sep 07 2018 11:42
@kellytk, sounds like a good use for a function.
Or, serde_json can do that, but if you're not already using it, I'd just write a function.
Kelly Thomas Kline
@kellytk
Sep 07 2018 11:44
Ok thanks
Ash
@ashthespy
Sep 07 2018 16:06
Is there an equivalent for std::sync::mpsc::Receiver::try_recv in the futures::sync::mpsc::Receiver version of it?
Kelly Thomas Kline
@kellytk
Sep 07 2018 19:48
To remove an element from a Vec its index must be provided. Can the index be obtained by passing the element at a later time?
Denis Lisov
@tanriol
Sep 07 2018 20:54
@ashthespy If you really need that, it's probably poll
@kellytk What are you trying to do?
Kelly Thomas Kline
@kellytk
Sep 07 2018 22:41
Retrieve the index of a [struct] element that may or may not have been pushed to a Vec @tanriol
I'm using .iter().position(|&x| x == id) currently and it appears to function as expected however I'm not very familiar with the available API so it seems to be worth asking about more direct alternatives