Skip to content

Commit f4ef07c

Browse files
committed
Get piece unchecked in write
1 parent 975bc18 commit f4ef07c

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

library/core/src/fmt/mod.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use crate::cell::{Cell, Ref, RefCell, RefMut, UnsafeCell};
66
use crate::char::EscapeDebugExtArgs;
7-
use crate::iter;
87
use crate::marker::PhantomData;
98
use crate::mem;
109
use crate::num::fmt as numfmt;
@@ -1141,7 +1140,10 @@ pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result {
11411140
match args.fmt {
11421141
None => {
11431142
// We can use default formatting parameters for all arguments.
1144-
for (arg, piece) in iter::zip(args.args, args.pieces) {
1143+
for (i, arg) in args.args.iter().enumerate() {
1144+
// SAFETY: args.args and args.pieces come from the same Arguments,
1145+
// which guarantees the indexes are always within bounds.
1146+
let piece = unsafe { args.pieces.get_unchecked(i) };
11451147
if !piece.is_empty() {
11461148
formatter.buf.write_str(*piece)?;
11471149
}
@@ -1152,7 +1154,10 @@ pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result {
11521154
Some(fmt) => {
11531155
// Every spec has a corresponding argument that is preceded by
11541156
// a string piece.
1155-
for (arg, piece) in iter::zip(fmt, args.pieces) {
1157+
for (i, arg) in fmt.iter().enumerate() {
1158+
// SAFETY: fmt and args.pieces come from the same Arguments,
1159+
// which guarantees the indexes are always within bounds.
1160+
let piece = unsafe { args.pieces.get_unchecked(i) };
11561161
if !piece.is_empty() {
11571162
formatter.buf.write_str(*piece)?;
11581163
}

0 commit comments

Comments
 (0)