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

Rollup of 10 pull requests #54790

Closed
wants to merge 28 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
fcda7b2
Add doc for impl From for Std Error
phungleson Sep 5, 2018
a7cc1fc
Examples for docs
phungleson Sep 25, 2018
53254a8
Add enable-missing-tools option
pvdrz Sep 28, 2018
0724ed6
Add DIST_REQUIRE_ALL_TOOLS to CI scripts
pvdrz Sep 28, 2018
2765575
Fix conditions to allow missing tools in CI
pvdrz Sep 30, 2018
922b9d8
[NFC] `getopts` is used by `librustc` and `librustc_driver`, but isn'…
DiamondLovesYou Sep 30, 2018
ec59188
Make spec_extend use for_each()
Lucretiel Oct 2, 2018
8da08c0
Run debuginfo tests against rust-enabled lldb, when possible
tromey Oct 2, 2018
84f75f0
Fix typo in CONTRIBUTING.md
Oct 2, 2018
30f2e96
Remove main() in examples
phungleson Oct 2, 2018
d686896
Update a FIXME in memory.rs
wesleywiser Oct 3, 2018
187bcb9
rustc/ty: whitespace fixes
ljedrz Oct 2, 2018
f8cacca
rustc/ty: simplify some patterns
ljedrz Oct 2, 2018
774881d
rustc/ty: mark a comment as FIXME
ljedrz Oct 2, 2018
db17164
rustc/ty: calculate span after a possible early continue
ljedrz Oct 2, 2018
04b99bc
rustc/ty: improve allocations
ljedrz Oct 2, 2018
f0de294
A handful of cleanups for rustc/mir
ljedrz Oct 3, 2018
c2c8f8a
Update clippy
Manishearth Oct 3, 2018
3c22127
Rollup merge of #53523 - phungleson:fix-impl-from-for-std-error, r=Gu…
kennytm Oct 3, 2018
72aad24
Rollup merge of #54638 - christianpoveda:master, r=kennytm
kennytm Oct 3, 2018
f66f01e
Rollup merge of #54698 - DiamondLovesYou:getopts-deps, r=davidtwco
kennytm Oct 3, 2018
8b9145e
Rollup merge of #54743 - ljedrz:cleanup_ty_p2, r=zackmdavis
kennytm Oct 3, 2018
2b84507
Rollup merge of #54761 - Lucretiel:patch-1, r=cramertj
kennytm Oct 3, 2018
61a5a51
Rollup merge of #54764 - tromey:test-rust-lldb, r=alexcrichton
kennytm Oct 3, 2018
09b4913
Rollup merge of #54769 - jacobherrington:patch-1, r=kennytm
kennytm Oct 3, 2018
0ba5d74
Rollup merge of #54773 - rust-lang:wesleywiser-patch-1, r=oli-obk
kennytm Oct 3, 2018
a42a4d6
Rollup merge of #54784 - Manishearth:clippyup, r=oli-obk
kennytm Oct 3, 2018
94508ca
Rollup merge of #54788 - ljedrz:cleanup_rustc_mir, r=oli-obk
kennytm Oct 3, 2018
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
Prev Previous commit
Next Next commit
Examples for docs
  • Loading branch information
