Skip to content

Commit

Permalink
Move the checks for Arguments constructors to inline const
Browse files Browse the repository at this point in the history
  • Loading branch information
saethlin committed May 25, 2024
1 parent a365890 commit 29a1b3b
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions core/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,23 +338,19 @@ pub struct Arguments<'a> {
impl<'a> Arguments<'a> {
#[inline]
#[rustc_const_unstable(feature = "const_fmt_arguments_new", issue = "none")]
pub const fn new_const(pieces: &'a [&'static str]) -> Self {
if pieces.len() > 1 {
// Since panic!() expands to panic_fmt(format_args!()), using panic! here is both a
// bit silly and also significantly increases the amount of MIR generated by panics.
crate::panicking::panic_nounwind("invalid args");
}
pub const fn new_const<const N: usize>(pieces: &'a [&'static str; N]) -> Self {
const { assert!(N <= 1) };
Arguments { pieces, fmt: None, args: &[] }
}

/// When using the format_args!() macro, this function is used to generate the
/// Arguments structure.
#[inline]
pub fn new_v1(pieces: &'a [&'static str], args: &'a [rt::Argument<'a>]) -> Arguments<'a> {
if pieces.len() < args.len() || pieces.len() > args.len() + 1 {
// See Arguments::new_const for why we don't use panic!.
crate::panicking::panic_nounwind("invalid args");
}
pub fn new_v1<const P: usize, const A: usize>(
pieces: &'a [&'static str; P],
args: &'a [rt::Argument<'a>; A],
) -> Arguments<'a> {
const { assert!(P >= A && P <= A + 1, "invalid args") }
Arguments { pieces, fmt: None, args }
}

Expand Down

0 comments on commit 29a1b3b

Please sign in to comment.