Over the Thanksgiving break I’ve been devoting a lot of time to thinking about DST and Rust’s approach to vector and object types. As before, this is very much still churning in my mind so I’m just going to toss out some semi-structured thoughts.
Treating vectors like any other container. Some time back, I wrote up a post about how we could treat vectors like any other container, which would (to some extent) avoid the need for DST.
Dynamically sized types (DST). In Part 1 of the series, I
sketched out how “Dynamically Sized Types” might work. In that scheme,
[T] is interpreted as an existential type like
exists N. [T, ..N], and
Trait is interpreted as
T:Trait. T. The type system ensures that DSTs always appear behind
one of the builtin pointer types, and those pointer types become fat
- Advantage. Impls for objects and vectors work really well.
- Disadvantage. Hard to square with user-defined smart pointers
RC<[int]>. The problem is worse than I presented in that post, I’ll elaborate a bit more.
Statically sized types (SST). In Part 2 of the series, I
sketched out an alternative scheme that I later dubbed “Statically
Sized Types”. In this scheme, in some ways similar to today,
Trait are not themselves types, but rather shorthands for
existential types where the
exists qualifier is moved outside the
smart pointer. For example,
exists N. ~[T, ..N]. The
scheme does not involve fat pointers; rather, the existential type
carries the length, and the thin pointer is embedded within the
- Advantage. It is easy to create a type like
RC<[int]>from an existing
RC<[int, ..N]>(and, similarly, an
RC<Trait>from an existing
- Disadvantage. Incompatible with monomorphization except via virtual calls. I described part of the problem in Part 3 of the series. I’ll elaborate a bit more here.
Where does that leave us?
So, basically, we are left with two flawed schemes. In this post I just want to elaborate on some of the thoughts I had over Thanksgiving. Roughly speaking they are three:
- DST and smart pointer interaction is even less smooth than I thought,
but workable for
- SSTs, vectors, and smart pointers are just plain unworkable.
- SSTs, objects, and smart pointers work out reasonable well.
At the end, I suggest two plausible solutions that seem workable to me at this point:
- DST, after elaborating more examples to see whether they work;
- Treat vectors like any other container combined with SST for object types.