Skip to content

Guidance on TcpConnect and self-referential structs #101

@richardjlyon

Description

@richardjlyon

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

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