I have written the following code:

tag debug_metadata {
    file_metadata(@metadata<file_md>);
    compile_unit_metadata(@metadata<compile_unit_md>);
    subprogram_metadata(@metadata<subprogram_md>);
}

fn md_from_metadata<T>(val: debug_metadata) -> T unsafe {
    alt val {
      file_metadata(md) { unsafe::reinterpret_cast(md) }
      compile_unit_metadata(md) { unsafe::reinterpret_cast(md) }
      subprogram_metadata(md) { unsafe::reinterpret_cast(md) }
    }
}

Assume that I know precisely what type I am extracting at any given
point when I call md_from_metadata, so I call the specific typed
version that gives me the correct output (ie. I am never actually
casting a value to the incorrect type). My Principles of Software
Engineering prof would surely call this "bad zen", but using
md_from_metadata in this way makes the calling code noticeably cleaner
in my eyes. Are there any safety concerns that come with using
reinterpret_cast in this way, or is the code simply a harmless hack?

Cheers,
Josh
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to