Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move ::std::error to ::core::error #33149

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions src/liballoc/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ use core::ops::{Placer, Boxed, Place, InPlace, BoxPlace};
use core::ptr::{self, Unique};
use core::raw::TraitObject;
use core::convert::From;
use core::error::Error;

/// 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 @@ -641,3 +642,112 @@ impl<T: ?Sized> AsMut<T> for Box<T> {
&mut **self
}
}



#[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 + Sync + 'a> From<E> for Box<Error + Send + Sync + 'a> {
fn from(err: E) -> Box<Error + Send + Sync + 'a> {
Box::new(err)
}
}

impl Box<str> {
fn new_box_str(s: &str) -> Box<str> {
let len = s.len();
let buf = RawVec::with_capacity(len);
unsafe {
ptr::copy_nonoverlapping(s.as_ptr(), buf.ptr(), len);
mem::transmute(buf.into_box()) // bytes to str ~magic
}
}
}

#[unstable(feature="error_box_str",issue="0")]
impl Error for Box<str> {
fn description(&self) -> &str {
self
}
}

impl Box<Error> {
#[inline]
#[stable(feature = "error_downcast", since = "1.3.0")]
/// Attempt to downcast the box to a concrete type.
pub fn downcast<T: Error + 'static>(self) -> Result<Box<T>, Box<Error>> {
if self.is::<T>() {
unsafe {
// Get the raw representation of the trait object
let raw = Box::into_raw(self);
let to: TraitObject =
mem::transmute::<*mut Error, TraitObject>(raw);

// Extract the data pointer
Ok(Box::from_raw(to.data as *mut T))
}
} else {
Err(self)
}
}
}

impl Box<Error + Send> {
#[inline]
#[stable(feature = "error_downcast", since = "1.3.0")]
/// Attempt to downcast the box to a concrete type.
pub fn downcast<T: Error + 'static>(self)
-> Result<Box<T>, Box<Error + Send>> {
let err: Box<Error> = self;
<Box<Error>>::downcast(err).map_err(|s| unsafe {
// reapply the Send marker
mem::transmute::<Box<Error>, Box<Error + Send>>(s)
})
}
}

impl Box<Error + Send + Sync> {
#[inline]
#[stable(feature = "error_downcast", since = "1.3.0")]
/// Attempt to downcast the box to a concrete type.
pub fn downcast<T: Error + 'static>(self)
-> Result<Box<T>, Self> {
let err: Box<Error> = self;
<Box<Error>>::downcast(err).map_err(|s| unsafe {
// reapply the Send+Sync marker
mem::transmute::<Box<Error>, Box<Error + Send + Sync>>(s)
})
}
}

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

#[stable(feature = "string_box_error", since = "1.7.0")]
impl<'a> From<&'a str> for Box<Error> {
fn from(err: &'a str) -> Box<Error> {
From::from(Box::new_box_str(err))
}
}

#[stable(feature = "box_error", since = "1.7.0")]
impl<T: Error> Error for Box<T> {
fn description(&self) -> &str {
Error::description(&**self)
}

fn cause(&self) -> Option<&Error> {
Error::cause(&**self)
}
}

54 changes: 54 additions & 0 deletions src/libcollections/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ use core::mem;
use core::ops::{self, Add, Index, IndexMut};
use core::ptr;
use core::str::pattern::Pattern;
use core::error::Error;
use rustc_unicode::char::{decode_utf16, REPLACEMENT_CHARACTER};
use rustc_unicode::str as unicode_str;

Expand Down Expand Up @@ -1897,3 +1898,56 @@ impl<'a> DoubleEndedIterator for Drain<'a> {
self.iter.next_back()
}
}



#[stable(feature = "rust1", since = "1.0.0")]
impl From<String> for Box<Error + Send + Sync> {
fn from(err: String) -> Box<Error + Send + Sync> {
#[derive(Debug)]
struct StringError(String);

impl Error for StringError {
fn description(&self) -> &str { &self.0 }
}

impl fmt::Display for StringError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}

Box::new(StringError(err))
}
}

#[stable(feature = "string_box_error", since = "1.7.0")]
impl From<String> for Box<Error> {
fn from(str_err: String) -> Box<Error> {
let err1: Box<Error + Send + Sync> = From::from(str_err);
let err2: Box<Error> = err1;
err2
}
}


#[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 Error for FromUtf16Error {
fn description(&self) -> &str {
"invalid utf-16"
}
}

#[stable(feature = "str_parse_error2", since = "1.8.0")]
impl Error for ParseError {
fn description(&self) -> &str {
match *self {}
}
}
152 changes: 3 additions & 149 deletions src/libstd/error.rs → src/libcore/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,17 @@
// reconsider what crate these items belong in.

