Skip to content

Extract Tree api into deref target #299

Closed
@molpopgen

Description

@molpopgen

Work going on over in #296 risks code duplication.
To eliminate this, we need to separate out the public
API of a tree from its storage requirements.

Roughly, something like this should do the trick:

// Gets the public API
struct TreeApi {
    tree: NonNull<ll_bindings::tsk_tree_t>,
}

// Worries about storage, Drop, etc..
struct TreeTest {
    tree: ll_bindings::tsk_tree_t,
    api: TreeApi,
}

impl TreeTest {
    fn new(ts: TreeSequence) -> Self {
        let mut m = MaybeUninit::<ll_bindings::tsk_tree_t>::uninit();
        let rv = unsafe { ll_bindings::tsk_tree_init(m.as_mut_ptr(), ts.as_ptr(), 0) };
        assert_eq!(rv, 0);
        let mut tree = unsafe { m.assume_init() };
        let tp = &mut tree as *mut ll_bindings::tsk_tree_t;

        Self {
            tree,
            api: TreeApi {
                tree: unsafe { NonNull::new_unchecked(tp) },
            },
        }
    }
}

Once done, we can have separate owning/non-owning Tree types.

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