dstify 0.1.1

safe construction of custom dynamically-sized types (DSTs)
Documentation

DSTify   crate docs

Rust crate for safe construction of custom dynamically-sized types (DSTs). Heavily inspired by slice-dst.

use dstify::Dstify;
use std::{fs::File, io, path::Path};

#[derive(Dstify, Debug)]
#[repr(C)]
struct FileWithPath {
    file: File,
    path: Path, // DST
}

impl FileWithPath {
    pub fn open<P: AsRef<Path>>(path: P) -> io::Result<Box<Self>> {
        let path = path.as_ref();
        let file = File::open(path)?;
        // call the method generated by `Dstify` proc macro
        Ok(FileWithPath::init_unsized(file, path))
    }
    pub fn path(&self) -> &Path {
        &self.path
    }
    pub fn file(&self) -> &File {
        &self.file
    }
}
fn main() {
    let named_file = FileWithPath::open("Cargo.toml")?;
    // type of `named_file` is `Box<FileWithPath>`
    println!("{named_file:?}");
    // reference to `FileWithPath` is a wide (fat) pointer
    assert_eq!(size_of_val(&named_file), 16);
}

note: dyn Trait DSTs are not yet supported.

Motivation

In some, arguably niche, use-cases it's more convenient to use a slice instead of a reference. Using a slice removes the need for a lifetime annotation and saves one pointer indirection. Creating a reference (wide/fat pointer) to a built-in type like str or [u8] is easy, but it becomes extremely hard and error-prone once we'd like to store additional fields.

From dynamically sized types in rust nomicon:

Currently the only properly supported way to create a custom DST is by making your type generic and performing an unsizing coercion

Another possiblity is to manually allocate the memory with proper size and alignment and emplace the field values at correct offsets.
That's what is happening behind the curtains of dstify.