Skip to content

Meaningless pin! in wgpu_hal::dx12 #8534

@jimblandy

Description

@jimblandy

In #8495 (comment) @Wumpf asked @inner-daemons to pin an ArrayVec because we're going to take a pointer to it, resulting in this code:

        let mut view_instancing =
            core::pin::pin!(ArrayVec::<Direct3D12::D3D12_VIEW_INSTANCE_LOCATION, 32>::new());

However, this doesn't help and isn't necessary.

"Doesn't help": like most types, ArrayVec<T> implements Unpin if T does, meaning that Pin<ArrayVec<T>> is just an ordinary newtype, with no unusual effect. You could simply use Pin::get_mut to get a mutable reference to the ArrayVec and then use std::mem::replace to swap out a different one and drop the original.

"Isn't necessary": You can actually do what you intend here without using pin, using ordinary Rust:

  1. Before building the D3D12_VIEW_INSTANCING_DESC, declare a local variable that borrows a shared reference to view_instancing.

  2. Use this reference directly to initialize pViewInstanceLocations, since &T converts to *const T automatically.

  3. Say drop(<reference var>); after you're done using the pointer. This ensures that the shared reference lives this long.

As long as view_instancing is borrowed for sharing, it can't be moved.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions