diff options
| author | Alex Crichton <alex@alexcrichton.com> | 2015-03-31 16:18:55 -0700 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2015-03-31 16:18:55 -0700 |
| commit | 50b3ecf3bcc2e39a7a42e7f4b49f19398d5cc681 (patch) | |
| tree | 0546f83f5625d11fc4d5d9b2ad215a4ac3ce4778 /src/libstd/io | |
| parent | 85e997adfffa5e8511ff31710649038216028832 (diff) | |
| parent | ac77392f8ab1c201b0c927f6a2d30b632b95acda (diff) | |
| download | rust-50b3ecf3bcc2e39a7a42e7f4b49f19398d5cc681.tar.gz rust-50b3ecf3bcc2e39a7a42e7f4b49f19398d5cc681.zip | |
rollup merge of #23919: alexcrichton/stabilize-io-error
Conflicts: src/libstd/fs/tempdir.rs src/libstd/io/error.rs
Diffstat (limited to 'src/libstd/io')
| -rw-r--r-- | src/libstd/io/buffered.rs | 2 | ||||
| -rw-r--r-- | src/libstd/io/cursor.rs | 3 | ||||
| -rw-r--r-- | src/libstd/io/error.rs | 67 | ||||
| -rw-r--r-- | src/libstd/io/impls.rs | 2 | ||||
| -rw-r--r-- | src/libstd/io/mod.rs | 7 |
5 files changed, 40 insertions, 41 deletions
diff --git a/src/libstd/io/buffered.rs b/src/libstd/io/buffered.rs index 628ad839c16..479665c4728 100644 --- a/src/libstd/io/buffered.rs +++ b/src/libstd/io/buffered.rs @@ -165,7 +165,7 @@ impl<W: Write> BufWriter<W> { match self.inner.as_mut().unwrap().write(&self.buf[written..]) { Ok(0) => { ret = Err(Error::new(ErrorKind::WriteZero, - "failed to write the buffered data", None)); + "failed to write the buffered data")); break; } Ok(n) => written += n, diff --git a/src/libstd/io/cursor.rs b/src/libstd/io/cursor.rs index ad81143b7b4..a7b7f96c22b 100644 --- a/src/libstd/io/cursor.rs +++ b/src/libstd/io/cursor.rs @@ -75,8 +75,7 @@ macro_rules! seek { if pos < 0 { Err(Error::new(ErrorKind::InvalidInput, - "invalid seek to a negative position", - None)) + "invalid seek to a negative position")) } else { self.pos = pos as u64; Ok(self.pos) diff --git a/src/libstd/io/error.rs b/src/libstd/io/error.rs index 615df771e9b..b84dcb8fb62 100644 --- a/src/libstd/io/error.rs +++ b/src/libstd/io/error.rs @@ -9,11 +9,12 @@ // except according to those terms. use boxed::Box; +use convert::Into; use error; use fmt; +use marker::Send; use option::Option::{self, Some, None}; use result; -use string::String; use sys; /// A type for results generated by I/O related functions where the `Err` type @@ -30,23 +31,22 @@ pub type Result<T> = result::Result<T, Error>; /// Errors mostly originate from the underlying OS, but custom instances of /// `Error` can be created with crafted error messages and a particular value of /// `ErrorKind`. -#[derive(PartialEq, Eq, Clone, Debug)] +#[derive(Debug)] #[stable(feature = "rust1", since = "1.0.0")] pub struct Error { repr: Repr, } -#[derive(PartialEq, Eq, Clone, Debug)] +#[derive(Debug)] enum Repr { Os(i32), Custom(Box<Custom>), } -#[derive(PartialEq, Eq, Clone, Debug)] +#[derive(Debug)] struct Custom { kind: ErrorKind, - desc: &'static str, - detail: Option<String> + error: Box<error::Error+Send>, } /// A list specifying general categories of I/O error. @@ -124,18 +124,34 @@ pub enum ErrorKind { } impl Error { - /// Creates a new custom error from a specified kind/description/detail. - #[unstable(feature = "io", reason = "the exact makeup of an Error may - change to include `Box<Error>` for \ - example")] - pub fn new(kind: ErrorKind, - description: &'static str, - detail: Option<String>) -> Error { + /// Creates a new I/O error from a known kind of error as well as an + /// arbitrary error payload. + /// + /// This function is used to generically create I/O errors which do not + /// originate from the OS itself. The `error` argument is an arbitrary + /// payload which will be contained in this `Error`. Accessors as well as + /// downcasting will soon be added to this type as well to access the custom + /// information. + /// + /// # Examples + /// + /// ``` + /// use std::io::{Error, ErrorKind}; + /// + /// // errors can be created from strings + /// let custom_error = Error::new(ErrorKind::Other, "oh no!"); + /// + /// // errors can also be created from other errors + /// let custom_error2 = Error::new(ErrorKind::Interrupted, custom_error); + /// ``` + #[stable(feature = "rust1", since = "1.0.0")] + pub fn new<E>(kind: ErrorKind, error: E) -> Error + where E: Into<Box<error::Error+Send>> + { Error { repr: Repr::Custom(Box::new(Custom { kind: kind, - desc: description, - detail: detail, + error: error.into(), })) } } @@ -161,8 +177,7 @@ impl Error { /// /// If this `Error` was constructed via `last_os_error` then this function /// will return `Some`, otherwise it will return `None`. - #[unstable(feature = "io", reason = "function was just added and the return \ - type may become an abstract OS error")] + #[stable(feature = "rust1", since = "1.0.0")] pub fn raw_os_error(&self) -> Option<i32> { match self.repr { Repr::Os(i) => Some(i), @@ -188,21 +203,7 @@ impl fmt::Display for Error { let detail = sys::os::error_string(code); write!(fmt, "{} (os error {})", detail, code) } - Repr::Custom(ref c) => { - match **c { - Custom { - kind: ErrorKind::Other, - desc: "unknown error", - detail: Some(ref detail) - } => { - write!(fmt, "{}", detail) - } - Custom { detail: None, desc, .. } => - write!(fmt, "{}", desc), - Custom { detail: Some(ref detail), desc, .. } => - write!(fmt, "{} ({})", desc, detail) - } - } + Repr::Custom(ref c) => c.error.fmt(fmt), } } } @@ -212,7 +213,7 @@ impl error::Error for Error { fn description(&self) -> &str { match self.repr { Repr::Os(..) => "os error", - Repr::Custom(ref c) => c.desc, + Repr::Custom(ref c) => c.error.description(), } } } diff --git a/src/libstd/io/impls.rs b/src/libstd/io/impls.rs index 52daba36213..67bc45d3b62 100644 --- a/src/libstd/io/impls.rs +++ b/src/libstd/io/impls.rs @@ -180,7 +180,7 @@ impl<'a> Write for &'a mut [u8] { if try!(self.write(data)) == data.len() { Ok(()) } else { - Err(Error::new(ErrorKind::WriteZero, "failed to write whole buffer", None)) + Err(Error::new(ErrorKind::WriteZero, "failed to write whole buffer")) } } diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index 0e379b5ab43..d72abe8c69b 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -83,7 +83,7 @@ fn append_to_string<F>(buf: &mut String, f: F) -> Result<usize> if str::from_utf8(&g.s[g.len..]).is_err() { ret.and_then(|_| { Err(Error::new(ErrorKind::InvalidInput, - "stream did not contain valid UTF-8", None)) + "stream did not contain valid UTF-8")) }) } else { g.len = g.s.len(); @@ -359,8 +359,7 @@ pub trait Write { while buf.len() > 0 { match self.write(buf) { Ok(0) => return Err(Error::new(ErrorKind::WriteZero, - "failed to write whole buffer", - None)), + "failed to write whole buffer")), Ok(n) => buf = &buf[n..], Err(ref e) if e.kind() == ErrorKind::Interrupted => {} Err(e) => return Err(e), @@ -782,7 +781,7 @@ pub struct Chars<R> { /// An enumeration of possible errors that can be generated from the `Chars` /// adapter. -#[derive(PartialEq, Clone, Debug)] +#[derive(Debug)] #[unstable(feature = "io", reason = "awaiting stability of Read::chars")] pub enum CharsError { /// Variant representing that the underlying stream was read successfully |
