These are chat archives for rust-lang/rust

10th
Jun 2018
jiuweigui
@jiuweigui
Jun 10 2018 00:17
Anybody know/have recommendations about mqtt libraries? All I have managed to find are either pre-alpha or abandoned..
Fredrik Portström
@portstrom
Jun 10 2018 08:39
@trsh Your code allocates no memory.
Zakarum
@omni-viral
Jun 10 2018 08:41
That's stack allocations. 4 stack allocations
last one allocates fixed amount of bytes for any type (4 on 32bit and 8 on 64bit system).
Fredrik Portström
@portstrom
Jun 10 2018 08:46
The entire stack (2 MB by default) is allocated up front, and no memory allocations are made when putting values on the stack.
Fredrik Portström
@portstrom
Jun 10 2018 09:05
If your question is how much these variables add to the size of the stack frame of your function is, that question has a more complex answer. In an optimized build, your unused variables will be removed and use no memory at all. Try this as a debug and then release build:
https://play.rust-lang.org/?gist=250383fc8e9c931f7d6056a405448479&version=stable&mode=debug
https://play.rust-lang.org/?gist=250383fc8e9c931f7d6056a405448479&version=stable&mode=release
trsh
@trsh
Jun 10 2018 09:52
@portstrom I was always thinking that when I do x = y, its like cloning values, but x = &y just point to same location in memory
trsh
@trsh
Jun 10 2018 14:10
What would be The most elegant way to conver JsonValue in serde_json::Value?
David Harvey-Macaulay
@alteous
Jun 10 2018 14:13
   Uploading gltf v0.11.0 (file:///home/alteous/gltf)
error: failed to get a 200 OK response, got 503
headers:
    HTTP/1.1 100 Continue



    HTTP/1.1 503 Service Unavailable

    Connection: keep-alive

    Server: Cowboy

    Date: Sun, 10 Jun 2018 14:12:58 GMT

    Content-Length: 506

    Content-Type: text/html; charset=utf-8

    Cache-Control: no-cache, no-store
This just happened twice in a row and gltf-json and gltf-derive published just fine.
Any ideas why?
Fredrik Portström
@portstrom
Jun 10 2018 14:49
@trsh Could you explain your question in more detail?
David Harvey-Macaulay
@alteous
Jun 10 2018 15:34
I think I figured why: the package is too large for crates.io to accept. After adding include = ["src", "examples", "tests", "Cargo.toml"]to the [package] section the package uploaded just fine.
Oh gosh now https://docs.rs/gltf is broken...
David Harvey-Macaulay
@alteous
Jun 10 2018 16:38
Ah, cargo package --list is helpful in these situations.
Mathieu Prevel
@mprevel
Jun 10 2018 17:24

Hi guys,
I'm new to rust and try to parse a file line by line but get lifetimes issues :s

// the struct I build
#[derive(Debug, Clone)]
pub struct TestCfg<'a> {
    audio_file: &'a str,
    volume: u32
}

fn parse<'a>(s: &'a str, i: &str) -> (&'a str, u32) {
    (s, i.parse().unwrap())
}

fn to_config<'a>(s: &'a str) -> Option<TestCfg<'a>> {
    let x = s.split(";");
    let split: Vec<&str> = x.collect();

    let maybe_config: Option<TestCfg<'a>> = match split.as_slice() {
        [a, b] => {
            let (audio_file, volume) = parse(a, b);
            let cfg = TestCfg {
                audio_file,
                volume
            };
            Some(cfg)
        },
        _ => None
    };

    maybe_config
}

// the issue is when I read the file and try to map each line to a config
// lifetime is not long enough in the lambda
// ...
            let vec_configs: Vec<TestCfg> = buffer.lines()
                .flat_map(|line| { // line is of type Result<String, Error>
                    let l = line.unwrap();
                    let the_config = to_config(&l); // <= issue here 'borrowed value does not live long enough'
                    the_config
                })
                .collect();

What do I miss to get the proper lifetime ?

David Harvey-Macaulay
@alteous
Jun 10 2018 17:26
@mprevel What type is buffer in buffer.lines()?
Sylwester Rąpała
@xoac
Jun 10 2018 17:27
@mprevel You can share your example using https://play.rust-lang.org/
Mathieu Prevel
@mprevel
Jun 10 2018 17:27
        let mut buffer = BufReader::new(file);
David Harvey-Macaulay
@alteous
Jun 10 2018 17:30
In this situation, this will not work because buffer.lines() produces String which must be 'owned' by something but audio_file: &str strictly only 'borrows' from an owner.
One solution may be to read the file into a buffer: String first and call buffer.lines() since that would produce &str.
Another might be to make audio_file: String or audio_file: Cow<'a, str>.
Does that make any sense?
Mathieu Prevel
@mprevel
Jun 10 2018 17:32
Thanks, I'll check that :)
Mathieu Prevel
@mprevel
Jun 10 2018 17:39
It works :)
            use std::io::Read;

            let mut buff_str = String::new();
            file.read_to_string(&mut buff_str);

            let vec_configs: Vec<TestCfg> = buff_str.lines()
                .flat_map(|line| to_config(&line))
                .collect();
Jakub Kozlowski
@jkozlowski
Jun 10 2018 22:47
I am wrapping a c lib (spdk) and therefore need to pass a rather long (and complex) set of linker flags (so not just -l and -L); I think I need to use .cargo/config file for this, but I am facing issues with the custom flags applying to all my dependencies, rather than just my wrapper; so if I use [build] and list all the flags, then they apply also to all my dependencies and cause some issues; I tried [target.x86_64-unknown-linux-gnu.rust_spdk], but it doesn't seem to work either. I was hoping someone could give me some pointers. Thanks!
the flags are -Wl,--whole-archive -lspdk_copy_ioat -lspdk_ioat -Wl,--no-whole-archive -Wl,--whole-archive -lspdk_vbdev_lvol -lspdk_blob -lspdk_blob_bdev -lspdk_lvol -lspdk_bdev_malloc -lspdk_bdev_null -lspdk_bdev_nvme -lspdk_nvme -lspdk_vbdev_passthru -lspdk_vbdev_error -lspdk_vbdev_gpt -lspdk_vbdev_split -lspdk_bdev_aio -lspdk_bdev_virtio -lspdk_virtio -Wl,--no-whole-archive -laio -L/tmp/spdk/build/lib -Wl,--whole-archive -lspdk_event_bdev -lspdk_event_copy -Wl,--no-whole-archive -lspdk_blobfs -lspdk_blob -lspdk_bdev -lspdk_blob_bdev -lspdk_copy -lspdk_event -lspdk_util -lspdk_conf -lspdk_trace -lspdk_log -lspdk_jsonrpc -lspdk_json -lspdk_rpc /tmp/spdk/build/lib/libspdk_env_dpdk.a -Wl,--start-group -Wl,--whole-archive /tmp/spdk/dpdk/build/lib/librte_eal.a /tmp/spdk/dpdk/build/lib/librte_mempool.a /tmp/spdk/dpdk/build/lib/librte_ring.a /tmp/spdk/dpdk/build/lib/librte_mempool_ring.a /tmp/spdk/dpdk/build/lib/librte_pci.a /tmp/spdk/dpdk/build/lib/librte_bus_pci.a -Wl,--end-group -Wl,--no-whole-archive -lnuma -ldl -lrt -luuid