From ea0577bbaf58090be4ba82ef5baed748f034b181 Mon Sep 17 00:00:00 2001 From: Aaron Weiss Date: Sun, 3 Jan 2016 05:39:44 -0500 Subject: [PATCH] Implemented Clone for IrcServer and updated multithreaded examples accordingly. --- examples/autoreconnect.rs | 3 +-- examples/multithreaded.rs | 3 +-- examples/tweeter.rs | 3 +-- src/client/server/mod.rs | 7 +++++++ 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/examples/autoreconnect.rs b/examples/autoreconnect.rs index 387007b..8cc2326 100644 --- a/examples/autoreconnect.rs +++ b/examples/autoreconnect.rs @@ -1,7 +1,6 @@ extern crate irc; use std::default::Default; -use std::sync::Arc; use std::thread::spawn; use irc::client::prelude::*; @@ -12,7 +11,7 @@ fn main() { channels: Some(vec![format!("#vana")]), .. Default::default() }; - let server = Arc::new(IrcServer::from_config(config).unwrap()); + let server = IrcServer::from_config(config).unwrap(); // FIXME: if set_keepalive is stabilized, this can be readded. // server.conn().set_keepalive(Some(5)).unwrap(); let server = server.clone(); diff --git a/examples/multithreaded.rs b/examples/multithreaded.rs index 4602540..723b1eb 100644 --- a/examples/multithreaded.rs +++ b/examples/multithreaded.rs @@ -1,7 +1,6 @@ extern crate irc; use std::default::Default; -use std::sync::Arc; use std::thread::spawn; use irc::client::prelude::*; @@ -12,7 +11,7 @@ fn main() { channels: Some(vec![format!("#vana")]), .. Default::default() }; - let server = Arc::new(IrcServer::from_config(config).unwrap()); + let server = IrcServer::from_config(config).unwrap(); server.identify().unwrap(); let server = server.clone(); let _ = spawn(move || { diff --git a/examples/tweeter.rs b/examples/tweeter.rs index 465a0e2..787e794 100644 --- a/examples/tweeter.rs +++ b/examples/tweeter.rs @@ -1,7 +1,6 @@ extern crate irc; use std::default::Default; -use std::sync::Arc; use std::thread::{sleep, spawn}; use std::time::Duration; use irc::client::prelude::*; @@ -13,7 +12,7 @@ fn main() { channels: Some(vec![format!("#vana")]), .. Default::default() }; - let server = Arc::new(IrcServer::from_config(config).unwrap()); + let server = IrcServer::from_config(config).unwrap(); server.identify().unwrap(); let server2 = server.clone(); // Let's set up a loop that just prints the messages. diff --git a/src/client/server/mod.rs b/src/client/server/mod.rs index cafaefd..71e3e0f 100644 --- a/src/client/server/mod.rs +++ b/src/client/server/mod.rs @@ -37,6 +37,7 @@ pub struct IrcServer { state: Arc>, } +/// Thread-safe internal state for an IRC server connection. struct ServerState { /// The thread-safe IRC connection. conn: Connection, @@ -81,6 +82,12 @@ impl IrcServer, BufWriter> { } } +impl<'a, T: IrcRead, U: IrcWrite> Clone for IrcServer { + fn clone(&self) -> IrcServer { + IrcServer { state: self.state.clone() } + } +} + impl<'a, T: IrcRead, U: IrcWrite> Server<'a, T, U> for IrcServer { fn config(&self) -> &Config { &self.state.config