use any::TypeId;
use boxed::Box;
use char;
use fmt::{self, Debug, Display};
use fmt::{Debug, Display};
use marker::{Send, Sync, Reflect};
use mem::transmute;
use num;
use raw::TraitObject;
use str;
use string::{self, String};
use option::Option::{self, Some, None};

/// Base functionality for all errors in Rust.
#[stable(feature = "rust1", since = "1.0.0")]
#[fundamental]
pub trait Error: Debug + Display + Reflect {
/// A short description of the error.
///
Expand All @@ -83,63 +82,6 @@ pub trait Error: Debug + Display + Reflect {
}
}

#[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 + Sync + 'a> From<E> for Box<Error + Send + Sync + 'a> {
fn from(err: E) -> Box<Error + Send + Sync + 'a> {
Box::new(err)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl From<String> for Box<Error + Send + Sync> {
fn from(err: String) -> Box<Error + Send + Sync> {
#[derive(Debug)]
struct StringError(String);

impl Error for StringError {
fn description(&self) -> &str { &self.0 }
}

impl Display for StringError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(&self.0, f)
}
}

Box::new(StringError(err))
}
}

#[stable(feature = "string_box_error", since = "1.7.0")]
impl From<String> for Box<Error> {
fn from(str_err: String) -> Box<Error> {
let err1: Box<Error + Send + Sync> = From::from(str_err);
let err2: Box<Error> = err1;
err2
}
}

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

#[stable(feature = "string_box_error", since = "1.7.0")]
impl<'a> From<&'a str> for Box<Error> {
fn from(err: &'a str) -> Box<Error> {
From::from(String::from(err))
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Error for str::ParseBoolError {
fn description(&self) -> &str { "failed to parse bool" }
Expand All @@ -166,45 +108,6 @@ impl Error for num::ParseFloatError {
}
}

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

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

#[stable(feature = "str_parse_error2", since = "1.8.0")]
impl Error for string::ParseError {
fn description(&self) -> &str {
match *self {}
}
}

#[stable(feature = "decode_utf16", since = "1.9.0")]
impl Error for char::DecodeUtf16Error {
fn description(&self) -> &str {
"unpaired surrogate found"
}
}

#[stable(feature = "box_error", since = "1.7.0")]
impl<T: Error> Error for Box<T> {
fn description(&self) -> &str {
Error::description(&**self)
}

fn cause(&self) -> Option<&Error> {
Error::cause(&**self)
}
}

// copied from any.rs
impl Error + 'static {
/// Returns true if the boxed type is the same as `T`
Expand Down Expand Up @@ -304,55 +207,6 @@ impl Error + 'static + Send + Sync {
}
}

impl Error {
#[inline]
#[stable(feature = "error_downcast", since = "1.3.0")]
/// Attempt to downcast the box to a concrete type.
pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<Error>> {
if self.is::<T>() {
unsafe {
// Get the raw representation of the trait object
let raw = Box::into_raw(self);
let to: TraitObject =
transmute::<*mut Error, TraitObject>(raw);

// Extract the data pointer
Ok(Box::from_raw(to.data as *mut T))
}
} else {
Err(self)
}
}
}

impl Error + Send {
#[inline]
#[stable(feature = "error_downcast", since = "1.3.0")]
/// Attempt to downcast the box to a concrete type.
pub fn downcast<T: Error + 'static>(self: Box<Self>)
-> Result<Box<T>, Box<Error + Send>> {
let err: Box<Error> = self;
<Error>::downcast(err).map_err(|s| unsafe {
// reapply the Send marker
transmute::<Box<Error>, Box<Error + Send>>(s)
})
}
}

impl Error + Send + Sync {
#[inline]
#[stable(feature = "error_downcast", since = "1.3.0")]
/// Attempt to downcast the box to a concrete type.
pub fn downcast<T: Error + 'static>(self: Box<Self>)
-> Result<Box<T>, Box<Self>> {
let err: Box<Error> = self;
<Error>::downcast(err).map_err(|s| unsafe {
// reapply the Send+Sync marker
transmute::<Box<Error>, Box<Error + Send + Sync>>(s)
})
}
}

#[cfg(test)]
mod tests {
use prelude::v1::*;
Expand Down
2 changes: 2 additions & 0 deletions src/libcore/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,7 @@ pub mod str;
pub mod hash;
pub mod fmt;

pub mod error;

// note: does not need to be public
mod tuple;
7 changes: 7 additions & 0 deletions src/librustc_unicode/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,13 @@ pub struct DecodeUtf16Error {
code: u16,
}

#[stable(feature = "decode_utf16", since = "1.9.0")]
impl ::core::error::Error for DecodeUtf16Error {
fn description(&self) -> &str {
"unpaired surrogate found"
}
}

/// Create an iterator over the UTF-16 encoded code points in `iter`,
/// returning unpaired surrogates as `Err`s.
///
Expand Down
Loading