Skip to content

Commit

Permalink
Implement From<&'a &'static str> for Arguments<'a>
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Aug 6, 2023
1 parent 11467b1 commit 153c2e4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
9 changes: 9 additions & 0 deletions library/core/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions library/core/tests/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"";
Expand Down

0 comments on commit 153c2e4

Please sign in to comment.