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

Guidance on TcpConnect and self-referential structs #101

Open
richardjlyon opened this issue Dec 24, 2023 · 0 comments
Open

Guidance on TcpConnect and self-referential structs #101

richardjlyon opened this issue Dec 24, 2023 · 0 comments

Comments

@richardjlyon
Copy link

richardjlyon commented Dec 24, 2023

Hi!

I am using the async TcpConnect. I have a struct that owns both the connector, and at most one socket. This looks a little like so:

struct MyClient<T: TcpConnect> {
    connector: T,
    connection: Option<T::Connection<'?>>,
}

impl<T: TcpConnect> MyClient<T> {
    fn connect(&mut self) {
        self.connection = self.connector.connect(remote); //  lifetime that matches the function
    }
}

The issue arises with lifetimes. T::Connection takes on a lifetime that must be less than that of the connector meaning (I think) that as far as the borrow checker is concerned, this is a self-referential struct.

If that is not the case, then I wonder what the lifetime parameter of T::Connection should be. If I give it one (say a) then the reference to self in connect must also receive the annotation which screws up borrowing in any code that tries to use it.

Is there a way to store T::Connection in this manner?

If the conceptual above is not clear enough, I can put an example together.

For context, I am porting some code that used TcpClientStack to have an async interface. If this is not supported (ie I am not just being dense) then I will just refactor to clean things up; this seemed like the path of least resistance from TcpClientStack::TcpSocket

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