Repository: actix/actix-web
File: src/extract.rs (Rust)

Code before:
        ok(req.method().clone())
    }
}

impl FromRequest for ConnectionInfo {
    type Error = Infallible;
    type Future = Ready<Result<ConnectionInfo, Infallible>>;
    type Config = ();

    fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
        ok(req.connection_info().clone())
    }
}

#[derive(Debug)]
struct MissingPeerAddr;

impl fmt::Display for MissingPeerAddr {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Missing peer address.")
    }
}

impl ResponseError for MissingPeerAddr {}

impl FromRequest for SocketAddr {
    type Error = Error;
    type Future = Ready<Result<SocketAddr, Error>>;
    type Config = ();

    fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
        match req.peer_addr() {
            Some(addr) => ok(addr),
            None => {
                log::error!("Missing peer address.");
                err(MissingPeerAddr.into())
            }
        }
    }
}

#[doc(hidden)]
impl FromRequest for () {
    type Error = Infallible;
    type Future = Ready<Result<(), Infallible>>;
    type Config = ();

    fn from_request(_: &HttpRequest, _: &mut Payload) -> Self::Future {
        ok(())
    }
}

Review comment (suggestion):
I think i'd prefer a newtype struct for this `PeerAddr(SocketAddr)` since it _could_ be non-obvious what socket addr this refers to. Plus in general, it's easier to find these sorts of things in docs.

Code after:
/// ```
impl FromRequest for Method {
    type Error = Infallible;
    type Future = Ready<Result<Self, Self::Error>>;
    type Config = ();

    fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
        ok(req.method().clone())
    }
}

#[doc(hidden)]
impl FromRequest for () {
    type Error = Infallible;
    type Future = Ready<Result<(), Infallible>>;
    type Config = ();

    fn from_request(_: &HttpRequest, _: &mut Payload) -> Self::Future {
        ok(())
    }
}

macro_rules! tuple_from_req ({$fut_type:ident, $(($n:tt, $T:ident)),+} => {

    // This module is a trick to get around the inability of
    // `macro_rules!` macros to make new idents. We want to make
    // a new `FutWrapper` struct for each distinct invocation of
    // this macro. Ideally, we would name it something like
    // `FutWrapper_$fut_type`, but this can't be done in a macro_rules
    // macro.
    //
    // Instead, we put everything in a module named `$fut_type`, thus allowing
    // us to use the name `FutWrapper` without worrying about conflicts.
    // This macro only exists to generate trait impls for tuples - these
    // are inherently global, so users don't have to care about this
    // weird trick.
    #[allow(non_snake_case)]
    mod $fut_type {

        // Bring everything into scope, so we don't need
        // redundant imports
        use super::*;

        /// A helper struct to allow us to pin-project through
        /// to individual fields
        #[pin_project::pin_project]
        struct FutWrapper<$($T: FromRequest),+>($(#[pin] $T::Future),+);

        /// FromRequest implementation for tuple
        #[doc(hidden)]
        #[allow(unused_parens)]