Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some wrapper for resources (e.g. pixmaps) that frees them on drop (free_pixmap()) #78

Closed
psychon opened this issue Nov 16, 2019 · 2 comments · Fixed by #629
Closed
Labels
enhancement New feature or request good first issue Good for newcomers P3 Priority Middle

Comments

@psychon
Copy link
Owner

psychon commented Nov 16, 2019

I end up writing something like the following in most of the examples:

x11rb/examples/xeyes.rs

Lines 151 to 156 in 52b21fa

struct FreePixmap<'c, C: Connection>(&'c C, PIXMAP);
impl<C: Connection> Drop for FreePixmap<'_, C> {
fn drop(&mut self) {
self.0.free_pixmap(self.1).unwrap();
}
}

A proper implementation would add Deref, I guess. And some wrappers around things like CreatePixmap, although that is harder to do automatically...

@psychon psychon added enhancement New feature or request good first issue Good for newcomers labels Nov 16, 2019
@psychon
Copy link
Owner Author

psychon commented Dec 4, 2019

I gave this a go and I am not too happy about the necessary amount of code, not about the unwrap(). Perhaps this can be simplified with a macro? How "easily usable" would the result be?

// CreateWindow/DestroyWindow
// OpenFont/CloseFont
// CreatePixmap/FreePixmap
// CreateGC/FreeGC
// CreateColormap/FreeColormap
// CreateCursor/CreateGlyphCursor/FreeCursor
//
// Plus the same for all the extensions that are out there.

/// TODO
#[derive(Debug)]
pub struct FreePixmap<'c, C: Connection>(&'c C, u32);

impl<'c, C: Connection> FreePixmap<'c, C>
{
    /// TODO
    pub fn for_pixmap(conn: &'c C, pixmap: PIXMAP) -> Self
    {
        FreePixmap(conn, pixmap)
    }

    /// TODO
    pub fn create_pixmap(conn: &'c C, depth: u8, drawable: u32, width: u16, height: u16) -> Result<Self, ConnectionError> {
        let pixmap = conn.generate_id();
        drop(conn.create_pixmap(depth, pixmap, drawable, width, height)?);
        Ok(Self::for_pixmap(conn, pixmap))
    }

    pub fn pixmap(&self) -> PIXMAP {
        self.1
    }
}

impl<C: Connection> Deref for FreePixmap<'_, C>
{
    type Target = PIXMAP;

    fn deref(&self) -> &PIXMAP {
        &self.1
    }
}

impl<C: Connection> Drop for FreePixmap<'_, C>
{
    fn drop(&mut self) {
        drop(self.0.free_pixmap(self.1).unwrap());
    }
}

@psychon psychon added the P3 Priority Middle label Mar 10, 2020
psychon added a commit that referenced this issue Jun 4, 2021
This commits adds structs that e.g. wrap a window. When this struct is
dropped, the contained window is destroyed.

Such a wrapper is added for each kind of resource in the core protocol.
Extensions are not (yet?) handled.

Implements-most-of: #78
Signed-off-by: Uli Schlachter <psychon@znc.in>
@psychon
Copy link
Owner Author

psychon commented Jun 6, 2021

#620 implemented the xproto part of this. For this issue, the remaining pieces are:

  • Also add wrapper objects for all the various extension objects.
  • Instead of hand-writing all of this code, integrate this into the code generator.

I think the second bullet point should be addressed before the first one. Hand-writing this stuff for xproto was already annoying enough. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers P3 Priority Middle
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant