Skip to content

Commit

Permalink
Collect the definition of the Error trait into libstd for now. This
Browse files Browse the repository at this point in the history
sidesteps a coherence difficulty where `liballoc` had to prove that
`&str: !Error`, which didn't involve any local types.
  • Loading branch information
nikomatsakis committed Apr 1, 2015
1 parent 15b58fe commit 19d3dab
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 157 deletions.
53 changes: 2 additions & 51 deletions src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,12 @@ use core::prelude::*;
use core::any::Any;
use core::cmp::Ordering;
use core::default::Default;
use core::error::Error;
use core::fmt;
use core::hash::{self, Hash};
use core::mem;
use core::ops::{Deref, DerefMut};
use core::ptr::{self, Unique};
use core::raw::{TraitObject, Slice};

use heap;
use core::ptr::{Unique};
use core::raw::{TraitObject};

/// A value that represents the heap. This is the default place that the `box`
/// keyword allocates into when no place is supplied.
Expand Down Expand Up @@ -303,49 +300,3 @@ impl<I: DoubleEndedIterator + ?Sized> DoubleEndedIterator for Box<I> {
#[stable(feature = "rust1", since = "1.0.0")]
impl<I: ExactSizeIterator + ?Sized> ExactSizeIterator for Box<I> {}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, E: Error + 'a> From<E> for Box<Error + 'a> {
fn from(err: E) -> Box<Error + 'a> {
Box::new(err)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, E: Error + Send + 'a> From<E> for Box<Error + Send + 'a> {
fn from(err: E) -> Box<Error + Send + 'a> {
Box::new(err)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, 'b> From<&'b str> for Box<Error + Send + 'a> {
fn from(err: &'b str) -> Box<Error + Send + 'a> {
#[derive(Debug)]
struct StringError(Box<str>);
impl Error for StringError {
fn description(&self) -> &str { &self.0 }
}
impl fmt::Display for StringError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}

// Unfortunately `String` is located in libcollections, so we construct
// a `Box<str>` manually here.
unsafe {
let alloc = if err.len() == 0 {
0 as *mut u8
} else {
let ptr = heap::allocate(err.len(), 1);
if ptr.is_null() { ::oom(); }
ptr as *mut u8
};
ptr::copy(err.as_bytes().as_ptr(), alloc, err.len());
Box::new(StringError(mem::transmute(Slice {
data: alloc,
len: err.len(),
})))
}
}
}
11 changes: 0 additions & 11 deletions src/libcollections/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use core::prelude::*;

use core::default::Default;
use core::error::Error;
use core::fmt;
use core::hash;
use core::iter::{IntoIterator, FromIterator};
Expand Down Expand Up @@ -723,23 +722,13 @@ impl fmt::Display for FromUtf8Error {
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Error for FromUtf8Error {
fn description(&self) -> &str { "invalid utf-8" }
}

#[stable(feature = "rust1", since = "1.0.0")]
impl fmt::Display for FromUtf16Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt("invalid utf-16: lone surrogate found", f)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Error for FromUtf16Error {
fn description(&self) -> &str { "invalid utf-16" }
}

#[stable(feature = "rust1", since = "1.0.0")]
impl FromIterator<char> for String {
fn from_iter<I: IntoIterator<Item=char>>(iter: I) -> String {
Expand Down
56 changes: 0 additions & 56 deletions src/libcore/error.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/libcore/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ pub mod slice;
pub mod str;
pub mod hash;
pub mod fmt;
pub mod error;

#[doc(primitive = "bool")]
mod bool {
Expand Down
41 changes: 20 additions & 21 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use self::wrapping::{OverflowingOps, WrappingOps};
use char::CharExt;
use clone::Clone;
use cmp::{PartialEq, Eq, PartialOrd, Ord};
use error::Error;
use fmt;
use intrinsics;
use iter::Iterator;
Expand Down Expand Up @@ -2948,16 +2947,9 @@ enum IntErrorKind {
Underflow,
}

#[stable(feature = "rust1", since = "1.0.0")]
impl fmt::Display for ParseIntError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.description().fmt(f)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Error for ParseIntError {
fn description(&self) -> &str {
impl ParseIntError {
#[unstable(feature = "core", reason = "available through Error trait")]
pub fn description(&self) -> &str {
match self.kind {
IntErrorKind::Empty => "cannot parse integer from empty string",
IntErrorKind::InvalidDigit => "invalid digit found in string",
Expand All @@ -2967,6 +2959,13 @@ impl Error for ParseIntError {
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl fmt::Display for ParseIntError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.description().fmt(f)
}
}

/// An error which can be returned when parsing a float.
#[derive(Debug, Clone, PartialEq)]
#[stable(feature = "rust1", since = "1.0.0")]
Expand All @@ -2978,19 +2977,19 @@ enum FloatErrorKind {
Invalid,
}

#[stable(feature = "rust1", since = "1.0.0")]
impl fmt::Display for ParseFloatError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.description().fmt(f)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Error for ParseFloatError {
fn description(&self) -> &str {
impl ParseFloatError {
#[unstable(feature = "core", reason = "available through Error trait")]
pub fn description(&self) -> &str {
match self.kind {
FloatErrorKind::Empty => "cannot parse float from empty string",
FloatErrorKind::Invalid => "invalid float literal",
}
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl fmt::Display for ParseFloatError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.description().fmt(f)
}
}
16 changes: 0 additions & 16 deletions src/libcore/str/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use char::CharExt;
use clone::Clone;
use cmp::{self, Eq};
use default::Default;
use error::Error;
use fmt;
use iter::ExactSizeIterator;
use iter::{Map, Iterator, DoubleEndedIterator};
Expand Down Expand Up @@ -192,11 +191,6 @@ impl fmt::Display for ParseBoolError {
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Error for ParseBoolError {
fn description(&self) -> &str { "failed to parse bool" }
}

/*
Section: Creating a string
*/
Expand Down Expand Up @@ -241,16 +235,6 @@ pub unsafe fn from_utf8_unchecked<'a>(v: &'a [u8]) -> &'a str {
mem::transmute(v)
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Error for Utf8Error {
fn description(&self) -> &str {
match *self {
Utf8Error::TooShort => "invalid utf-8: not enough bytes",
Utf8Error::InvalidByte(..) => "invalid utf-8: corrupt contents",
}
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl fmt::Display for Utf8Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down
Loading

0 comments on commit 19d3dab

Please sign in to comment.