From ddff2ed649340fc07d3f6a3357c85b61d4b22b5f Mon Sep 17 00:00:00 2001 From: Shotaro Yamada Date: Sun, 6 Jan 2019 15:33:11 +0900 Subject: [PATCH 1/3] Remove unnecessary adapter --- src/libcore/fmt/mod.rs | 25 ++----------------------- 1 file changed, 2 insertions(+), 23 deletions(-) diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index ec1aeb8a7d1e9..bb6f511acf7e5 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -191,29 +191,8 @@ pub trait Write { /// assert_eq!(&buf, "world"); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - fn write_fmt(&mut self, args: Arguments) -> Result { - // This Adapter is needed to allow `self` (of type `&mut - // Self`) to be cast to a Write (below) without - // requiring a `Sized` bound. - struct Adapter<'a,T: ?Sized +'a>(&'a mut T); - - impl Write for Adapter<'_, T> - where T: Write - { - fn write_str(&mut self, s: &str) -> Result { - self.0.write_str(s) - } - - fn write_char(&mut self, c: char) -> Result { - self.0.write_char(c) - } - - fn write_fmt(&mut self, args: Arguments) -> Result { - self.0.write_fmt(args) - } - } - - write(&mut Adapter(self), args) + fn write_fmt(mut self: &mut Self, args: Arguments) -> Result { + write(&mut self, args) } } From f67124245cc24babd0345347a77089ba698acd14 Mon Sep 17 00:00:00 2001 From: Shotaro Yamada Date: Sun, 6 Jan 2019 15:33:42 +0900 Subject: [PATCH 2/3] Derive Clone for ArgumentV1 manual impl was a workaround for #28229. --- src/libcore/fmt/mod.rs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index bb6f511acf7e5..0127277d9cfc8 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -247,7 +247,7 @@ struct Void { /// family of functions. It contains a function to format the given value. At /// compile time it is ensured that the function and the value have the correct /// types, and then this struct is used to canonicalize arguments to one type. -#[derive(Copy)] +#[derive(Copy, Clone)] #[allow(missing_debug_implementations)] #[unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "0")] @@ -257,14 +257,6 @@ pub struct ArgumentV1<'a> { formatter: fn(&Void, &mut Formatter) -> Result, } -#[unstable(feature = "fmt_internals", reason = "internal to format_args!", - issue = "0")] -impl Clone for ArgumentV1<'_> { - fn clone(&self) -> Self { - *self - } -} - impl<'a> ArgumentV1<'a> { #[inline(never)] fn show_usize(x: &usize, f: &mut Formatter) -> Result { From 12ae3651f891d22f804c67923d02cbdfa10fa60d Mon Sep 17 00:00:00 2001 From: Shotaro Yamada Date: Sun, 6 Jan 2019 15:36:14 +0900 Subject: [PATCH 3/3] Misc cleanups --- src/libcore/fmt/mod.rs | 12 ++++++------ src/libcore/str/pattern.rs | 3 +-- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index 0127277d9cfc8..214b5d3a84f24 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -1076,7 +1076,7 @@ impl<'a> Formatter<'a> { self.args[i].as_usize() } rt::v1::Count::NextParam => { - self.curarg.next().and_then(|arg| arg.as_usize()) + self.curarg.next()?.as_usize() } } } @@ -1142,15 +1142,15 @@ impl<'a> Formatter<'a> { sign = Some('+'); width += 1; } - let mut prefixed = false; - if self.alternate() { - prefixed = true; width += prefix.chars().count(); + let prefixed = self.alternate(); + if prefixed { + width += prefix.chars().count(); } // Writes the sign if it exists, and then the prefix if it was requested let write_prefix = |f: &mut Formatter| { if let Some(c) = sign { - f.buf.write_str(c.encode_utf8(&mut [0; 4]))?; + f.buf.write_char(c)?; } if prefixed { f.buf.write_str(prefix) } else { Ok(()) } @@ -1312,7 +1312,7 @@ impl<'a> Formatter<'a> { // remove the sign from the formatted parts formatted.sign = b""; - width = if width < sign.len() { 0 } else { width - sign.len() }; + width = width.saturating_sub(sign.len()); align = rt::v1::Alignment::Right; self.fill = '0'; self.align = rt::v1::Alignment::Right; diff --git a/src/libcore/str/pattern.rs b/src/libcore/str/pattern.rs index b4eae4d1bb742..55a7ba181e527 100644 --- a/src/libcore/str/pattern.rs +++ b/src/libcore/str/pattern.rs @@ -425,8 +425,7 @@ impl<'a> Pattern<'a> for char { #[inline] fn into_searcher(self, haystack: &'a str) -> Self::Searcher { let mut utf8_encoded = [0; 4]; - self.encode_utf8(&mut utf8_encoded); - let utf8_size = self.len_utf8(); + let utf8_size = self.encode_utf8(&mut utf8_encoded).len(); CharSearcher { haystack, finger: 0,