These are chat archives for rust-lang/rust

28th
Mar 2019
Sam Johnson
@sam0x17
Mar 28 00:19
oh that's handy
Sam Johnson
@sam0x17
Mar 28 00:39
anyone know how to convert a BufMut to a Bytes in the bytes crate?
matrixbot
@matrixbot
Mar 28 00:40
bspeice You'd need to read everything in the BufMut and copy out to a separate Bytes buffer; because BufMut can be a chain of Bytes underneath, it's not a trivial conversion.
Sam Johnson
@sam0x17
Mar 28 00:41
ok in that case, what would be the best way to get a Bytes that is two Byteses concatenated with each other?
I was going to use BufMut initialized with a capacity of bytes1.len() + bytes2.len()
but then BufMut didn't have the to_bytes method I thought it was going to have, as you mention :/
matrixbot
@matrixbot
Mar 28 00:42
bspeice Remember, BufMut is a trait, not a struct. The idea would be to create a BytesMut with capacity as you mentioned, then copy the first and second Bytes.
bspeice Alternately, if you don't actually want to do the copy, you can use Chain as a view over both underlying Bytes.
bspeice Edit: BytesMut isn't a thing, I should've just said Bytes.
Sam Johnson
@sam0x17
Mar 28 00:44
gotcha
Hassan Farooq
@typon
Mar 28 02:03
hi. i have a question about storing references in structs. can i ask it here?
i want to be able to store an instance of TakesRef inside my wrapper class - and i'm pretty lost on how to please the borrow checker. ive tried the "rental" crate, but i couldnt really get it to work even with it.
matrixbot
@matrixbot
Mar 28 02:53

krircc Standardizing WASI: A system interface to run WebAssembly outside the web →

https://hacks.mozilla.org/2019/03/standardizing-wasi-a-webassembly-system-interface/

Sergie Kardashov
@leorik
Mar 28 04:09
@typon You can't move borrowed value. Think of move semantic like a literal moving of data in memory (although its not always a case), doing so will invalidate reference and potentiallly cause SEGFAULT.
In your example you are doing just that: by assigning Wrapper.string value of string, you are trying to move value of string while its borrowed, and thats illegal.
However, if you remove Wrapper.string field and corresponding value move, you'll get another problem: Wrapper::new function owns string value, so when this function complete its execution, string value will be destroyed while you holding reference to it (ref inside TakesRef) and you'll get another dangling reference problem.
To circumvent this you have to options: either String::clone this value in TakesRef struct, or heap allocate value with Rc or Arc like in this example: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=aac521bebfbb0bff831fa755ba26b60c
Sergie Kardashov
@leorik
Mar 28 04:22
Or, if you insist on using refs, just make sure string lives longer than Wrapper and TakesRef : https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=7202fb0c31ed831c7e303758036d92a5
Sam Johnson
@sam0x17
Mar 28 05:35
FYI I ended up doing this:
        let merged = [&input[..], &input[..]].concat();
        Ok(Bytes::from(merged))
this "duplicates" the input Bytes object, which is what I wanted to do -- create a bytes object that is the input repeated twice
Ash
@ashthespy
Mar 28 17:42
Am I making things more complicated than required here?
{
        let max:usize = 15;
        let trim_to:usize = 10;
        let mut vec:Vec<usize> =  (0..max).collect();
        let vec_len = vec.len();
        println!("vec[{}]: {:?}",vec.len(),vec);
        if let Some(trim_at) = vec_len.checked_sub(trim_to) {
            println!("trim_at: {}", trim_at);
            vec.drain(0..trim_at);
            let tail:Vec<usize> =  (vec_len - trim_to..max).collect();
            assert_eq!(vec, tail);
        }
        println!("snip_vec[{}]: {:?}",vec.len(),vec);
    }
Riley Cat
@WreckedAvent
Mar 28 18:26
@ashthespy ^
depending on how meaningful mutating the vec is
Ash
@ashthespy
Mar 28 18:28
Hmm - initally was using split_off but since I don't need the "head" thought drain was better?
Riley Cat
@WreckedAvent
Mar 28 18:29
"better" has a lot of definitions :slight_smile:
in what way?
Ash
@ashthespy
Mar 28 18:31
In my actual use case - I want to snip the vector, and then add new stuff to it..
Riley Cat
@WreckedAvent
Mar 28 18:34
ah, the mutations are meaningful
Ash
@ashthespy
Mar 28 18:35
I was also wondering if I could simply checking for the case when trim_at > max
Riley Cat
@WreckedAvent
Mar 28 18:36
well, you can keep using the split methods and just clone_from_slice to get a vec you can push onto, or keep doing what you're doing
Ash
@ashthespy
Mar 28 18:37
Doesn't that add an extra allocation?
Riley Cat
@WreckedAvent
Mar 28 18:42
sure
do you value conciseness or avoiding a clone of 5 integers? :slight_smile:
that's what I meant by "better", there's no one "better"
Ash
@ashthespy
Mar 28 20:46

do you value conciseness or avoiding a clone of 5 integers? :slight_smile:

This was just a toy example - the actual use case a bit more complex :-)

Denis Lisov
@tanriol
Mar 28 21:36
@ashthespy Does the actual use case involve chopping off the head just once or multiple times?
Ash
@ashthespy
Mar 28 21:48
@tanriol Multiple times - it's a track list
Denis Lisov
@tanriol
Mar 28 22:42
A track list is likely not too long, modified not often and irregularly, so I'd just leave it as a Vec without additional tricks.