Skip to content

Commit 2da9856

Browse files
committed
Add internal io::Error::new_const tot avoid allocations.
1 parent f398a49 commit 2da9856

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

library/std/src/io/error.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ impl fmt::Debug for Error {
6969
enum Repr {
7070
Os(i32),
7171
Simple(ErrorKind),
72+
// &str is a fat pointer, but &&str is a thin pointer.
73+
SimpleMessage(ErrorKind, &'static &'static str),
7274
Custom(Box<Custom>),
7375
}
7476

@@ -259,6 +261,18 @@ impl Error {
259261
Error { repr: Repr::Custom(Box::new(Custom { kind, error })) }
260262
}
261263

264+
/// Creates a new I/O error from a known kind of error as well as a
265+
/// constant message.
266+
///
267+
/// This function not allocate.
268+
///
269+
/// This function should maybe change to
270+
/// `new_const<const MSG: &'static str>(kind: ErrorKind)`
271+
/// in the future, when const generics allows that.
272+
pub(crate) const fn new_const(kind: ErrorKind, message: &'static &'static str) -> Error {
273+
Self { repr: Repr::SimpleMessage(kind, message) }
274+
}
275+
262276
/// Returns an error representing the last OS error which occurred.
263277
///
264278
/// This function reads the value of `errno` for the target platform (e.g.
@@ -342,6 +356,7 @@ impl Error {
342356
Repr::Os(i) => Some(i),
343357
Repr::Custom(..) => None,
344358
Repr::Simple(..) => None,
359+
Repr::SimpleMessage(..) => None,
345360
}
346361
}
347362

@@ -377,6 +392,7 @@ impl Error {
377392
match self.repr {
378393
Repr::Os(..) => None,
379394
Repr::Simple(..) => None,
395+
Repr::SimpleMessage(..) => None,
380396
Repr::Custom(ref c) => Some(&*c.error),
381397
}
382398
}
@@ -448,6 +464,7 @@ impl Error {
448464
match self.repr {
449465
Repr::Os(..) => None,
450466
Repr::Simple(..) => None,
467+
Repr::SimpleMessage(..) => None,
451468
Repr::Custom(ref mut c) => Some(&mut *c.error),
452469
}
453470
}
@@ -484,6 +501,7 @@ impl Error {
484501
match self.repr {
485502
Repr::Os(..) => None,
486503
Repr::Simple(..) => None,
504+
Repr::SimpleMessage(..) => None,
487505
Repr::Custom(c) => Some(c.error),
488506
}
489507
}
@@ -512,6 +530,7 @@ impl Error {
512530
Repr::Os(code) => sys::decode_error_kind(code),
513531
Repr::Custom(ref c) => c.kind,
514532
Repr::Simple(kind) => kind,
533+
Repr::SimpleMessage(kind, _) => kind,
515534
}
516535
}
517536
}
@@ -527,6 +546,9 @@ impl fmt::Debug for Repr {
527546
.finish(),
528547
Repr::Custom(ref c) => fmt::Debug::fmt(&c, fmt),
529548
Repr::Simple(kind) => fmt.debug_tuple("Kind").field(&kind).finish(),
549+
Repr::SimpleMessage(kind, &message) => {
550+
fmt.debug_struct("Error").field("kind", &kind).field("message", &message).finish()
551+
}
530552
}
531553
}
532554
}
@@ -541,6 +563,7 @@ impl fmt::Display for Error {
541563
}
542564
Repr::Custom(ref c) => c.error.fmt(fmt),
543565
Repr::Simple(kind) => write!(fmt, "{}", kind.as_str()),
566+
Repr::SimpleMessage(_, &msg) => msg.fmt(fmt),
544567
}
545568
}
546569
}
@@ -551,6 +574,7 @@ impl error::Error for Error {
551574
fn description(&self) -> &str {
552575
match self.repr {
553576
Repr::Os(..) | Repr::Simple(..) => self.kind().as_str(),
577+
Repr::SimpleMessage(_, &msg) => msg,
554578
Repr::Custom(ref c) => c.error.description(),
555579
}
556580
}
@@ -560,6 +584,7 @@ impl error::Error for Error {
560584
match self.repr {
561585
Repr::Os(..) => None,
562586
Repr::Simple(..) => None,
587+
Repr::SimpleMessage(..) => None,
563588
Repr::Custom(ref c) => c.error.cause(),
564589
}
565590
}
@@ -568,6 +593,7 @@ impl error::Error for Error {
568593
match self.repr {
569594
Repr::Os(..) => None,
570595
Repr::Simple(..) => None,
596+
Repr::SimpleMessage(..) => None,
571597
Repr::Custom(ref c) => c.error.source(),
572598
}
573599
}

0 commit comments

Comments
 (0)