Skip to content

Commit b7093e5

Browse files
authoredJan 13, 2019
Rollup merge of rust-lang#57454 - sinkuu:fmt_cleanup, r=joshtriplett
Some cleanups for core::fmt
2 parents 36c7cde + 12ae365 commit b7093e5

File tree

2 files changed

+10
-40
lines changed

2 files changed

+10
-40
lines changed
 

‎src/libcore/fmt/mod.rs

+9-38
Original file line numberDiff line numberDiff line change
@@ -191,29 +191,8 @@ pub trait Write {
191191
/// assert_eq!(&buf, "world");
192192
/// ```
193193
#[stable(feature = "rust1", since = "1.0.0")]
194-
fn write_fmt(&mut self, args: Arguments) -> Result {
195-
// This Adapter is needed to allow `self` (of type `&mut
196-
// Self`) to be cast to a Write (below) without
197-
// requiring a `Sized` bound.
198-
struct Adapter<'a,T: ?Sized +'a>(&'a mut T);
199-
200-
impl<T: ?Sized> Write for Adapter<'_, T>
201-
where T: Write
202-
{
203-
fn write_str(&mut self, s: &str) -> Result {
204-
self.0.write_str(s)
205-
}
206-
207-
fn write_char(&mut self, c: char) -> Result {
208-
self.0.write_char(c)
209-
}
210-
211-
fn write_fmt(&mut self, args: Arguments) -> Result {
212-
self.0.write_fmt(args)
213-
}
214-
}
215-
216-
write(&mut Adapter(self), args)
194+
fn write_fmt(mut self: &mut Self, args: Arguments) -> Result {
195+
write(&mut self, args)
217196
}
218197
}
219198

@@ -268,7 +247,7 @@ struct Void {
268247
/// family of functions. It contains a function to format the given value. At
269248
/// compile time it is ensured that the function and the value have the correct
270249
/// types, and then this struct is used to canonicalize arguments to one type.
271-
#[derive(Copy)]
250+
#[derive(Copy, Clone)]
272251
#[allow(missing_debug_implementations)]
273252
#[unstable(feature = "fmt_internals", reason = "internal to format_args!",
274253
issue = "0")]
@@ -278,14 +257,6 @@ pub struct ArgumentV1<'a> {
278257
formatter: fn(&Void, &mut Formatter) -> Result,
279258
}
280259

281-
#[unstable(feature = "fmt_internals", reason = "internal to format_args!",
282-
issue = "0")]
283-
impl Clone for ArgumentV1<'_> {
284-
fn clone(&self) -> Self {
285-
*self
286-
}
287-
}
288-
289260
impl<'a> ArgumentV1<'a> {
290261
#[inline(never)]
291262
fn show_usize(x: &usize, f: &mut Formatter) -> Result {
@@ -1105,7 +1076,7 @@ impl<'a> Formatter<'a> {
11051076
self.args[i].as_usize()
11061077
}
11071078
rt::v1::Count::NextParam => {
1108-
self.curarg.next().and_then(|arg| arg.as_usize())
1079+
self.curarg.next()?.as_usize()
11091080
}
11101081
}
11111082
}
@@ -1171,15 +1142,15 @@ impl<'a> Formatter<'a> {
11711142
sign = Some('+'); width += 1;
11721143
}
11731144

1174-
let mut prefixed = false;
1175-
if self.alternate() {
1176-
prefixed = true; width += prefix.chars().count();
1145+
let prefixed = self.alternate();
1146+
if prefixed {
1147+
width += prefix.chars().count();
11771148
}
11781149

11791150
// Writes the sign if it exists, and then the prefix if it was requested
11801151
let write_prefix = |f: &mut Formatter| {
11811152
if let Some(c) = sign {
1182-
f.buf.write_str(c.encode_utf8(&mut [0; 4]))?;
1153+
f.buf.write_char(c)?;
11831154
}
11841155
if prefixed { f.buf.write_str(prefix) }
11851156
else { Ok(()) }
@@ -1341,7 +1312,7 @@ impl<'a> Formatter<'a> {
13411312

13421313
// remove the sign from the formatted parts
13431314
formatted.sign = b"";
1344-
width = if width < sign.len() { 0 } else { width - sign.len() };
1315+
width = width.saturating_sub(sign.len());
13451316
align = rt::v1::Alignment::Right;
13461317
self.fill = '0';
13471318
self.align = rt::v1::Alignment::Right;

‎src/libcore/str/pattern.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,7 @@ impl<'a> Pattern<'a> for char {
425425
#[inline]
426426
fn into_searcher(self, haystack: &'a str) -> Self::Searcher {
427427
let mut utf8_encoded = [0; 4];
428-
self.encode_utf8(&mut utf8_encoded);
429-
let utf8_size = self.len_utf8();
428+
let utf8_size = self.encode_utf8(&mut utf8_encoded).len();
430429
CharSearcher {
431430
haystack,
432431
finger: 0,

0 commit comments

Comments
 (0)
Please sign in to comment.