These are chat archives for rust-lang/rust

4th
Nov 2016
David McGillicuddy
@djmcgill
Nov 04 2016 17:05
Bit of a weird question: I have a test that I would like to ensure never accesses certain indices in (the middle of) an array. Can I "booby trap" an array element such that it panics when read from? Uninitialised memory is just undefined behaviour right, rather than panicing when read even in test?
Diggory Blake
@Diggsey
Nov 04 2016 17:06
@djmcgill You could wrap your array in a proxy type that intercepts the indexing
Michael Howell
@notriddle
Nov 04 2016 17:06
Uninitialized memory is UB, yes.
Flavius Aspra
@flavius
Nov 04 2016 17:07
Does rust have unitialized memory? (except, I imagine, unsafe rust)
Diggory Blake
@Diggsey
Nov 04 2016 17:07
not in the safe subset
Michael Howell
@notriddle
Nov 04 2016 17:07
Can you not just make it an array of Options?
David McGillicuddy
@djmcgill
Nov 04 2016 17:08
Basically I have an array of uninitialised memory of type &mut [Instance] and I have a function that fills it. Instance is just a struct.
pub fn fill_instances(&self, instances: &mut [Instance]) -> usize
Diggory Blake
@Diggsey
Nov 04 2016 17:09
you can't have uninitialized memory in safe rust, so there's no way that function can be safe
even if it only writes to the array, it will try to drop the elements being overwritten first
David McGillicuddy
@djmcgill
Nov 04 2016 17:10
It gets the array from the gfx create_buffer_persistent
maybe it's not uninitialised
I'm trying to browse the source, but I guess I'll ask in gfx-rs
Michael Howell
@notriddle
Nov 04 2016 17:16
Actually, uninitialized memory can exist in safe Rust. You just can't read it, create references to it, or pass it to another function. This works:
let x;
// x is uninitialized here, so this line wouldn't compile
// println!("{}", x);
x = 2;
// now I can print it
println!("{}", x);
Maciej Hirsz
@maciejhirsz
Nov 04 2016 17:25
Vec technically allows you to have the benefits on uninitialized memory, but I don’t think there is an equivalent of it that would work on stack instead of heap
not in safe rust that is