Hacker News new | more | comments | ask | show | jobs | submit login

So this is tricky.

The way custom DSTs work in Rust is super annoying at the moment, but slightly less annoying for generics.

You are free to define a custom DST that is like:

    struct Foo {
        header: u32,
        flexible: [SomeType]
However, there is no way to construct this type. The most you can do is calculate field offsets and write a ton of unsafe code.

If the type was instead

    struct Foo<T: ?Sized> {
        header: u32,
        flexible: T
you would be able to construct a `&Foo<[SomeType]>` from a `&Foo<[SomeType; N]>` via DST coercions.

This only works if you know the size of the array at compile time.

For a runtime sized thing you have to implement a bespoke vector-like thing. You can find an example of that code in https://github.com/servo/servo/blob/e19fefcb474ea6593a684a1c... where we have a generic "HeaderWithSlice<H, [T]>" type which can be heap allocated as a header followed by the flexible DST [T].

This could be improved. The HeaderWithSlice thing could probably be a useful crate for implementing completely-heap-allocated vectors (where the len/cap are on the heap) or shared reference counted types with flexible members. We haven't really split it out as a crate because we don't actually ever mutate it so the amount of code we need is significantly less (but it's not as useful as it could be).

So yeah, flexible array members can be implemented in Rust, but it's a lot of hacky work. It's an equivalent amount of work and unsafety to write a custom Index impl on a repr(C) type with a zero-length array at the end. The DST doesn't actually get you much here.

Applications are open for YC Summer 2019

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact