Skip to content

Mapping lifetimes prevent use within self-contained structs #8149

@cwfitzgerald

Description

@cwfitzgerald

I had the use case today where I wanted to build an "InstanceBuilder" where the user of this api incrementally added structs to this builder, and as I went along I wrote those structs into a staging buffer, which gets uploaded when this builder is finished. What I ended up with is two structs that don't compile as they're self referential.

struct InstanceBuilderOne {
    instance_data_buffer: wgpu::Buffer,
    instance_mapping: wgpu::QueueWriteBufferView<'self>
}

struct InstanceBuilderTwo {
    instance_data_buffer: wgpu::Buffer,
    instance_mapping: wgpu::BufferViewMut<'self>,
}

I think the usability increase from getting rid of these lifetimes (and cloning the underlying resources, now that that is an option) is worth the small cost of cloning the resources. The mapping functions themselves shouldn't be very hot.

Workaround

For those who want a workaround, I ended up splitting up my instance builder into two structs, where the user made the builder, then you made a writer, which included these references in them

struct InstanceBuilderThree {
    instance_data_buffer: wgpu::Buffer,
}

impl InstanceBuilderThree {
    fn write(&self) -> InstanceBuilderThreeWriter<'_>;
}

struct InstanceBuilderThreeWriter<'a> {
    inner: &'a InstanceBuilderThree 
    instance_mapping: wgpu::BufferViewMut<'a>,
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions