Skip to content

Cast to a DST pointer? #288

@joshlf

Description

@joshlf

This question is inspired by the slice_dst crate (cc @CAD97)

Is it valid, given a #[repr(C)] struct with a trailing slice field, to cast a slice fat pointer to a fat pointer to that struct? If so, is it guaranteed that the slice length will be retained as expected? Concretely:

#[repr(C)]
struct Foo {
    bar: u8,
    baz: [u16],
}

fn create_foo_ptr(base: *const u8, elems: usize) -> *const Foo {
    let slice_ptr = std::ptr::slice_from_raw_parts(base as *const u16, elems);
    slice_ptr as *const Foo // <-- Will this produce a `*const Foo` with length `elems` for the `baz` field?
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions