more documentation \o/

This commit is contained in:
lumi 2017-02-21 17:38:29 +01:00
parent 06380c55cc
commit 0cc57e1793
4 changed files with 38 additions and 0 deletions

View file

@ -1,3 +1,5 @@
//! Provides an `Error` for use in this crate.
use std::io; use std::io;
use std::net::TcpStream; use std::net::TcpStream;
@ -10,6 +12,7 @@ use xml::writer::Error as EmitterError;
use minidom::Error as MinidomError; use minidom::Error as MinidomError;
/// An error which wraps a bunch of errors from different crates and the stdlib.
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
XmlError(XmlError), XmlError(XmlError),

View file

@ -1,25 +1,56 @@
//! Provides an abstract event type which can be downcasted into a more specific one.
//!
//! # Examples
//!
//! ```
//! use xmpp::event::{Event, AbstractEvent};
//!
//! #[derive(Debug, PartialEq, Eq)]
//! struct EventA;
//!
//! impl Event for EventA {}
//!
//! #[derive(Debug, PartialEq, Eq)]
//! struct EventB;
//!
//! impl Event for EventB {}
//!
//! let event_a = AbstractEvent::new(EventA);
//!
//! assert_eq!(event_a.is::<EventA>(), true);
//! assert_eq!(event_a.is::<EventB>(), false);
//!
//! assert_eq!(event_a.downcast::<EventA>(), Some(&EventA));
//! assert_eq!(event_a.downcast::<EventB>(), None);
//! ```
use std::fmt::Debug; use std::fmt::Debug;
use std::any::Any; use std::any::Any;
/// An abstract event.
pub struct AbstractEvent { pub struct AbstractEvent {
inner: Box<Any>, inner: Box<Any>,
} }
impl AbstractEvent { impl AbstractEvent {
/// Creates an abstract event from a concrete event.
pub fn new<E: Event>(event: E) -> AbstractEvent { pub fn new<E: Event>(event: E) -> AbstractEvent {
AbstractEvent { AbstractEvent {
inner: Box::new(event), inner: Box::new(event),
} }
} }
/// Downcasts this abstract event into a concrete event.
pub fn downcast<E: Event + 'static>(&self) -> Option<&E> { pub fn downcast<E: Event + 'static>(&self) -> Option<&E> {
self.inner.downcast_ref::<E>() self.inner.downcast_ref::<E>()
} }
/// Checks whether this abstract event is a specific concrete event.
pub fn is<E: Event + 'static>(&self) -> bool { pub fn is<E: Event + 'static>(&self) -> bool {
self.inner.is::<E>() self.inner.is::<E>()
} }
} }
/// A marker trait which all events must implement.
pub trait Event: Any + Debug {} pub trait Event: Any + Debug {}

View file

@ -1,3 +1,5 @@
//! Provides a type for Jabber IDs.
use std::fmt; use std::fmt;
use std::convert::Into; use std::convert::Into;

View file

@ -1,3 +1,5 @@
//! Provides the plugin infrastructure.
use event::{Event, AbstractEvent}; use event::{Event, AbstractEvent};
use std::any::Any; use std::any::Any;