These are chat archives for rust-lang/rust

5th
May 2016
mhsjlw
@mhsjlw
May 05 2016 10:52
Hi, how can I slice a vector from x to y
Say I have [1, 2, 3, 4, 5, 6, 7, 8]
and I want to get just 2 -> 5 as a vector
but don't actually remove it from the vector
Zakarum
@omni-viral
May 05 2016 11:07
Vec owns it's values
So you can only clone them to new vector
If that is OK than
let vec1 = vec![1, 2, 3, 4, 5, 6, 7, 8];
let vec2 = vec1[2..5].to_vec();
But you can use borrowed slice from original vector without cloning elements
let vec1 = vec![1, 2, 3, 4, 5, 6, 7, 8];
let the_slice = &vec1[2..5];
mhsjlw
@mhsjlw
May 05 2016 11:16
hmm
is let the_slice = &vec1[2..5]; elemets 2-5 of vec1?
or am I creating the_slice and setting to a value of 2,3,4,5
@SCareAngel ^
because what I need is something like this
let partial_bytes = bytes_vec.slice(cur_byte, total_bytes);
Zakarum
@omni-viral
May 05 2016 11:36
@mhsjlw &vec1[2..5] is elements of vec1. from index 2 to 5 (not inclusively)
let vec1 = vec!['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
let the_slice = &vec1[2..5];
for i in the_slice.iter() {
    print!("{}", i);
}
will output: "cde"
mhsjlw
@mhsjlw
May 05 2016 11:52
that's gonna be a performance hit :P
mhsjlw
@mhsjlw
May 05 2016 12:18
what does red mean when it is next to a function in the docs ?
ah
.slice is expirimental
im on the beta though hmm
what's a uint?
unsigned 32 bit integer ?
oh my mistake, I'm on someone else's docs... ?
pub fn send_world(&mut self, conn: &mut TcpStream){
        conn.send_level_init();

        let gb = self.gzip_world();
        let bytes = gb.as_slice();
        let total_bytes = bytes.len();
        let mut cur_byte: usize = 0;

        loop {
            if total_bytes - cur_byte > 1024 {
                let bytes_vec = bytes.to_vec();

                //let partial_bytes = bytes_vec.slice(cur_byte, cur_byte + 1024);
                let partial_bytes = &bytes_vec[cur_byte..(cur_byte + 1024)];
                println!("{:?}", partial_bytes.len());

                conn.send_chunk_data(1024, partial_bytes, ((cur_byte / total_bytes * 100) as u8));

                cur_byte += 1024;
            } else if total_bytes - cur_byte > 0 {
                let bytes_vec = bytes.to_vec();

                //let partial_bytes = bytes_vec.slice(cur_byte, total_bytes);
                let partial_bytes = &bytes_vec[cur_byte..total_bytes];
                println!("{:?}", partial_bytes.len());

                conn.send_chunk_data((total_bytes - cur_byte) as i16, partial_bytes, ((cur_byte / total_bytes * 100) as u8));

                cur_byte += total_bytes - cur_byte;
            } else{
                break;
            }
        }

        conn.send_level_finalize(self.x_size as i16, self.y_size as i16, self.z_size as i16);
    }
that's my code, but it isn't working the way i want it to
Erik Hedvall
@Ogeon
May 05 2016 12:22

Beta is just like stable, but the next version. No extra.

what's a uint?

That's the old name for usize
mhsjlw
@mhsjlw
May 05 2016 12:23
that makes sense
ok
yeah I need to slice a vec
but it's not working out :P
that function should go through a 1024 * 4096 vec (the entire world data) and slice it up into 1024 chunks, compress it, and pad the rest with 0x00
Erik Hedvall
@Ogeon
May 05 2016 12:24
what you want is something like this:
&my_vec[start..(start + slice_len)]
mhsjlw
@mhsjlw
May 05 2016 12:25
let partial_bytes = &bytes_vec[cur_byte..(cur_byte + 1024)];
and then to go right to the end
let partial_bytes = &bytes_vec[cur_byte..total_bytes];
that's for the last iteration
but it only has a length of 39
Erik Hedvall
@Ogeon
May 05 2016 12:25
Or just &bytes_vec[cur_byte..]
mhsjlw
@mhsjlw
May 05 2016 12:25
and it only sends once ?
it doesn't make sense
I just pushed my changes
im sure there is a lot of stuff in there that is wrong, forgive me :P
but yeah hmm
any ideas why it wouldn't slice properly?
or is my logic off (very possible)
Erik Hedvall
@Ogeon
May 05 2016 12:28
Your lengths and positions are usize, which is unsigned, so total_bytes - cur_byte > 0 doesn't make sense. It will just overflow and not happen.
Sorry, it will overflow and always happen
Check for total_bytes > cur_byte instead
Same for total_bytes - cur_byte > 1024. It should rather be cur_bytes + 1024 < total_bytes
That one is less critical, though
mhsjlw
@mhsjlw
May 05 2016 12:33
ok I'll give it a try when I can
Erik Hedvall
@Ogeon
May 05 2016 12:34
Also, this is unnecessary: let bytes_vec = bytes.to_vec(); :P Just slice bytes
mhsjlw
@mhsjlw
May 05 2016 16:05
thank you!
the map is now sent and the player spawns
Erik Hedvall
@Ogeon
May 05 2016 16:06
Nice!
This message was deleted
just a screenshot :D
if you change the block id (byte) it will display different blocks
now I will implement spawning other players when they log in
Erik Hedvall
@Ogeon
May 05 2016 16:10
:tada:
mhsjlw
@mhsjlw
May 05 2016 16:10
once I'm done that I'll probably do some more optimizations and maybe add world loading and configuration
Erik Hedvall
@Ogeon
May 05 2016 16:11
I love it when I'm writing something like a server, or something else that's basically a black box, and makes the first contact. So satisfying!
mhsjlw
@mhsjlw
May 05 2016 16:11
random generation in rust O.o that would be cool! I wonder how fast it is ...
@Ogeon yeah I know the feeling, it's awesome !
most of the stuff I do is exactly that, reverse engineering protocols and rewriting server-side and client side implementations
I thought why not give rust a try !
(I also wrote javascript bindings to a world generation library written in rust here: https://github.com/mhsjlw/node-voxel-worldgen)
it is super, super fast
much faster than expected
Erik Hedvall
@Ogeon
May 05 2016 16:15
Yeah, you can get some nice speeds with the XOR-shift RNG. I saw some other RNG being announced, the other day, that's supposedly faster. I wonder how good the results of that one is...
mhsjlw
@mhsjlw
May 05 2016 16:16
O.o gotta look into that :D
mhsjlw
@mhsjlw
May 05 2016 16:18
woah nice !
Erik Hedvall
@Ogeon
May 05 2016 16:18
I'm sure it's good enough for world generators
mhsjlw
@mhsjlw
May 05 2016 16:19
ill give it a try and let you know
Erik Hedvall
@Ogeon
May 05 2016 16:19
They don't need to be cryptographic, as far as I know :wink2:
Daniel Collin
@emoon
May 05 2016 18:23
Totally unrelated. Anyone knows if compiler plugins is going to be marked as stable at some point?
Erik Hedvall
@Ogeon
May 05 2016 21:02
Yes, eventually, as I understand it. I have seen ideas, together with the plans for a new macros-by-example system.