rust/library/core/src
bors 75716b4510 Auto merge of #118159 - EliasHolzmann:formatting_options, r=m-ou-se
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.
2024-12-06 22:28:42 +00:00
..
alloc stabilize const_{size,align}_of_val 2024-12-02 20:18:25 +01:00
array Add '<[T]>::as_array', '<[T]>::as_mut_array', '<*const [T]>::as_array', and '<*mut [T]>::as_mut_array' conversion methods; 2024-11-26 21:49:28 +01:00
ascii Add more precondition check tests 2024-10-09 19:34:27 -04:00
async_iter Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
cell Re-do recursive const stability checks 2024-10-25 20:31:40 +02:00
char update cfgs 2024-11-27 15:14:54 +00:00
clone CloneToUninit: use a private specialization trait 2024-07-29 20:44:43 +03:00
cmp Use generic NonZero everywhere in core. 2024-02-22 15:17:33 +01:00
convert Fix doc nits 2024-07-26 13:26:33 +01:00
ffi Teach rust core about Xtensa VaListImpl and add a custom lowering of vaarg for xtensa. 2024-12-03 10:54:08 +00:00
fmt Access members of FormattingOptions directly instead of via getters/setters 2024-12-05 21:48:36 +01:00
future update cfgs 2024-11-27 15:14:54 +00:00
hash stabilize const_collections_with_hasher and build_hasher_default_const_new 2024-12-02 16:34:39 +01:00
intrinsics clarify simd_relaxed_fma non-determinism 2024-12-04 08:39:19 +01:00
io Add BorrowedBuf::into_filled{,_mut} methods to allow returning buffer with original lifetime 2024-11-02 14:26:21 -04:00
iter Also use zero when referencing to capacity or length 2024-11-28 09:47:11 +01:00
macros update cfgs 2024-11-27 15:14:54 +00:00
mem Rollup merge of #133762 - RalfJung:const-size-of-val, r=workingjubilee 2024-12-03 17:27:09 +01:00
net replace placeholder version 2024-11-27 12:10:21 +00:00
num Rollup merge of #133651 - scottmcm:nonnull-nonzero-no-field-projection, r=oli-obk 2024-12-04 05:42:07 +01:00
ops Add diagnostic item for std::ops::ControlFlow 2024-11-30 19:53:36 +01:00
panic replace placeholder version 2024-11-27 12:10:21 +00:00
prelude Avoid comments that describe multiple use items. 2024-07-17 08:02:46 +10:00
ptr Rollup merge of #133651 - scottmcm:nonnull-nonzero-no-field-projection, r=oli-obk 2024-12-04 05:42:07 +01:00
range Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
slice Rollup merge of #133743 - bjoernager:slice-as-array, r=joboet 2024-12-02 17:36:08 +01:00
str update cfgs 2024-11-27 15:14:54 +00:00
sync replace placeholder version 2024-11-27 12:10:21 +00:00
task Stabilize noop_waker 2024-12-05 14:14:17 -08:00
unicode Reformat Python code with ruff 2024-12-04 23:03:44 +01:00
any.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
arch.rs Add core::arch::breakpoint and test 2024-12-02 23:56:24 -08:00
ascii.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
asserting.rs Reformat use declarations. 2024-07-29 08:26:52 +10:00
bool.rs [Clippy] Swap filter_map_bool_then to use diagnostic item instead of path 2024-09-19 13:13:42 +01:00
borrow.rs Suggest borrowing on fn argument that is impl AsRef 2024-05-09 23:25:31 +00:00
cell.rs get rid of a bunch of unnecessary rustc_const_unstable 2024-11-30 11:55:58 +01:00
clone.rs Make CloneToUninit dyn-compatible 2024-11-12 15:08:41 -06:00
cmp.rs library: consistently use American spelling for 'behavior' 2024-10-25 12:02:47 +02:00
default.rs update cfgs 2024-09-05 17:24:01 +01:00
error.md Mention core's PanicInfo in error.md. 2024-06-11 15:47:00 +02:00
error.rs Library: Rename "object safe" to "dyn compatible" 2024-10-09 18:48:29 +02:00
escape.rs Optimize escape_ascii 2024-10-09 17:17:50 -04:00
hint.rs update cfgs 2024-11-27 15:14:54 +00:00
internal_macros.rs Fix doc nits 2024-07-26 13:26:33 +01:00
lib.miri.rs add 'x.py miri', and make it work for 'library/{core,alloc,std}' 2024-04-03 20:27:20 +02:00
lib.rs Rename core_pattern_type and core_pattern_types lib feature gates to pattern_type_macro 2024-12-04 16:16:24 +00:00
marker.rs Constify Drop and Destruct 2024-11-25 17:27:41 +00:00
option.rs update cfgs 2024-11-27 15:14:54 +00:00
panic.rs update cfgs 2024-11-27 15:14:54 +00:00
panicking.rs update cfgs 2024-11-27 15:14:54 +00:00
pat.rs Rename core_pattern_type and core_pattern_types lib feature gates to pattern_type_macro 2024-12-04 16:16:24 +00:00
pin.rs Auto merge of #133533 - BoxyUwU:bump-boostrap, r=jieyouxu,Mark-Simulacrum 2024-11-29 22:39:10 +00:00
primitive.rs Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
primitive_docs.rs Rollup merge of #132136 - RalfJung:target-feature-abi-compat, r=Mark-Simulacrum 2024-11-10 10:09:52 +01:00
random.rs random: add tracking issue, address other comments 2024-09-23 10:36:16 +02:00
range.rs Reformat using the new identifier sorting from rustfmt 2024-09-22 19:11:29 -04:00
result.rs update cfgs 2024-11-27 15:14:54 +00:00
time.rs get rid of a whole bunch of unnecessary rustc_const_unstable attributes 2024-11-02 09:59:55 +01:00
tuple.rs update cfgs 2024-09-05 17:24:01 +01:00
ub_checks.rs update cfgs 2024-11-27 15:14:54 +00:00
unit.rs Import the 2021 prelude in the core crate 2024-03-25 13:12:06 -07:00