From a2a64e510cedb20c454ac3ff098ff018ee8fced7 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sun, 9 Jun 2019 08:43:35 +0200 Subject: [PATCH] Store owned reactor Handles instead of references The lifetime parameter on builders make it harder to embed these in other contexts (in my case, a hyper builder). In hyper itself, reactor handles also seem to be stored as owned rather than as references, and since they are defined to contain an reference counted pointer, cloning should be cheap. --- quinn-h3/src/client.rs | 8 ++++---- quinn-h3/src/server.rs | 8 ++++---- quinn/src/builders.rs | 18 ++++++++---------- quinn/src/endpoint.rs | 2 +- 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/quinn-h3/src/client.rs b/quinn-h3/src/client.rs index b2935cd8b..7d030269a 100644 --- a/quinn-h3/src/client.rs +++ b/quinn-h3/src/client.rs @@ -22,14 +22,14 @@ use crate::{ try_take, Error, Settings, }; -pub struct ClientBuilder<'a> { - endpoint: EndpointBuilder<'a>, +pub struct ClientBuilder { + endpoint: EndpointBuilder, log: Option, settings: Settings, } -impl<'a> ClientBuilder<'a> { - pub fn new(endpoint: EndpointBuilder<'a>) -> Self { +impl ClientBuilder { + pub fn new(endpoint: EndpointBuilder) -> Self { Self { endpoint: endpoint, log: None, diff --git a/quinn-h3/src/server.rs b/quinn-h3/src/server.rs index 927fa790f..f77211813 100644 --- a/quinn-h3/src/server.rs +++ b/quinn-h3/src/server.rs @@ -21,14 +21,14 @@ use crate::{ try_take, Error, Settings, }; -pub struct ServerBuilder<'a> { - endpoint: EndpointBuilder<'a>, +pub struct ServerBuilder { + endpoint: EndpointBuilder, log: Option, settings: Settings, } -impl<'a> ServerBuilder<'a> { - pub fn new(endpoint: EndpointBuilder<'a>) -> Self { +impl ServerBuilder { + pub fn new(endpoint: EndpointBuilder) -> Self { Self { endpoint: endpoint, log: None, diff --git a/quinn/src/builders.rs b/quinn/src/builders.rs index c6dcca7b0..7bda92610 100644 --- a/quinn/src/builders.rs +++ b/quinn/src/builders.rs @@ -1,4 +1,3 @@ -use std::borrow::Cow; use std::io; use std::net::ToSocketAddrs; use std::str; @@ -14,8 +13,8 @@ use crate::endpoint::{Endpoint, EndpointDriver, EndpointRef, Incoming}; use crate::udp::UdpSocket; /// A helper for constructing an `Endpoint`. -pub struct EndpointBuilder<'a> { - reactor: Option<&'a tokio_reactor::Handle>, +pub struct EndpointBuilder { + reactor: Option, logger: Logger, server_config: Option, config: EndpointConfig, @@ -23,7 +22,7 @@ pub struct EndpointBuilder<'a> { } #[allow(missing_docs)] -impl<'a> EndpointBuilder<'a> { +impl EndpointBuilder { /// Start a builder with a specific initial low-level configuration. pub fn new(config: EndpointConfig) -> Self { Self { @@ -46,10 +45,9 @@ impl<'a> EndpointBuilder<'a> { self, socket: std::net::UdpSocket, ) -> Result<(EndpointDriver, Endpoint, Incoming), EndpointError> { - let reactor = match self.reactor { - Some(x) => Cow::Borrowed(x), - None => Cow::Owned(tokio_reactor::Handle::default()), - }; + let reactor = self + .reactor + .unwrap_or_else(|| tokio_reactor::Handle::default()); let addr = socket.local_addr().map_err(EndpointError::Socket)?; let socket = UdpSocket::from_std(socket, &reactor).map_err(EndpointError::Socket)?; let rc = EndpointRef::new( @@ -78,7 +76,7 @@ impl<'a> EndpointBuilder<'a> { self } - pub fn reactor(&mut self, handle: &'a tokio_reactor::Handle) -> &mut Self { + pub fn reactor(&mut self, handle: tokio_reactor::Handle) -> &mut Self { self.reactor = Some(handle); self } @@ -96,7 +94,7 @@ impl<'a> EndpointBuilder<'a> { } } -impl<'a> Default for EndpointBuilder<'a> { +impl Default for EndpointBuilder { fn default() -> Self { Self { reactor: None, diff --git a/quinn/src/endpoint.rs b/quinn/src/endpoint.rs index 9b9c838fe..a7454b7e5 100644 --- a/quinn/src/endpoint.rs +++ b/quinn/src/endpoint.rs @@ -33,7 +33,7 @@ pub struct Endpoint { impl Endpoint { /// Begin constructing an `Endpoint` - pub fn builder<'a>() -> EndpointBuilder<'a> { + pub fn builder() -> EndpointBuilder { EndpointBuilder::default() }