Partial rewrite to utilize impl Future in return position:

Changed return types relevant methods of `conn` module; no implementation changes;

Rewrote relevant methods of `reactor` and `client`.
This commit is contained in:
Ratys 2018-06-08 17:22:43 +03:00
parent b5a575966f
commit bfb8bf5a1b
3 changed files with 37 additions and 15 deletions

View file

@ -2,6 +2,7 @@
use std::fs::File;
use std::fmt;
use std::io::Read;
use std::net::SocketAddr;
use encoding::EncoderTrap;
use encoding::label::encoding_from_whatwg_label;
@ -119,7 +120,7 @@ impl<'a> Future for ConnectionFuture<'a> {
impl Connection {
/// Creates a new `Connection` using the specified `Config` and `Handle`.
pub fn new<'a>(config: &'a Config) -> error::Result<ConnectionFuture<'a>> {
pub fn new<'a>(config: &'a Config) -> impl Future<Item = Connection, Error = error::IrcError> {
if config.use_mock_connection() {
Ok(ConnectionFuture::Mock(config))
} else if config.use_ssl() {

View file

@ -754,7 +754,7 @@ impl IrcClient {
// will instead panic.
let _ = thread::spawn(move || {
let mut reactor = Core::new().unwrap();
let conn = reactor.run(Connection::new(&cfg).unwrap()).unwrap();
let conn = reactor.run(Connection::new(&cfg)).unwrap();
tx_view.send(conn.log_view()).unwrap();
let (sink, stream) = conn.split();
@ -817,15 +817,28 @@ impl IrcClient {
/// # }
/// # fn process_msg(server: &IrcClient, message: Message) -> error::Result<()> { Ok(()) }
/// ```
pub fn new_future(config: &Config) -> error::Result<IrcClientFuture> {
let (tx_outgoing, rx_outgoing) = mpsc::unbounded();
Ok(IrcClientFuture {
conn: Connection::new(config)?,
config,
tx_outgoing: Some(tx_outgoing),
rx_outgoing: Some(rx_outgoing),
})
pub fn new_future(config: &Config) -> impl Future<
Item = (IrcClient, impl Future<Item = (), Error = error::IrcError> + 'static),
Error = error::IrcError
> {
Connection::new(config)
.and_then(|connection| {
let (tx_outgoing, rx_outgoing) = mpsc::unbounded();
let log_view = connection.log_view();
let (sink, stream) = connection.split();
let outgoing_future = sink.send_all(
rx_outgoing.map_err::<error::IrcError, _>(|()| {
unreachable!("futures::sync::mpsc::Receiver should never return Err");
})
).map(|_| ());
ClientState::new(stream, tx_outgoing, config.clone()).map(|state| {
let client = IrcClient {
state: Arc::new(state),
view: log_view,
};
(client, outgoing_future)
})
})
}
/// Gets the current nickname in use. This may be the primary username set in the configuration,

View file

@ -69,7 +69,10 @@ impl IrcReactor {
/// });
/// # }
/// ```
pub fn prepare_client<'a>(&mut self, config: &'a Config) -> error::Result<IrcClientFuture<'a>> {
pub fn prepare_client<'a>(&mut self, config: &'a Config) -> impl Future<
Item = (IrcClient, impl Future<Item = (), Error = error::IrcError> + 'static),
Error = error::IrcError
> {
IrcClient::new_future(config)
}
@ -90,8 +93,12 @@ impl IrcReactor {
/// });
/// # }
/// ```
pub fn connect_client(&mut self, future: IrcClientFuture) -> error::Result<IrcClient> {
self.inner.run(future).map(|PackedIrcClient(client, future)| {
pub fn connect_client<F, G>(&mut self, future: F) -> error::Result<IrcClient>
where
F: Future<Item = (IrcClient, G), Error = error::IrcError>,
G: Future<Item = (), Error = error::IrcError> + 'static,
{
self.inner.run(future).map(|(client, future)| {
self.register_future(future);
client
})
@ -114,7 +121,8 @@ impl IrcReactor {
/// # }
/// ```
pub fn prepare_client_and_connect(&mut self, config: &Config) -> error::Result<IrcClient> {
self.prepare_client(config).and_then(|future| self.connect_client(future))
let client_future = self.prepare_client(config);
self.connect_client(client_future)
}
/// Registers the given client with the specified message handler. The reactor will store this