These are chat archives for rust-lang/rust

23rd
Jul 2017
Sherab Giovannini
@Shaddy
Jul 23 2017 09:07
is there any std::mem::* function that allows me to discover the entire memory consumption of any type? size_of_val drops me 24 for a huge string, for example
is it required to iterate over each element and calculate each item?
Michal 'vorner' Vaner
@vorner
Jul 23 2017 09:27
@Shaddy: I think this one measures only the „local“ (eg. stack) size of the type. Maybe this is what you want? https://crates.io/crates/heapsize
Sherab Giovannini
@Shaddy
Jul 23 2017 10:16
yep
ty
Sherab Giovannini
@Shaddy
Jul 23 2017 11:07

I'm doing another dummy codes, this one will read randomly some bits over a slice.


    use BitSize::{U8, U16, U32, U64};

    let bitsize = [U8, U16, U32, U64];

    while total_bits > 0 {
        let choice = rng.choose(&bitsize).unwrap();

        let bits = match *choice {
            U8 => rng.gen_range(0, 8),
            U16 => rng.gen_range(0, 16),
            U32 => rng.gen_range(0, 32),
            U64 => rng.gen_range(0, 64)
        };            

        let readen:u32 = match *choice {
            U8 =>  reader.read_u8(bits),
            U16 => reader.read_u16(bits),
            U32 => reader.read_u32(bits),
            U64 => reader.read_u64(bits),
        };

        if readen.is_ok() {
            total_bits -= bits as u32;
        } else {
            break;
        }
    }

The problem is that each read, returns a read_u8 => Result<u8, bitreader::BitStreamError> same for u16, etc.

Which is the best way to accomplish different return types?

another way that comes to my mind is to just return a bool of is_ok for each read, then use it to break the loop, but the question is still there
RuRu92
@RuRu92
Jul 23 2017 17:09
How do I pass a object that has a trait object into a thread?
Sathya Narrayanan
@sourcepirate
Jul 23 2017 17:19
How do i return a 'static str from a function .
fn encode(s: 'static str) -> 'static str {}
is such return possible ???
i know &'static str return is possible
Andrey Lesnikov
@ozkriff
Jul 23 2017 17:21
fn encode(s: &'static str) -> &'static str { "xxx" } ?
Ilya Bogdanov
@vitvakatu
Jul 23 2017 17:21
@sourcepirate it's not possible, you can only return reference with lifetime, not lifetime without reference
Sathya Narrayanan
@sourcepirate
Jul 23 2017 17:23
Yup that's what i thought but after seeing this http://exercism.io/exercises/rust/run-length-encoding
it thowed me in a little confusion.
Sathya Narrayanan
@sourcepirate
Jul 23 2017 17:29
Thanks @vitvakatu @ozkriff
David Harvey-Macaulay
@alteous
Jul 23 2017 17:29
Hi, are there any general tips for improving (library) crate build times?