Skip to content

Commit

Permalink
ngrok: add IntoFuture impls for builders
Browse files Browse the repository at this point in the history
  • Loading branch information
jrobsonchase committed Feb 28, 2024
1 parent 12efb0d commit 6f167a0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
1 change: 0 additions & 1 deletion ngrok/examples/mingrok.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ async fn main() -> Result<(), Error> {
info!(?req, "received update command");
Err("unable to update".into())
})
.connect()
.await?
.http_endpoint()
.listen_and_forward(forwards_to.clone())
Expand Down
10 changes: 10 additions & 0 deletions ngrok/src/config/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ macro_rules! impl_builder {
$crate::forwarder::forward(tunnel, info, to_url)
}
}

impl std::future::IntoFuture for $name {
type IntoFuture = std::pin::Pin<Box<dyn std::future::Future<Output = Self::Output>>>;
type Output = Result<$tun, RpcError>;

fn into_future(self) -> Self::IntoFuture {
use futures::FutureExt;
async move { self.listen().await }.boxed()
}
}
}
};
}
Expand Down
26 changes: 26 additions & 0 deletions ngrok/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{
VecDeque,
},
env,
future::IntoFuture,
io,
sync::{
atomic::{
Expand All @@ -19,6 +20,7 @@ use arc_swap::ArcSwap;
use async_trait::async_trait;
use bytes::Bytes;
use futures::{
future::BoxFuture,
prelude::*,
FutureExt,
};
Expand Down Expand Up @@ -333,6 +335,30 @@ pub struct SessionBuilder {
id: Option<String>,
}

impl<'a> IntoFuture for &'a mut SessionBuilder {
type IntoFuture = BoxFuture<'a, Result<Session, ConnectError>>;
type Output = Result<Session, ConnectError>;
fn into_future(self) -> Self::IntoFuture {
(&*self).into_future()
}
}

impl<'a> IntoFuture for &'a SessionBuilder {
type IntoFuture = BoxFuture<'a, Result<Session, ConnectError>>;
type Output = Result<Session, ConnectError>;
fn into_future(self) -> Self::IntoFuture {
async move { self.connect().await }.boxed()
}
}

impl IntoFuture for SessionBuilder {
type IntoFuture = BoxFuture<'static, Result<Session, ConnectError>>;
type Output = Result<Session, ConnectError>;
fn into_future(self) -> Self::IntoFuture {
async move { self.connect().await }.boxed()
}
}

/// Errors arising at [SessionBuilder::connect] time.
#[derive(Error, Debug)]
#[non_exhaustive]
Expand Down

0 comments on commit 6f167a0

Please sign in to comment.