phungleson committed Sep 25, 2018
commit a7cc1fccbd49a45a4d99a4c4ccdad16972b235bf
148 changes: 148 additions & 0 deletions src/libstd/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,36 @@ pub trait Error: Debug + Display {
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, E: Error + 'a> From<E> for Box<dyn Error + 'a> {
/// Converts a type of [`Error`] into a box of dyn [`Error`].
///
/// # Examples
///
/// ```
/// use std::error::Error;
/// use std::fmt;
/// use std::mem;
///
/// #[derive(Debug)]
/// struct AnError;
///
/// impl fmt::Display for AnError {
/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
/// write!(f , "An error")
/// }
/// }
///
/// impl Error for AnError {
/// fn description(&self) -> &str {
/// "Description of an error"
/// }
/// }
///
/// fn main() {
/// let an_error = AnError;
/// assert!(0 == mem::size_of_val(&an_error));
/// let a_boxed_error = Box::<Error>::from(an_error);
/// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
/// }
/// ```
fn from(err: E) -> Box<dyn Error + 'a> {
Box::new(err)
}
Expand All @@ -162,6 +192,41 @@ impl<'a, E: Error + 'a> From<E> for Box<dyn Error + 'a> {
impl<'a, E: Error + Send + Sync + 'a> From<E> for Box<dyn Error + Send + Sync + 'a> {
/// Converts a type of [`Error`] + [`Send`] + [`Sync`] into a box of dyn [`Error`] +
/// [`Send`] + [`Sync`].
///
/// # Examples
///
/// ```
/// use std::error::Error;
/// use std::fmt;
/// use std::mem;
///
/// #[derive(Debug)]
/// struct AnError;
///
/// impl fmt::Display for AnError {
/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
/// write!(f , "An error")
/// }
/// }
///
/// impl Error for AnError {
/// fn description(&self) -> &str {
/// "Description of an error"
/// }
/// }
///
/// unsafe impl Send for AnError {}
///
/// unsafe impl Sync for AnError {}
///
/// fn main() {
/// let an_error = AnError;
/// assert!(0 == mem::size_of_val(&an_error));
/// let a_boxed_error = Box::<Error + Send + Sync>::from(an_error);
/// assert!(
/// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
/// }
/// ```
fn from(err: E) -> Box<dyn Error + Send + Sync + 'a> {
Box::new(err)
}
Expand All @@ -170,6 +235,20 @@ impl<'a, E: Error + Send + Sync + 'a> From<E> for Box<dyn Error + Send + Sync +
#[stable(feature = "rust1", since = "1.0.0")]
impl From<String> for Box<dyn Error + Send + Sync> {
/// Converts a [`String`] into a box of dyn [`Error`] + [`Send`] + [`Sync`].
///
/// # Examples
///
/// ```
/// use std::error::Error;
/// use std::mem;
///
/// fn main() {
/// let a_string_error = "a string error".to_string();
/// let a_boxed_error = Box::<Error + Send + Sync>::from(a_string_error);
/// assert!(
/// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
/// }
/// ```
fn from(err: String) -> Box<dyn Error + Send + Sync> {
#[derive(Debug)]
struct StringError(String);
Expand All @@ -191,6 +270,19 @@ impl From<String> for Box<dyn Error + Send + Sync> {
#[stable(feature = "string_box_error", since = "1.6.0")]
impl From<String> for Box<dyn Error> {
/// Converts a [`String`] into a box of dyn [`Error`].
///
/// # Examples
///
/// ```
/// use std::error::Error;
/// use std::mem;
///
/// fn main() {
/// let a_string_error = "a string error".to_string();
/// let a_boxed_error = Box::<Error>::from(a_string_error);
/// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
/// }
/// ```
fn from(str_err: String) -> Box<dyn Error> {
let err1: Box<dyn Error + Send + Sync> = From::from(str_err);
let err2: Box<dyn Error> = err1;
Expand All @@ -201,6 +293,20 @@ impl From<String> for Box<dyn Error> {
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, 'b> From<&'b str> for Box<dyn Error + Send + Sync + 'a> {
/// Converts a [`str`] into a box of dyn [`Error`] + [`Send`] + [`Sync`].
///
/// # Examples
///
/// ```
/// use std::error::Error;
/// use std::mem;
///
/// fn main() {
/// let a_str_error = "a str error";
/// let a_boxed_error = Box::<Error + Send + Sync>::from(a_str_error);
/// assert!(
/// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
/// }
/// ```
fn from(err: &'b str) -> Box<dyn Error + Send + Sync + 'a> {
From::from(String::from(err))
}
Expand All @@ -209,6 +315,19 @@ impl<'a, 'b> From<&'b str> for Box<dyn Error + Send + Sync + 'a> {
#[stable(feature = "string_box_error", since = "1.6.0")]
impl<'a> From<&'a str> for Box<dyn Error> {
/// Converts a [`str`] into a box of dyn [`Error`].
///
/// # Examples
///
/// ```
/// use std::error::Error;
/// use std::mem;
///
/// fn main() {
/// let a_str_error = "a str error";
/// let a_boxed_error = Box::<Error>::from(a_str_error);
/// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
/// }
/// ```
fn from(err: &'a str) -> Box<dyn Error> {
From::from(String::from(err))
}
Expand All @@ -217,6 +336,21 @@ impl<'a> From<&'a str> for Box<dyn Error> {
#[stable(feature = "cow_box_error", since = "1.22.0")]
impl<'a, 'b> From<Cow<'b, str>> for Box<dyn Error + Send + Sync + 'a> {
/// Converts a [`Cow`] into a box of dyn [`Error`] + [`Send`] + [`Sync`].
///
/// # Examples
///
/// ```
/// use std::error::Error;
/// use std::mem;
/// use std::borrow::Cow;
///
/// fn main() {
/// let a_cow_str_error = Cow::from("a str error");
/// let a_boxed_error = Box::<Error + Send + Sync>::from(a_cow_str_error);
/// assert!(
/// mem::size_of::<Box<dyn Error + Send + Sync>>() == mem::size_of_val(&a_boxed_error))
/// }
/// ```
fn from(err: Cow<'b, str>) -> Box<dyn Error + Send + Sync + 'a> {
From::from(String::from(err))
}
Expand All @@ -225,6 +359,20 @@ impl<'a, 'b> From<Cow<'b, str>> for Box<dyn Error + Send + Sync + 'a> {
#[stable(feature = "cow_box_error", since = "1.22.0")]
impl<'a> From<Cow<'a, str>> for Box<dyn Error> {
/// Converts a [`Cow`] into a box of dyn [`Error`].
///
/// # Examples
///
/// ```
/// use std::error::Error;
/// use std::mem;
/// use std::borrow::Cow;
///
/// fn main() {
/// let a_cow_str_error = Cow::from("a str error");
/// let a_boxed_error = Box::<Error>::from(a_cow_str_error);
/// assert!(mem::size_of::<Box<dyn Error>>() == mem::size_of_val(&a_boxed_error))
/// }
/// ```
fn from(err: Cow<'a, str>) -> Box<dyn Error> {
From::from(String::from(err))
}
Expand Down