mirror of
https://github.com/rust-lang/rust.git
synced 2025-10-21 04:17:35 +00:00

Implementation of `fmt::FormattingOptions` Tracking issue: #118117 Public API: ```rust #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct FormattingOptions { … } #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum Sign { Plus, Minus } #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum DebugAsHex { Lower, Upper } impl FormattingOptions { pub fn new() -> Self; pub fn sign(&mut self, sign: Option<Sign>) -> &mut Self; pub fn sign_aware_zero_pad(&mut self, sign_aware_zero_pad: bool) -> &mut Self; pub fn alternate(&mut self, alternate: bool) -> &mut Self; pub fn fill(&mut self, fill: char) -> &mut Self; pub fn align(&mut self, alignment: Option<Alignment>) -> &mut Self; pub fn width(&mut self, width: Option<usize>) -> &mut Self; pub fn precision(&mut self, precision: Option<usize>) -> &mut Self; pub fn debug_as_hex(&mut self, debug_as_hex: Option<DebugAsHex>) -> &mut Self; pub fn get_sign(&self) -> Option<Sign>; pub fn get_sign_aware_zero_pad(&self) -> bool; pub fn get_alternate(&self) -> bool; pub fn get_fill(&self) -> char; pub fn get_align(&self) -> Option<Alignment>; pub fn get_width(&self) -> Option<usize>; pub fn get_precision(&self) -> Option<usize>; pub fn get_debug_as_hex(&self) -> Option<DebugAsHex>; pub fn create_formatter<'a>(self, write: &'a mut (dyn Write + 'a)) -> Formatter<'a>; } impl<'a> Formatter<'a> { pub fn new(write: &'a mut (dyn Write + 'a), options: FormattingOptions) -> Self; pub fn with_options<'b>(&'b mut self, options: FormattingOptions) -> Formatter<'b>; pub fn sign(&self) -> Option<Sign>; pub fn options(&self) -> FormattingOptions; } ``` Relevant changes from the public API in the tracking issue (I'm leaving out some stuff I consider obvious mistakes, like missing `#[derive(..)]`s and `pub` specifiers): - `enum DebugAsHex`/`FormattingOptions::debug_as_hex`/`FormattingOptions::get_debug_as_hex`: To support `{:x?}` as well as `{:X?}`. I had completely missed these options in the ACP. I'm open for any and all bikeshedding, not married to the name. - `fill`/`get_fill` now takes/returns `char` instead of `Option<char>`. This simply mirrors what `Formatter::fill` returns (with default being `' '`). - Changed `zero_pad`/`get_zero_pad` to `sign_aware_zero_pad`/`get_sign_aware_zero_pad`. This also mirrors `Formatter::sign_aware_zero_pad`. While I'm not a fan of this quite verbose name, I do believe that having the interface of `Formatter` and `FormattingOptions` be compatible is more important. - For the same reason, renamed `alignment`/`get_alignment` to `aling`/`get_align`. - Deviating from my initial idea, `Formatter::with_options` returns a `Formatter` which has the lifetime of the `self` reference as its generic lifetime parameter (in the original API spec, the generic lifetime of the returned `Formatter` was the generic lifetime used by `self` instead). Otherwise, one could construct two `Formatter`s that both mutably borrow the same underlying buffer, which would be unsound. This solution still has performance benefits over simply using `Formatter::new`, so I believe it is worthwhile to keep this method.
189 lines
4.8 KiB
Rust
189 lines
4.8 KiB
Rust
// tidy-alphabetical-start
|
|
#![cfg_attr(target_has_atomic = "128", feature(integer_atomics))]
|
|
#![cfg_attr(test, feature(cfg_match))]
|
|
#![feature(alloc_layout_extra)]
|
|
#![feature(array_chunks)]
|
|
#![feature(array_ptr_get)]
|
|
#![feature(array_try_from_fn)]
|
|
#![feature(array_windows)]
|
|
#![feature(ascii_char)]
|
|
#![feature(ascii_char_variants)]
|
|
#![feature(async_iter_from_iter)]
|
|
#![feature(async_iterator)]
|
|
#![feature(bigint_helper_methods)]
|
|
#![feature(cell_update)]
|
|
#![feature(clone_to_uninit)]
|
|
#![feature(const_black_box)]
|
|
#![feature(const_eval_select)]
|
|
#![feature(const_nonnull_new)]
|
|
#![feature(const_swap)]
|
|
#![feature(const_trait_impl)]
|
|
#![feature(core_intrinsics)]
|
|
#![feature(core_io_borrowed_buf)]
|
|
#![feature(core_private_bignum)]
|
|
#![feature(core_private_diy_float)]
|
|
#![feature(dec2flt)]
|
|
#![feature(duration_constants)]
|
|
#![feature(duration_constructors)]
|
|
#![feature(error_generic_member_access)]
|
|
#![feature(exact_size_is_empty)]
|
|
#![feature(extern_types)]
|
|
#![feature(float_minimum_maximum)]
|
|
#![feature(flt2dec)]
|
|
#![feature(fmt_internals)]
|
|
#![feature(formatting_options)]
|
|
#![feature(freeze)]
|
|
#![feature(future_join)]
|
|
#![feature(generic_assert_internals)]
|
|
#![feature(get_many_mut)]
|
|
#![feature(hasher_prefixfree_extras)]
|
|
#![feature(hashmap_internals)]
|
|
#![feature(inline_const_pat)]
|
|
#![feature(int_roundings)]
|
|
#![feature(ip)]
|
|
#![feature(ip_from)]
|
|
#![feature(is_ascii_octdigit)]
|
|
#![feature(iter_advance_by)]
|
|
#![feature(iter_array_chunks)]
|
|
#![feature(iter_chain)]
|
|
#![feature(iter_collect_into)]
|
|
#![feature(iter_intersperse)]
|
|
#![feature(iter_is_partitioned)]
|
|
#![feature(iter_map_windows)]
|
|
#![feature(iter_next_chunk)]
|
|
#![feature(iter_order_by)]
|
|
#![feature(iter_partition_in_place)]
|
|
#![feature(iterator_try_collect)]
|
|
#![feature(iterator_try_reduce)]
|
|
#![feature(layout_for_ptr)]
|
|
#![feature(lazy_get)]
|
|
#![feature(maybe_uninit_fill)]
|
|
#![feature(maybe_uninit_uninit_array_transpose)]
|
|
#![feature(maybe_uninit_write_slice)]
|
|
#![feature(min_specialization)]
|
|
#![feature(never_type)]
|
|
#![feature(num_midpoint_signed)]
|
|
#![feature(numfmt)]
|
|
#![feature(pattern)]
|
|
#![feature(pointer_is_aligned_to)]
|
|
#![feature(portable_simd)]
|
|
#![feature(ptr_metadata)]
|
|
#![feature(slice_from_ptr_range)]
|
|
#![feature(slice_internals)]
|
|
#![feature(slice_partition_dedup)]
|
|
#![feature(slice_split_once)]
|
|
#![feature(slice_take)]
|
|
#![feature(split_array)]
|
|
#![feature(split_as_slice)]
|
|
#![feature(std_internals)]
|
|
#![feature(step_trait)]
|
|
#![feature(str_internals)]
|
|
#![feature(strict_provenance_atomic_ptr)]
|
|
#![feature(strict_provenance_lints)]
|
|
#![feature(test)]
|
|
#![feature(trait_upcasting)]
|
|
#![feature(trusted_len)]
|
|
#![feature(trusted_random_access)]
|
|
#![feature(try_blocks)]
|
|
#![feature(try_find)]
|
|
#![feature(try_trait_v2)]
|
|
#![feature(unsigned_is_multiple_of)]
|
|
#![feature(unsize)]
|
|
#![feature(unsized_tuple_coercion)]
|
|
#![feature(unwrap_infallible)]
|
|
// tidy-alphabetical-end
|
|
#![allow(internal_features)]
|
|
#![deny(fuzzy_provenance_casts)]
|
|
#![deny(unsafe_op_in_unsafe_fn)]
|
|
|
|
/// Version of `assert_matches` that ignores fancy runtime printing in const context and uses structural equality.
|
|
macro_rules! assert_eq_const_safe {
|
|
($left:expr, $right:expr$(, $($arg:tt)+)?) => {
|
|
{
|
|
fn runtime() {
|
|
assert_eq!($left, $right, $($arg)*);
|
|
}
|
|
const fn compiletime() {
|
|
assert!(matches!($left, const { $right }));
|
|
}
|
|
core::intrinsics::const_eval_select((), compiletime, runtime)
|
|
}
|
|
};
|
|
}
|
|
|
|
/// Creates a test for runtime and a test for constant-time.
|
|
macro_rules! test_runtime_and_compiletime {
|
|
($(
|
|
$(#[$attr:meta])*
|
|
fn $test:ident() $block:block
|
|
)*) => {
|
|
$(
|
|
$(#[$attr])*
|
|
#[test]
|
|
fn $test() $block
|
|
$(#[$attr])*
|
|
const _: () = $block;
|
|
)*
|
|
}
|
|
}
|
|
|
|
mod alloc;
|
|
mod any;
|
|
mod array;
|
|
mod ascii;
|
|
mod ascii_char;
|
|
mod asserting;
|
|
mod async_iter;
|
|
mod atomic;
|
|
mod bool;
|
|
mod cell;
|
|
mod char;
|
|
mod clone;
|
|
mod cmp;
|
|
mod const_ptr;
|
|
mod convert;
|
|
mod ffi;
|
|
mod fmt;
|
|
mod future;
|
|
mod hash;
|
|
mod intrinsics;
|
|
mod io;
|
|
mod iter;
|
|
mod lazy;
|
|
mod macros;
|
|
mod manually_drop;
|
|
mod mem;
|
|
mod net;
|
|
mod nonzero;
|
|
mod num;
|
|
mod ops;
|
|
mod option;
|
|
mod panic;
|
|
mod pattern;
|
|
mod pin;
|
|
mod pin_macro;
|
|
mod ptr;
|
|
mod result;
|
|
mod simd;
|
|
mod slice;
|
|
mod str;
|
|
mod str_lossy;
|
|
mod task;
|
|
mod time;
|
|
mod tuple;
|
|
mod unicode;
|
|
mod waker;
|
|
|
|
/// Copied from `std::test_helpers::test_rng`, see that function for rationale.
|
|
#[track_caller]
|
|
#[allow(dead_code)] // Not used in all configurations.
|
|
pub(crate) fn test_rng() -> rand_xorshift::XorShiftRng {
|
|
use core::hash::{BuildHasher, Hash, Hasher};
|
|
let mut hasher = std::hash::RandomState::new().build_hasher();
|
|
core::panic::Location::caller().hash(&mut hasher);
|
|
let hc64 = hasher.finish();
|
|
let seed_vec = hc64.to_le_bytes().into_iter().chain(0u8..8).collect::<Vec<u8>>();
|
|
let seed: [u8; 16] = seed_vec.as_slice().try_into().unwrap();
|
|
rand::SeedableRng::from_seed(seed)
|
|
}
|