Writer

Struct Writer 

Source
pub struct Writer { /* private fields */ }
Expand description

Used to send messages to the IRC server.

This object is thread safe. You can clone it and send the clones to other threads. You can write from multiple threads without any issue. Internally, it uses Arc and Mutex.

Implementations§

Source§

impl Writer

Source

pub fn disconnect(&self) -> Result<(), Error>

Drop the connection and trigger the reconnection process.

There might be a reconnection attempt, based on your settings. This should be used if you want the connection to be re-created. This is not the preferred way of shutting down the connection for good. Use close for this.

Source

pub fn is_closed(&self) -> bool

Check if the connection was manually closed.

Source

pub fn close(&self) -> Result<(), Error>

Close the connection and stop listening for messages.

There will not be any reconnection attempt. An error will be returned if the connection is already closed.

Source

pub fn raw<S: AsRef<str>>(&self, data: S) -> Result<(), Error>

Send a raw string to the IRC server.

A new line will be not be added, so make sure that you include it. An error will be returned if the client is disconnected.

Examples found in repository?
examples/peekaboo.rs (line 19)
11fn main() {
12    let args: Vec<String> = env::args().collect();
13    let channel = args.get(1).expect("Channel must be given as an argument.");
14
15    // Connect to freenode and use no not reconnect.
16    let (writer, reader) = connect("irc.freenode.net:6667",
17                                   ReconnectionSettings::DoNotReconnect,
18                                   UTF_8).unwrap();
19    writer.raw(format!("USER {} 8 * :{}\n", "peekaboo", "peekaboo"));
20    writer.raw(format!("NICK {}\n", "peekaboo"));
21
22    // Receive events.
23    for event in reader.iter() {
24        println!("{:?}", event);
25        match event {
26            Event::Message(msg) => {
27                if msg.code == Code::RplWelcome {
28                    // join channel, no password
29                    writer.raw(format!("JOIN {}\n", channel));
30                }
31                // JOIN is sent when you join a channel.
32                if msg.code == Code::Join {
33                    // If there is a prefix...
34                    if let Some(prefix) = msg.prefix {
35                        match prefix {
36                            // And the prefix is a user...
37                            Prefix::User(user) => {
38                                // And that user's nick is peekaboo, we've joined the channel!
39                                if user.nickname == "peekaboo" {
40                                    writer.raw(format!("PRIVMSG {} :{}\n", channel, "peekaboo"));
41                                    // Note that if the reconnection settings said to reconnect,
42                                    // it would. Close would "really" stop it.
43                                    writer.raw(format!("QUIT :{}\n", "peekaboo"));
44                                    // writer.close();
45                                }
46                            }
47                            _ => {}
48                        }
49                    }
50                }
51            }
52            _ => {}
53        }
54    }
55}

Trait Implementations§

Source§

impl Clone for Writer

Source§

fn clone(&self) -> Writer

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl Freeze for Writer

§

impl !RefUnwindSafe for Writer

§

impl Send for Writer

§

impl Sync for Writer

§

impl Unpin for Writer

§

impl !UnwindSafe for Writer

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.