Skip to content

Extract Tree api into deref target #299

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

Closed
molpopgen opened this issue Jul 24, 2022 · 1 comment
Closed

Extract Tree api into deref target #299

molpopgen opened this issue Jul 24, 2022 · 1 comment

Comments

@molpopgen
Copy link
Member

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.

@molpopgen
Copy link
Member Author

Closed by #300

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant