From b900de0f778c4b66921a71824ed7745724405f45 Mon Sep 17 00:00:00 2001 From: Lena Wildervanck Date: Fri, 6 Mar 2020 10:29:11 +0100 Subject: [PATCH 1/4] Implement Error for TryReserveError --- src/liballoc/collections/mod.rs | 17 +++++++++++++++++ src/libstd/error.rs | 7 +++++++ 2 files changed, 24 insertions(+) diff --git a/src/liballoc/collections/mod.rs b/src/liballoc/collections/mod.rs index 0bb62373fab1e..9e7e66e657a93 100644 --- a/src/liballoc/collections/mod.rs +++ b/src/liballoc/collections/mod.rs @@ -42,6 +42,7 @@ pub use linked_list::LinkedList; pub use vec_deque::VecDeque; use crate::alloc::{Layout, LayoutErr}; +use core::fmt::Display; /// The error type for `try_reserve` methods. #[derive(Clone, PartialEq, Eq, Debug)] @@ -77,6 +78,22 @@ impl From for TryReserveError { } } +#[unstable(feature = "try_reserve", reason = "new API", issue = "48043")] +impl Display for TryReserveError { + fn fmt( + &self, + fmt: &mut core::fmt::Formatter<'_>, + ) -> core::result::Result<(), core::fmt::Error> { + fmt.write_str("memory allocation failed")?; + fmt.write_str(match &self { + TryReserveError::CapacityOverflow => { + " because the computed capacity exceeded the collection's maximum" + } + TryReserveError::AllocError { .. } => " because the memory allocator returned a error", + }) + } +} + /// An intermediate trait for specialization of `Extend`. #[doc(hidden)] trait SpecExtend { diff --git a/src/libstd/error.rs b/src/libstd/error.rs index b480581e21ba9..61944edf1422b 100644 --- a/src/libstd/error.rs +++ b/src/libstd/error.rs @@ -552,6 +552,13 @@ impl Error for char::ParseCharError { } } +#[unstable(feature = "try_reserve", reason = "new API", issue = "48043")] +impl Error for alloc::collections::TryReserveError { + fn description(&self) -> &str { + "memory allocation failed" + } +} + // Copied from `any.rs`. impl dyn Error + 'static { /// Returns `true` if the boxed type is the same as `T` From 88f8b881606aa22fdaabcefbf88ddc163e41d8e6 Mon Sep 17 00:00:00 2001 From: Lena Wildervanck Date: Tue, 10 Mar 2020 11:19:40 +0100 Subject: [PATCH 2/4] Remove deprecated description function of TryReserveError --- src/libstd/error.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/libstd/error.rs b/src/libstd/error.rs index 61944edf1422b..ba13a8a06df10 100644 --- a/src/libstd/error.rs +++ b/src/libstd/error.rs @@ -553,11 +553,7 @@ impl Error for char::ParseCharError { } #[unstable(feature = "try_reserve", reason = "new API", issue = "48043")] -impl Error for alloc::collections::TryReserveError { - fn description(&self) -> &str { - "memory allocation failed" - } -} +impl Error for alloc::collections::TryReserveError {} // Copied from `any.rs`. impl dyn Error + 'static { From 599cd683eac7fdb076fe86f2928062e087214a22 Mon Sep 17 00:00:00 2001 From: Lena Wildervanck Date: Wed, 11 Mar 2020 17:30:04 +0100 Subject: [PATCH 3/4] Format the match statement --- src/liballoc/collections/mod.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/liballoc/collections/mod.rs b/src/liballoc/collections/mod.rs index 9e7e66e657a93..b2e9b89cbda5b 100644 --- a/src/liballoc/collections/mod.rs +++ b/src/liballoc/collections/mod.rs @@ -85,12 +85,15 @@ impl Display for TryReserveError { fmt: &mut core::fmt::Formatter<'_>, ) -> core::result::Result<(), core::fmt::Error> { fmt.write_str("memory allocation failed")?; - fmt.write_str(match &self { + let reason = match &self { TryReserveError::CapacityOverflow => { " because the computed capacity exceeded the collection's maximum" } - TryReserveError::AllocError { .. } => " because the memory allocator returned a error", - }) + TryReserveError::AllocError { .. } => { + " because the memory allocator returned a error" + } + }; + fmt.write_str(reason) } } From 2c90a37969644866d0503c20a94196de3f6bea99 Mon Sep 17 00:00:00 2001 From: Lena Wildervanck Date: Wed, 11 Mar 2020 17:55:14 +0100 Subject: [PATCH 4/4] Reformat match statement to make the check pass --- src/liballoc/collections/mod.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/liballoc/collections/mod.rs b/src/liballoc/collections/mod.rs index b2e9b89cbda5b..6b21e54f66aa0 100644 --- a/src/liballoc/collections/mod.rs +++ b/src/liballoc/collections/mod.rs @@ -89,9 +89,7 @@ impl Display for TryReserveError { TryReserveError::CapacityOverflow => { " because the computed capacity exceeded the collection's maximum" } - TryReserveError::AllocError { .. } => { - " because the memory allocator returned a error" - } + TryReserveError::AllocError { .. } => " because the memory allocator returned a error", }; fmt.write_str(reason) }