Skip to content

Revisit Transport trait generics #101

@thedevbirb

Description

@thedevbirb

After #97, or together with it.

The trait Transport provides an Address has a generic rather than associated type. As such, when defining a ReqSocket param in a function we have to specify for example ReqSocket<Tcp, SocketAddr> while it's obvious the second generic should be SocketAddr. Moreover, given the trait and the transport are both defined in this library, consu mers of it are not able to swap the Address as they wish.

Perhaps we should change it to an associated type, that is instead of:

/// A trait for address types that can be used by any transport.
pub trait Address: Clone + Debug + Send + Sync + Unpin + Hash + Eq + 'static {}

pub trait Transport<A: Address> { /* ... */ }

doing

/// A trait for address types that can be used by any transport.
pub trait Address: Clone + Debug + Send + Sync + Unpin + Hash + Eq + 'static {}

pub trait Transport {
    type Address: Address;
	/* ... */
}

Then, we should make some changes also to adapt some helpers, for example:

impl<T> ReqSocket<T>
where
    T: Transport<Address = SocketAddr> + Send + Sync + Unpin + 'static,
{
    /// Connects to the target address with the default options.
    pub async fn connect(&mut self, addr: impl ToSocketAddrs) -> Result<(), ReqError> {
        let mut addrs = lookup_host(addr).await?;
        let endpoint = addrs.next().ok_or(ReqError::NoValidEndpoints)?;

        self.try_connect(endpoint).await
    }
}

Lastly, we should be aware of breaking changes. The Address trait has been added in #76, which is a recommended read.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions