Continuing the discussion from [Pre-RFC] Custom DSTs:
As promised here’s another take of custom DST, specified via reducing to a built-in one.
struct MatMeta {
width: usize,
height: usize,
stride: usize,
}
// Define the DST
dyn type Mat<T>(regular: MatMeta) = [T];
impl<T> dst::Regular for Mat<T> {
// Converts the metadata of the DST's to that of [T]'s.
fn reduce_with_metadata(meta: MatMeta) -> usize {
if meta.height == 0 {
0
} else {
(meta.height - 1) * meta.stride + meta.width
}
}
}
Unlike all previous attempts which requires user to provide size_of_val and align_of_val, I think this approach is more suitable for Rust, since
- it automatically handles
Drop
- it automatically handles
Send and Sync
- it needs less
unsafe
Also, from the experience of impl Trait for .. ↦ auto trait Trait, I don’t think separating the metadata of the DST into an impl block is a good idea (only japaric’s draft attach the metadata to the type, every other proposal so far provides the metadata as an associated type of a Referent/DynSized trait
).