I'm looking through some old (~2014) Rust code and I'm seeing this code block:
fn compile(self, func:&UncompiledFunction<'a>) -> &'a Val {
unsafe {
use std::raw::Repr;
use std::mem::transmute as cast;
let slice = self.repr();
let ty = <&'a str as Compile<'a>>::get_type();
let structure = Val::new(func, &ty);
let offset_data = cast::<_, usize>(&slice.data) - cast::<_, usize>(&slice);
let offset_len = cast::<_, usize>(&slice.len) - cast::<_, usize>(&slice);
func.insn_store_relative(structure, offset_data, func.insn_of(mem::transmute::<_, isize>(slice.data)));
func.insn_store_relative(structure, offset_len, func.insn_of(slice.len));
structure
}
}
According to the docs and this GitHub discussion std::raw::Repr
and std::raw::Slice
have been deprecated in favor of std::slice
functions.
As someone with only a beginner's understanding of the std library I'm unsure how to translate these particular lines from the above block:
let slice = self.repr(); // `self` here is a `static str`
let offset_data = cast::<_, usize>(&slice.data) - cast::<_, usize>(&slice);
let offset_len = cast::<_, usize>(&slice.len) - cast::<_, usize>(&slice);
I was looking through the documentation for Repr
with the hopes that I could produce an analogy with some function in the std::slice
family, but nothing is immediately clear to me.
I'm hoping someone can explain to me what exactly Repr
does (in different language) and what a more updated approach might be.