Re: [PATCH v2 10/10] ruby: new notmuch_rb_object_destroy() helper

Subject: Re: [PATCH v2 10/10] ruby: new notmuch_rb_object_destroy() helper

Date: Wed, 12 May 2021 21:31:28 -0500

To: David Bremner

Cc: notmuch@notmuchmail.org

From: Felipe Contreras


On Wed, May 12, 2021 at 5:10 PM David Bremner <david@tethera.net> wrote:
>
> Felipe Contreras <felipe.contreras@gmail.com> writes:
>
> > +static inline notmuch_status_t
> > +notmuch_rb_object_destroy (VALUE rb_object, const rb_data_type_t *type)
> > +{
> > +    void *nm_object;
> > +    notmuch_status_t ret;
> > +
> > +    Data_Get_Notmuch_Object (rb_object, type, nm_object);
> > +
> > +    ret = ((notmuch_status_t (*)(void *)) type->data) (nm_object);
> > +    DATA_PTR (rb_object) = NULL;
> > +
> > +    return ret;
> > +}
> > +
>
> I see the benefit of making the code shorter, but I don't understand the
> new code, while I did (mostly) understand the old code. So please
> explain this in a comment as if I have no idea how ruby extensions work.

The struct used to store the types (rb_data_type_t) contains a "data"
field where we can store whatever we want. I use that field to store a
pointer to the corresponding destroy function. For example
notmuch_rb_database_type contains a pointer to
notmuch_database_destroy.

I cast that pointer as a notmuch_status_t (func*)(void *) and call
that function passing the internal object (e.g. notmuch_database_t).

Is that explanation good enough?

Cheers.

-- 
Felipe Contreras
_______________________________________________
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-leave@notmuchmail.org

Thread: