From 153c2e4750e89d1010f63628a786d4eca03057a0 Mon Sep 17 00:00:00 2001 From: EFanZh Date: Sun, 6 Aug 2023 16:35:08 +0800 Subject: [PATCH] Implement `From<&'a &'static str>` for `Arguments<'a>` --- library/core/src/fmt/mod.rs | 9 +++++++++ library/core/tests/fmt/mod.rs | 7 +++++++ 2 files changed, 16 insertions(+) diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs index 9ce6093f1d1f3..012003be3b867 100644 --- a/library/core/src/fmt/mod.rs +++ b/library/core/src/fmt/mod.rs @@ -10,6 +10,7 @@ use crate::mem; use crate::num::fmt as numfmt; use crate::ops::Deref; use crate::result; +use crate::slice; use crate::str; mod builders; @@ -422,6 +423,14 @@ impl Display for Arguments<'_> { } } +#[stable(feature = "rust1", since = "1.0.0")] +impl<'a> From<&'a &'static str> for Arguments<'a> { + #[inline] + fn from(value: &'a &'static str) -> Self { + Self::new_const(slice::from_ref(value)) + } +} + /// `?` formatting. /// /// `Debug` should format the output in a programmer-facing, debugging context. diff --git a/library/core/tests/fmt/mod.rs b/library/core/tests/fmt/mod.rs index c1c80c46c78b7..5430d473eed0a 100644 --- a/library/core/tests/fmt/mod.rs +++ b/library/core/tests/fmt/mod.rs @@ -11,6 +11,13 @@ fn test_format_flags() { assert_eq!(format!("{: >3}", 'a'), " a"); } +#[test] +fn test_arguments_from_str() { + assert_eq!(core::fmt::Arguments::from(&"a string literal").to_string(), "a string literal"); + + assert_eq!(core::fmt::Arguments::from(&"a string literal").as_str(), Some("a string literal")); +} + #[test] fn test_pointer_formats_data_pointer() { let b: &[u8] = b"";