Skip to content

Instantly share code, notes, and snippets.

@dflemstr
Last active November 28, 2016 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dflemstr/1b1a4ae1465fa72b42c4866c8a82d86a to your computer and use it in GitHub Desktop.
Save dflemstr/1b1a4ae1465fa72b42c4866c8a82d86a to your computer and use it in GitHub Desktop.
#![feature(prelude_import)]
#![no_std]
#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate std as std;
#[macro_use]
extern crate error_chain;
extern crate a;
/// The Error type
///
/// This has a simple structure to support pattern matching
/// during error handling. The second field is internal state
/// that is mostly irrelevant for error handling purposes.
pub struct Error {
/// The kind of the error.
pub kind: ErrorKind,
/// Contains the error chain and the backtrace.
pub state: ::error_chain::State,
}
#[automatically_derived]
#[allow(unused_qualifications)]
impl ::std::fmt::Debug for Error {
fn fmt(&self, __arg_0: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
match *self {
Error { kind: ref __self_0_0, state: ref __self_0_1 } => {
let mut builder = __arg_0.debug_struct("Error");
let _ = builder.field("kind", &&(*__self_0_0));
let _ = builder.field("state", &&(*__self_0_1));
builder.finish()
}
}
}
}
#[allow(dead_code)]
impl Error {
/// Returns the backtrace associated with this error.
pub fn backtrace(&self) -> Option<&::error_chain::Backtrace> {
self.state.backtrace.as_ref().map(|v| &**v)
}
}
impl ::error_chain::ChainedError for Error {
type
ErrorKind
=
ErrorKind;
fn new(kind: ErrorKind, state: ::error_chain::State) -> Error {
Error{kind: kind, state: state,}
}
fn extract_backtrace(e: &(::std::error::Error+ Send + 'static))
-> Option<Option<::std::sync::Arc<::error_chain::Backtrace>>> {
if let Some(e) = e.downcast_ref::<Error>() {
return Some(e.state.backtrace.clone());
}
{
if let Some(e) = e.downcast_ref::<a::Error>() {
return Some(e.state.backtrace.clone());
}
}
None
}
}
#[allow(dead_code)]
impl Error {
/// Constructs an error from a kind.
pub fn from_kind(kind: ErrorKind) -> Error {
Error{kind: kind, state: ::error_chain::State::default(),}
}
/// Returns the kind of the error.
pub fn kind(&self) -> &ErrorKind { &self.kind }
/// Iterates over the error chain.
pub fn iter(&self) -> ::error_chain::ErrorChainIter {
::error_chain::ErrorChainIter(Some(self))
}
}
impl ::std::error::Error for Error {
fn description(&self) -> &str { self.kind.description() }
fn cause(&self) -> Option<&::std::error::Error> {
match self.state.next_error {
Some(ref c) => Some(&**c),
None => { match self.kind { _ => None, } }
}
}
}
impl ::std::fmt::Display for Error {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
::std::fmt::Display::fmt(&self.kind, f)
}
}
impl From<a::Error> for Error {
fn from(e: a::Error) -> Self {
Error{kind: ErrorKind::A(e.kind), state: e.state,}
}
}
impl From<ErrorKind> for Error {
fn from(e: ErrorKind) -> Self { Error::from_kind(e) }
}
impl <'a> From<&'a str> for Error {
fn from(s: &'a str) -> Self { Error::from_kind(s.into()) }
}
impl From<String> for Error {
fn from(s: String) -> Self { Error::from_kind(s.into()) }
}
impl ::std::ops::Deref for Error {
type
Target
=
ErrorKind;
fn deref(&self) -> &Self::Target { &self.kind }
}
#[doc = r" The kind of an error"]
pub enum ErrorKind {
#[doc = r" A convenient variant for String."]
Msg(String),
A(<a::Error as ::error_chain::ChainedError>::ErrorKind),
}
#[automatically_derived]
#[allow(unused_qualifications)]
impl ::std::fmt::Debug for ErrorKind {
fn fmt(&self, __arg_0: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
match (&*self,) {
(&ErrorKind::Msg(ref __self_0),) => {
let mut builder = __arg_0.debug_tuple("Msg");
let _ = builder.field(&&(*__self_0));
builder.finish()
}
(&ErrorKind::A(ref __self_0),) => {
let mut builder = __arg_0.debug_tuple("A");
let _ = builder.field(&&(*__self_0));
builder.finish()
}
}
}
}
#[allow(unused)]
impl ::std::fmt::Display for ErrorKind {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
match *self
{
#[doc = r" A convenient variant for String."]
ErrorKind::Msg(ref s) => {
let display_fn = |_, f: &mut ::std::fmt::Formatter| {
f.write_fmt(::std::fmt::Arguments::new_v1({
static __STATIC_FMTSTR:
&'static [&'static str]
=
&[""];
__STATIC_FMTSTR
},
&match (&s,) {
(__arg0,)
=>
[::std::fmt::ArgumentV1::new(__arg0,
::std::fmt::Display::fmt)],
})) };
display_fn(self, fmt)
}
ErrorKind::A(ref e) => {
let display_fn = |_, f: &mut ::std::fmt::Formatter| {
f.write_fmt(::std::fmt::Arguments::new_v1({
static __STATIC_FMTSTR:
&'static [&'static str]
=
&[""];
__STATIC_FMTSTR
},
&match (&e,) {
(__arg0,)
=>
[::std::fmt::ArgumentV1::new(__arg0,
::std::fmt::Display::fmt)],
})) };
display_fn(self, fmt)
}
}
}
}
#[allow(unused)]
impl ErrorKind {
/// A string describing the error kind.
pub fn description(&self) -> &str {
match *self
{
#[doc = r" A convenient variant for String."]
ErrorKind::Msg(ref s) => {
&s
}
ErrorKind::A(ref e) => { e.description() }
}
}
}
impl From<<a::Error as ::error_chain::ChainedError>::ErrorKind> for ErrorKind
{
fn from(e: <a::Error as ::error_chain::ChainedError>::ErrorKind) -> Self {
ErrorKind::A(e)
}
}
impl <'a> From<&'a str> for ErrorKind {
fn from(s: &'a str) -> Self { ErrorKind::Msg(s.to_string()) }
}
impl From<String> for ErrorKind {
fn from(s: String) -> Self { ErrorKind::Msg(s) }
}
impl From<Error> for ErrorKind {
fn from(e: Error) -> Self { e.kind }
}
/// Convenient wrapper around `std::Result`.
pub type Result<T> = ::std::result::Result<T, Error>;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment