These are chat archives for rust-lang/rust

9th
Dec 2017
Roman Proskuryakov
@kpp
Dec 09 2017 08:33
Aren't you bored with futures::sync::mpsc::channel(8) copypaste? I hate that magic number
Naja Melan
@najamelan
Dec 09 2017 14:46
Is there any more ergonomic way to write this?
fn key( path: &str ) -> Vec< String >
    {
        let mut components: Vec< String > = path.split( '/' ).map( |s| s.to_string() ).collect();

        // If present, remove the trailing empty string (which represents a trailing slash).
        // If it isn't removed a path without a trailing space will never match the same path with trailing space
        // Can't find a way to write this without the boolean without upsetting the borrow checker.
        //
        let mut trailing = false;

        {
            if let Some( s ) = components.last() {
            if s.is_empty()
            {
                trailing = true;
            }}
        }

        if trailing { components.pop(); }

        components
    }
The borrow checker won't let me pop without creating a bool in a separate scope
because components.last borrows the vector
This seems like a lot of boilerplate to express: "if the last string in the vec is empty, pop it"
Sean
@seanr707
Dec 09 2017 16:56
@najamelan What about this?
if match components.last() {
    Some(s) => s.is_empty(),
    _ => false,
} {
       components.pop();
}
Restioson
@Restioson
Dec 09 2017 19:25
@tanriol i might be able to rather have an enum which has Sized and Unsized variants
Thanks for the helpful info thought!
Naja Melan
@najamelan
Dec 09 2017 19:38
Also, I would prefer here to just use a slice, but that can't be returned from the method because it's size is not known at compile time, even though I declared the method inline.
@seanr707 Thanks alot, that works. Do I get it right that the match arguments is in the match scope (so the borrow is out of scope when the pop happens), where as an if expression belongs to the outer scope?
Michal 'vorner' Vaner
@vorner
Dec 09 2017 19:43
Hmm, cool :-|. I just got 100kB long error message.
Sean
@seanr707
Dec 09 2017 20:24
@najamelan I'm fairly new to the language but I believe so. What kind of error were you receiving before?
Nikita
@nikita-y
Dec 09 2017 22:39
hi there
i’ve got a problem. It seems that str::from_utf8 corrupts data
Denis Lisov
@tanriol
Dec 09 2017 22:41
Do you have a reproducible example?
Nikita
@nikita-y
Dec 09 2017 22:42
extern crate curl;
extern crate json;

use curl::easy::Easy;
use std::str;
use json::*;

fn main() {
    let query = build_qery();
    let mut easy = Easy::new();
    easy.url(query.as_str()).expect("Query error");

    let mut transfer = easy.transfer();
    let result = transfer.write_function(|data| {
        println!("{}", str::from_utf8(data).unwrap());
        //let temp_s = str::from_utf8(data).unwrap();
       // let parsed = json::parse(temp_s.expect("FFFFUCK");
        Ok(data.len())
    });
    result.expect("Transfer error");
    transfer.perform().expect("Transfer error")
}

fn build_qery() -> String {
    "https://api.darksky.net/forecast/6143f6925645b592e2ca57829537a30a/37.8267,-122.4233?units=si".to_owned()
}
when i execue curl request from console it works just fine
however when i use the example app it returns currupted json
Denis Lisov
@tanriol
Dec 09 2017 22:46
This code seems to work for me... what error should I observe?
Nikita
@nikita-y
Dec 09 2017 22:47
if you try to pharse json from the string (commented code) you’ll get pharse error
Denis Lisov
@tanriol
Dec 09 2017 22:52
Your problem is not related to from_utf8
Nikita
@nikita-y
Dec 09 2017 22:53
So far i have 3 suspects curl might corrupt data or str::from_utf8or I might be just an idiot
Denis Lisov
@tanriol
Dec 09 2017 22:53
You don't account for the fact that the closure passed to write_function can be called multiple times with parts of the reply.
Nikita
@nikita-y
Dec 09 2017 22:59
@tanriol It might explain why I get UnexpectedEndOfJson. Thank you
Sean
@seanr707
Dec 09 2017 23:13
Anyone on have any experience with cross compiling and/or gtk developement in Rust?