Skip to content

Exposing stride and integration with imgref #291

@madsmtm

Description

@madsmtm

We currently expose a Buffer<'_> with .width(), .height() and DerefMut<Target = [u32]>, but for implementing zero-copy, we need to expose a .stride() method as well, see #95. And while we're doing that, it'd be nice to add some helper methods like .rows() to make iteration a bit less cumbersome.

Surprise surprise, there's already a library for this! It's called imgref.

Naively, we'd like to just add impl<'a> DerefMut<Target = Img<&'a mut [u32]> for Buffer<'a, ...>, but that unfortunately won't work, since the way Deref/DerefMut is designed they require that we can actually produce a reference to them, and this can be somewhat difficult in this case since Img takes control over how the pointer is stored.

For example, consider WaylandBuffer as currently implemented:

struct WaylandBuffer {
    map: MmapMut,
    pub width: i32,
    pub height: i32,
    // ... Other fields.
}

impl Deref for WaylandBuffer {
    type Target = Img<&'unclear_lifetime mut [u32]>;
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut Img::new(self.map.as_mut(), self.width, self.height)
        // Won't work, the reference escapes the context.
    }
}

As far as I can tell, there isn't really a way to resolve this, at least not without larger changes to either Softbuffer or imgref?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions