From b900de0f778c4b66921a71824ed7745724405f45 Mon Sep 17 00:00:00 2001 From: Lena Wildervanck Date: Fri, 6 Mar 2020 10:29:11 +0100 Subject: Implement Error for TryReserveError --- src/liballoc/collections/mod.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/liballoc') diff --git a/src/liballoc/collections/mod.rs b/src/liballoc/collections/mod.rs index 0bb62373fab..9e7e66e657a 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 { -- cgit 1.4.1-3-g733a5 From 599cd683eac7fdb076fe86f2928062e087214a22 Mon Sep 17 00:00:00 2001 From: Lena Wildervanck Date: Wed, 11 Mar 2020 17:30:04 +0100 Subject: Format the match statement --- src/liballoc/collections/mod.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/collections/mod.rs b/src/liballoc/collections/mod.rs index 9e7e66e657a..b2e9b89cbda 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) } } -- cgit 1.4.1-3-g733a5 From 2c90a37969644866d0503c20a94196de3f6bea99 Mon Sep 17 00:00:00 2001 From: Lena Wildervanck Date: Wed, 11 Mar 2020 17:55:14 +0100 Subject: Reformat match statement to make the check pass --- src/liballoc/collections/mod.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'src/liballoc') diff --git a/src/liballoc/collections/mod.rs b/src/liballoc/collections/mod.rs index b2e9b89cbda..6b21e54f66a 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) } -- cgit 1.4.1-3-g733a5