-
Notifications
You must be signed in to change notification settings - Fork 546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose format_into
and format_into_io
for DelayedFormat
#1653
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,7 +97,30 @@ impl<'a, I: Iterator<Item = B> + Clone, B: Borrow<Item<'a>>> DelayedFormat<I> { | |
DelayedFormat { date, time, off: Some(name_and_diff), items, locale } | ||
} | ||
|
||
fn format(&self, w: &mut impl Write) -> fmt::Result { | ||
/// Formats `DelayedFormat` into an `std::io::Write` instance. | ||
/// # Errors | ||
/// This function returns an error if formatting into the `std::io::Write` instance fails. | ||
#[cfg(feature = "std")] | ||
pub fn format_into_io(self, w: &mut impl std::io::Write) -> fmt::Result { | ||
// wrapper to allow reuse of the existing string based | ||
// writers | ||
struct IoWriter<W: std::io::Write> { | ||
writer: W, | ||
} | ||
impl<W: std::io::Write> fmt::Write for IoWriter<W> { | ||
#[inline] | ||
fn write_str(&mut self, s: &str) -> fmt::Result { | ||
self.writer.write_all(s.as_bytes()).map_err(|_| fmt::Error) | ||
} | ||
} | ||
let mut writer = IoWriter { writer: w }; | ||
self.format_into(&mut writer) | ||
} | ||
|
||
/// Formats `DelayedFormat` into a `core::fmt::Write` instance. | ||
/// # Errors | ||
/// This function returns an error if formatting into the `core::fmt::Write` instance fails. | ||
pub fn format_into(&self, w: &mut impl Write) -> fmt::Result { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggest we call this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
for item in self.items.clone() { | ||
match *item.borrow() { | ||
Item::Literal(s) | Item::Space(s) => w.write_str(s), | ||
|
@@ -321,7 +344,7 @@ impl<'a, I: Iterator<Item = B> + Clone, B: Borrow<Item<'a>>> DelayedFormat<I> { | |
impl<'a, I: Iterator<Item = B> + Clone, B: Borrow<Item<'a>>> Display for DelayedFormat<I> { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
let mut result = String::new(); | ||
self.format(&mut result)?; | ||
self.format_into(&mut result)?; | ||
f.pad(&result) | ||
} | ||
} | ||
|
@@ -353,7 +376,7 @@ where | |
|
||
/// Formats single formatting item | ||
#[cfg(feature = "alloc")] | ||
#[deprecated(since = "0.4.32", note = "Use DelayedFormat::fmt instead")] | ||
#[deprecated(since = "0.4.32", note = "Use DelayedFormat::fmt or DelayedFormat::format instead")] | ||
pub fn format_item( | ||
w: &mut fmt::Formatter, | ||
date: Option<&NaiveDate>, | ||
|
@@ -611,6 +634,41 @@ mod tests { | |
#[cfg(feature = "alloc")] | ||
use crate::{NaiveDate, NaiveTime, TimeZone, Timelike, Utc}; | ||
|
||
#[cfg(all(feature = "std", feature = "alloc"))] | ||
#[test] | ||
fn test_delayed_format_into_io_matches_format_into() { | ||
let dt = crate::DateTime::from_timestamp(1643723400, 123456789).unwrap(); | ||
let df = dt.format("%Y-%m-%d %H:%M:%S%.9f"); | ||
|
||
let mut dt_str = String::new(); | ||
let mut dt_vec_str = Vec::new(); | ||
|
||
df.format_into(&mut dt_str).unwrap(); | ||
df.format_into_io(&mut dt_vec_str).unwrap(); | ||
|
||
assert_eq!(dt_str, String::from_utf8(dt_vec_str).unwrap()); | ||
assert_eq!(dt_str, "2022-02-01 13:50:00.123456789"); | ||
} | ||
|
||
#[cfg(all(feature = "std", feature = "unstable-locales", feature = "alloc"))] | ||
#[test] | ||
fn test_with_locale_delayed_format_into_io_matches_format_into() { | ||
use crate::format::locales::Locale; | ||
use crate::DateTime; | ||
|
||
let dt = DateTime::from_timestamp(1643723400, 123456789).unwrap(); | ||
let df = dt.format_localized("%A, %B %d, %Y", Locale::ja_JP); | ||
|
||
let mut dt_str = String::new(); | ||
let mut dt_vec_str = Vec::new(); | ||
|
||
df.format_into(&mut dt_str).unwrap(); | ||
df.format_into_io(&mut dt_vec_str).unwrap(); | ||
|
||
assert_eq!(dt_str, String::from_utf8(dt_vec_str).unwrap()); | ||
assert_eq!(dt_str, "火曜日, 2月 01, 2022"); | ||
} | ||
|
||
#[test] | ||
#[cfg(feature = "alloc")] | ||
fn test_date_format() { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really interested in taking this. Callers can construct their own adapter if that is important to them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
made it into an example instead.