logo
pub fn from_fn<P, F, C, Fut, Out, Err>(
    protocol_name: P,
    fun: F
) -> FromFnUpgrade<P, F> where
    P: ProtocolName + Clone,
    F: FnOnce(C, Endpoint) -> Fut,
    Fut: Future<Output = Result<Out, Err>>, 
Expand description

Initializes a new FromFnUpgrade.

Example

let _transport = MemoryTransport::default()
    .and_then(move |out, cp| {
        upgrade::apply(out, upgrade::from_fn("/foo/1", move |mut sock, endpoint| async move {
            if endpoint.is_dialer() {
                upgrade::write_one(&mut sock, "some handshake data").await?;
            } else {
                let handshake_data = upgrade::read_one(&mut sock, 1024).await?;
                if handshake_data != b"some handshake data" {
                    return Err(upgrade::ReadOneError::from(io::Error::from(io::ErrorKind::Other)));
                }
            }
            Ok(sock)
        }), cp, upgrade::Version::V1)
    });