|
1 | 1 | #![allow(missing_debug_implementations)]
|
2 | 2 | #![unstable(feature = "fmt_internals", reason = "internal to format_args!", issue = "none")]
|
3 | 3 |
|
4 |
| -//! These are the lang items used by format_args!(). |
| 4 | +//! All types and methods in this file are used by the compiler in |
| 5 | +//! the expansion/lowering of format_args!(). |
| 6 | +//! |
| 7 | +//! Do not modify them without understanding the consequences for the format_args!() macro. |
5 | 8 |
|
6 | 9 | use super::*;
|
7 | 10 | use crate::hint::unreachable_unchecked;
|
@@ -229,3 +232,43 @@ impl UnsafeArg {
|
229 | 232 | Self { _private: () }
|
230 | 233 | }
|
231 | 234 | }
|
| 235 | + |
| 236 | +/// Used by the format_args!() macro to create a fmt::Arguments object. |
| 237 | +#[doc(hidden)] |
| 238 | +#[unstable(feature = "fmt_internals", issue = "none")] |
| 239 | +#[rustc_diagnostic_item = "FmtArgumentsNew"] |
| 240 | +impl<'a> Arguments<'a> { |
| 241 | + #[inline] |
| 242 | + pub const fn new_const<const N: usize>(pieces: &'a [&'static str; N]) -> Self { |
| 243 | + const { assert!(N <= 1) }; |
| 244 | + Arguments { pieces, fmt: None, args: &[] } |
| 245 | + } |
| 246 | + |
| 247 | + /// When using the format_args!() macro, this function is used to generate the |
| 248 | + /// Arguments structure. |
| 249 | + #[inline] |
| 250 | + pub const fn new_v1<const P: usize, const A: usize>( |
| 251 | + pieces: &'a [&'static str; P], |
| 252 | + args: &'a [rt::Argument<'a>; A], |
| 253 | + ) -> Arguments<'a> { |
| 254 | + const { assert!(P >= A && P <= A + 1, "invalid args") } |
| 255 | + Arguments { pieces, fmt: None, args } |
| 256 | + } |
| 257 | + |
| 258 | + /// Specifies nonstandard formatting parameters. |
| 259 | + /// |
| 260 | + /// An `rt::UnsafeArg` is required because the following invariants must be held |
| 261 | + /// in order for this function to be safe: |
| 262 | + /// 1. The `pieces` slice must be at least as long as `fmt`. |
| 263 | + /// 2. Every `rt::Placeholder::position` value within `fmt` must be a valid index of `args`. |
| 264 | + /// 3. Every `rt::Count::Param` within `fmt` must contain a valid index of `args`. |
| 265 | + #[inline] |
| 266 | + pub const fn new_v1_formatted( |
| 267 | + pieces: &'a [&'static str], |
| 268 | + args: &'a [rt::Argument<'a>], |
| 269 | + fmt: &'a [rt::Placeholder], |
| 270 | + _unsafe_arg: rt::UnsafeArg, |
| 271 | + ) -> Arguments<'a> { |
| 272 | + Arguments { pieces, fmt: Some(fmt), args } |
| 273 | + } |
| 274 | +} |
0 commit comments