These are chat archives for rust-lang/rust

27th
Nov 2016
David Harvey-Macaulay
@alteous
Nov 27 2016 15:13

Hi, I'd like to be able to take a 1-element "slice" of a data field without using a [T; 1] or a Vec<T>. This is what I would like to do in theory:

struct Data {
    // Non-copyable field
    foo: String,
}

let data = Data { foo: "bar".to_string() };
let slice_of_one_item: &[String] = &data.foo;

I'm currently just using 1-element arrays to do this, but the problem is the array contents are not copyable, which leads to this problem. Could this be done a better way in safe Rust?

Edit: Updated the link

Jonas Platte
@jplatte
Nov 27 2016 15:26
@Alteous Why do you want that?
David Harvey-Macaulay
@alteous
Nov 27 2016 15:49
@jplatte For convenience. I like to write functions that process slices of data at a time, rather than one bit of data at a time.
Jonas Platte
@jplatte
Nov 27 2016 15:52
Okay, but why would you change your struct for that? Why not simply have the noncopyable thing as foo.thing: NonCopyable instead of foo.array: [NonCopyable; 1] and call bar as bar(&[foo.thing])?
David Harvey-Macaulay
@alteous
Nov 27 2016 16:14
@jplatte Sorry I don't understand, could you explain with some code?
David Harvey-Macaulay
@alteous
Nov 27 2016 16:19
Ahh, like this? That's a lot better.
Jonas Platte
@jplatte
Nov 27 2016 16:35
Hmm, you probably should use a trait instead of &[NonCopyable] then.
David Harvey-Macaulay
@alteous
Nov 27 2016 16:37
Maybe I'm missing the point here regarding slices. I thought they were like references but for contiguous data?
Jonas Platte
@jplatte
Nov 27 2016 16:42
Hmm... I'm not 100% sure if there could be a type-safe way to get a &[NonCopyable] from a &NonCopyable
David Harvey-Macaulay
@alteous
Nov 27 2016 16:42

Would that not incur dynamic dispatch? The point is (although I might be prematurely optimising here), I want to prefer

fn process(data: &[Data]) {
    // ...
}

fn main() {
    let data = [1, 2, 3, ...];
    process(&data);
}

to

fn process(data: &Data) {
    // ...
}

fn main() {
    let data = [1, 2, 3, ...];
    for datum in &data {
        process(&datum);
    }
}

to avoid the function call over head.

Inspiration is, of course, from Mike Acton's infamous talk.
Jonas Platte
@jplatte
Nov 27 2016 16:55
No, that wouldn't incur dynamic dispatch. I'm reading up on rust-lang/rust#27774 right now, seems like a function from &T to &[T] was actually in the stdlib at some point (though always as unstable)
David Harvey-Macaulay
@alteous
Nov 27 2016 17:41
@jplatte This is perfect, thanks!
I wonder why it was removed