Skip to content

Implement int_format_into feature #142098

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions library/alloctests/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#![feature(downcast_unchecked)]
#![feature(exact_size_is_empty)]
#![feature(hashmap_internals)]
#![feature(int_format_into)]
#![feature(linked_list_cursors)]
#![feature(map_try_insert)]
#![feature(pattern)]
Expand Down
46 changes: 26 additions & 20 deletions library/alloctests/tests/num.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
use std::fmt::{Debug, Display};
use core::fmt::NumBuffer;
use std::str::FromStr;

fn assert_nb<Int: ToString + FromStr + Debug + Display + Eq>(value: Int) {
let s = value.to_string();
let s2 = format!("s: {}.", value);
macro_rules! assert_nb {
($int:ident, $value:expr) => {
let value: $int = $value;
let s = value.to_string();
let s2 = format!("s: {}.", value);

assert_eq!(format!("s: {s}."), s2);
let Ok(ret) = Int::from_str(&s) else {
panic!("failed to convert into to string");
assert_eq!(format!("s: {s}."), s2);
let Ok(ret) = $int::from_str(&s) else {
panic!("failed to convert into to string");
};
assert_eq!(ret, value);

let mut buffer = NumBuffer::<$int>::new();
assert_eq!(value.format_into(&mut buffer), s.as_str());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since format_into is not part of a trait, I needed to switch from a function to a macro to avoid the generics limitations.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could write and implement a trait right here, in the test - right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could indeed, but would be better than the current code?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Less churn; other than that I don't have strong feelings.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If someone else agrees with you I'll make the change (lazyness++ 😆).

};
assert_eq!(ret, value);
}

macro_rules! uint_to_s {
($($fn_name:ident, $int:ident,)+) => {
$(
#[test]
fn $fn_name() {
assert_nb::<$int>($int::MIN);
assert_nb::<$int>($int::MAX);
assert_nb::<$int>(1);
assert_nb::<$int>($int::MIN / 2);
assert_nb::<$int>($int::MAX / 2);
assert_nb!($int, $int::MIN);
assert_nb!($int, $int::MAX);
assert_nb!($int, 1);
assert_nb!($int, $int::MIN / 2);
assert_nb!($int, $int::MAX / 2);
}
)+
}
Expand All @@ -31,13 +37,13 @@ macro_rules! int_to_s {
$(
#[test]
fn $fn_name() {
assert_nb::<$int>($int::MIN);
assert_nb::<$int>($int::MAX);
assert_nb::<$int>(1);
assert_nb::<$int>(0);
assert_nb::<$int>(-1);
assert_nb::<$int>($int::MIN / 2);
assert_nb::<$int>($int::MAX / 2);
assert_nb!($int, $int::MIN);
assert_nb!($int, $int::MAX);
assert_nb!($int, 1);
assert_nb!($int, 0);
assert_nb!($int, -1);
assert_nb!($int, $int::MIN / 2);
assert_nb!($int, $int::MAX / 2);
}
)+
}
Expand Down
4 changes: 4 additions & 0 deletions library/core/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod float;
#[cfg(no_fp_fmt_parse)]
mod nofloat;
mod num;
mod num_buffer;
mod rt;

#[stable(feature = "fmt_flags_align", since = "1.28.0")]
Expand All @@ -33,6 +34,9 @@ pub enum Alignment {
Center,
}

#[unstable(feature = "int_format_into", issue = "138215")]
pub use num_buffer::{NumBuffer, NumBufferTrait};

#[stable(feature = "debug_builders", since = "1.2.0")]
pub use self::builders::{DebugList, DebugMap, DebugSet, DebugStruct, DebugTuple};
#[unstable(feature = "debug_closure_helpers", issue = "117729")]
Expand Down
Loading
Loading