Skip to content

Encode retaining and releasing in types #83

@nox

Description

@nox

This is an idea I'm thinking about these days, we should be able to not retain things when using the get rule, instead we should be able to rely on lifetimes.

use std::ops::{Deref, Drop};

pub trait Release {
    unsafe fn release(&self);
}

pub struct Retained<T>(T) where T: Release;

impl<T: Release> Drop for Retained<T> {
    fn drop(&mut self) {
        unsafe {
            self.0.release();
        }
    }
}

enum Opaque {}
pub struct CFURL(Opaque);
pub struct CFURLRef(*const CFURL);

impl Release for CFURLRef {
    unsafe fn release(&self) {
        ...
    }
}

impl Deref for CFURLRef {
    type Target = CFURL;

    fn deref(&self) -> &CFURL {
        unsafe { &*self.0 }
    }
}

With additional traits for casting to supertype CFTypeRef, we should be able to provide functions going from &CFString to Retained<CFStringRef>.

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