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

Lifetime doubt with NcPlane::new_child #17

Closed
CyanideForBreakfast opened this issue Mar 6, 2022 · 2 comments
Closed

Lifetime doubt with NcPlane::new_child #17

CyanideForBreakfast opened this issue Mar 6, 2022 · 2 comments

Comments

@CyanideForBreakfast
Copy link

CyanideForBreakfast commented Mar 6, 2022

Hi, before I ask, let me tell that I'm myself a beginner in Rust.

Doubt

NcPlane::new_child has the following signature -

pub fn new_child<'parent, 'plane, 'opts>(parent: &'parent mut NcPlane, options: &'opts NcPlaneOptions) ->
NcResult<&'plane mut NcPlane>

The lifetime 'plane is problematic here, in my opinion, as the compiler has no way to "tie" this to another existing lifetime (like 'opts or 'parent). This leads to assign the reference to the newly created child plane, an unknown lifetime. And because of this is it tricky to use this plane as a field of struct since constraining the reference with the lifetime constraints of the struct is not viable.

If I recall correctly, all children planes would be destroyed on destruction of parent and hence it makes sense to constraint the child reference to live as long as parent ('parent lifetime), instead of an arbitrary lifetime ('plane).

So, I want to ask how should I go about it? Because I'm using a child plane as a field in a struct, something along the lines of -

struct Widget<'a> {
  plane: &'a mut NcPlane
  ... other fields
}

And assigning Widget { plane : NcPlane::new_child(parent_plane, ...) } just leads to compilation complications as we nest structs and all.
Am I doing something wrong?

@joseluis
Copy link
Collaborator

joseluis commented Mar 6, 2022

Hi, welcome,

This library doesn't implement the Drop trait, you'll have to call the destructor methods, ideally by implementing Drop in the wrapping structure that owns the notcuses state.

Wrapping everything in Rust owned types would make this library too different from the way the C API can be used, and this library tries to remain close to that. But there's anoher more rusty library planned, once this one is a bit more mature.

I'm glad you're trying to build something with it, real cases are the best tests.

@CyanideForBreakfast
Copy link
Author

CyanideForBreakfast commented Mar 7, 2022

Thanks for the clarification. I'll implement Drop for my use case.

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

2 participants