Forms a (possibly-wide) raw pointer from a data address and metadata.?

How to understand the content of the red area in the figure below, what is the relationship between const_ptr and data_address, how is const_ptr calculated, I have checked the rust source code and did not find.

They're using an union to construct a "fat pointer" out of its piece. For example, a &[u8] slice is a fat pointer consisting of a pointer to the first element (the data_address) and the number of elements in the slice (the metadata).

You can read the ptr-meta RFC for a better explanation.

I have finished reading the link you recommended, but I still don't quite understand how const_ptr is calculated in the above diagram, can you explain it in detail?

The PtrRepr type is a union.

#[repr(C)]
union PtrRepr<T: ?Sized> {
    const_ptr: *const T,
    mut_ptr: *mut T,
    components: PtrComponents<T>,
}

What they're doing is setting the components field to contain the data_address and metadata, then reading it out as a *const T. It's one way to reinterpret ("transmute") a type as something else when you know its layout.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.