ryuu 2.0.0+ryu.86d20a5

Fast floating point to string conversion
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
//! Safe API for formatting floating point numbers to text.

use core::mem::MaybeUninit;
use core::{fmt, ops, ptr, slice, str};

use crate::raw::{self, FormattedMeta};

/// The length of the buffer used to store the formatted text.
pub const BUFFER_LEN: usize = 32;

#[derive(Debug, Clone, Copy)]
/// Safe API for formatting floating point numbers to text.
///
/// ## Example
///
/// ```
/// assert_eq!(
///     ryuu::Formatter::format_finite_f64(1.234_f64).as_str(),
///     "1.234"
/// );
/// assert_eq!(
///     ryuu::Formatter::format_finite_f32(1.234_f32).as_str(),
///     "1.234"
/// );
/// ```
pub struct Formatter;

#[derive(Clone, Copy)]
/// The formatted text of a floating point number.
///
/// This implements `AsRef<str>` and `ops::Deref<Target = str>`.
pub struct Formatted {
    /// The inner bytes, maybe initialized.
    bytes: [MaybeUninit<u8>; BUFFER_LEN],

    /// The type of the formatted number, which indicates whether it is an
    /// integer, has a decimal point, or is in exponent form.
    meta: FormattedMeta,

    /// The offset of all bytes that have been initialized in `bytes`.
    initialized: usize,
}

impl fmt::Debug for Formatted {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Formatted")
            .field("bytes", &self.as_str())
            .field("meta", &self.meta)
            .field("initialized", &self.initialized)
            .finish()
    }
}

impl fmt::Display for Formatted {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl AsRef<str> for Formatted {
    /// Returns a reference to the string representation of the last formatted
    /// floating point number.
    ///
    /// # Panics
    ///
    /// This method panics if no floating point number has been formatted yet.
    #[inline]
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl ops::Deref for Formatted {
    type Target = str;

    /// Returns a reference to the string representation of the last formatted
    /// floating point number.
    ///
    /// # Panics
    ///
    /// This method panics if no floating point number has been formatted yet.
    #[inline]
    fn deref(&self) -> &Self::Target {
        self.as_str()
    }
}

impl Formatted {
    #[inline]
    /// Returns the formatted text.
    pub const fn as_str(&self) -> &str {
        #[allow(unsafe_code)]
        // SAFETY: The `bytes` array is guaranteed to be valid ASCII.
        unsafe {
            str::from_utf8_unchecked(self.as_bytes())
        }
    }

    #[inline]
    /// Returns the formatted text bytes.
    pub const fn as_bytes(&self) -> &[u8] {
        #[allow(unsafe_code)]
        // SAFETY: The `bytes` array is guaranteed to be initialized.
        unsafe {
            slice::from_raw_parts(self.bytes.as_ptr().cast::<u8>(), self.initialized)
        }
    }

    #[inline]
    /// Returns the formatted text with a fixed number of decimal places (on a
    /// best effort).
    ///
    /// ## Constant Parameters
    ///
    /// * `DECIMAL_PLACES` - The number of decimal places to include in the
    ///   output
    ///
    /// ## Examples
    ///
    /// ```rust
    /// # use ryuu::Formatter;
    /// #
    /// let mut formatted = Formatter::format(3.14159_f64);
    /// assert_eq!(formatted.as_str_fixed_dp::<2>(), "3.14");
    /// # let mut formatted = Formatter::format(-3.14159_f64);
    /// # assert_eq!(formatted.as_str_fixed_dp::<2>(), "-3.14");
    ///
    /// let mut formatted = Formatter::format(3.1_f64);
    /// assert_eq!(formatted.as_str_fixed_dp::<3>(), "3.1"); // Will not padded to "3.100"
    /// # let mut formatted = Formatter::format(-3.1_f64);
    /// # assert_eq!(formatted.as_str_fixed_dp::<3>(), "-3.1");
    ///
    /// let mut formatted = Formatter::format(1.0123e16_f64);
    /// assert_eq!(formatted.as_str_fixed_dp::<2>(), "1.0123e16");
    ///
    /// let mut formatted = Formatter::format(f64::INFINITY);
    /// assert_eq!(formatted.as_str_fixed_dp::<2>(), "inf");
    /// # let mut formatted = Formatter::format(f64::NEG_INFINITY);
    /// # assert_eq!(formatted.as_str_fixed_dp::<2>(), "-inf");
    /// # let mut formatted = Formatter::format(f64::NAN);
    /// # assert_eq!(formatted.as_str_fixed_dp::<2>(), "NaN");
    /// ```
    pub const fn as_str_fixed_dp<const DECIMAL_PLACES: usize>(&self) -> &str {
        match self.meta {
            FormattedMeta::Decimal {
                offset_decimal_point,
            } => {
                let target_length = offset_decimal_point + DECIMAL_PLACES + 1;

                if offset_decimal_point + DECIMAL_PLACES < self.initialized {
                    unsafe {
                        let bytes =
                            slice::from_raw_parts(self.bytes.as_ptr().cast::<u8>(), target_length);
                        str::from_utf8_unchecked(bytes)
                    }
                } else {
                    self.as_str()
                }
            }
            _ => self.as_str(),
        }
    }

    /// Returns the formatted text with a fixed number of decimal places.
    ///
    /// Unlike [`as_str_fixed_dp`](Self::as_str_fixed_dp), this method adjusts
    /// the decimal point if necessary.
    ///
    /// ## Constant Parameters
    ///
    /// * `DECIMAL_PLACES` - The number of decimal places to include in the
    ///   output
    ///
    /// ## Examples
    ///
    /// ```rust
    /// # use ryuu::Formatter;
    /// #
    /// let mut formatted = Formatter::format(3.14159_f64);
    /// assert_eq!(formatted.as_str_adjusting_dp::<2>(), "3.14");
    /// # let mut formatted = Formatter::format(-3.14159_f64);
    /// # assert_eq!(formatted.as_str_adjusting_dp::<2>(), "-3.14");
    ///
    /// let mut formatted = Formatter::format(3.1_f64);
    /// assert_eq!(formatted.as_str_adjusting_dp::<3>(), "3.100"); // Will padded to "3.100"
    /// assert_eq!(formatted.as_str_fixed_dp::<3>(), "3.100"); // Ohh, we have adjusted the decimal point
    /// # let mut formatted = Formatter::format(-3.1_f64);
    /// # assert_eq!(formatted.as_str_adjusting_dp::<3>(), "-3.100");
    /// # assert_eq!(formatted.as_str_fixed_dp::<3>(), "-3.100");
    ///
    /// let mut formatted = Formatter::format(1.0123e16_f64);
    /// assert_eq!(formatted.as_str_adjusting_dp::<2>(), "1.0123e16");
    ///
    /// let mut formatted = Formatter::format(f64::INFINITY);
    /// assert_eq!(formatted.as_str_adjusting_dp::<2>(), "inf");
    /// # let mut formatted = Formatter::format(f64::NEG_INFINITY);
    /// # assert_eq!(formatted.as_str_adjusting_dp::<2>(), "-inf");
    /// # let mut formatted = Formatter::format(f64::NAN);
    /// # assert_eq!(formatted.as_str_adjusting_dp::<2>(), "NaN");
    /// ```
    pub const fn as_str_adjusting_dp<const DECIMAL_PLACES: usize>(&mut self) -> &str {
        match self.meta {
            FormattedMeta::Decimal {
                offset_decimal_point,
            } => {
                let target_length = offset_decimal_point + DECIMAL_PLACES + 1;

                let to_be_zeroed = target_length.checked_sub(self.initialized);

                match to_be_zeroed {
                    None | Some(0) => unsafe {
                        str::from_utf8_unchecked(slice::from_raw_parts(
                            self.bytes.as_ptr().cast::<u8>(),
                            target_length,
                        ))
                    },
                    Some(to_be_zeroed) => {
                        // Initialize the bytes to '0'.
                        unsafe {
                            self.bytes
                                .as_mut_ptr()
                                .add(self.initialized)
                                .write_bytes(b'0', to_be_zeroed);
                        };

                        // Update the initialized length
                        self.initialized = target_length;

                        unsafe {
                            str::from_utf8_unchecked(slice::from_raw_parts(
                                self.bytes.as_ptr().cast::<u8>(),
                                target_length,
                            ))
                        }
                    }
                }
            }
            _ => self.as_str(),
        }
    }

    /// Copies the formatted text to the given buffer, with a fixed number of
    /// decimal places.
    ///
    /// This method provides precise control over the number of decimal places
    /// in the output, handling different formatting scenarios (decimal,
    /// exponent, and non-finite numbers) appropriately.
    ///
    /// ## Behavior by Format Type
    ///
    /// ### Decimal Format (e.g., "3.14159")
    /// - If `DECIMAL_PLACES` is 0, copies only the integer part (no decimal
    ///   point)
    /// - If `DECIMAL_PLACES` > 0, ensures exactly that many decimal places:
    ///   - Truncates if the original has more decimal places
    ///   - Pads with zeros if the original has fewer decimal places
    ///
    /// ### Exponent Format (e.g., "3.14e20")
    /// - Adjusts the decimal part while preserving the exponent
    /// - If `DECIMAL_PLACES` is 0, removes the decimal point entirely
    /// - Pads with zeros or truncates the decimal part as needed
    ///
    /// ### Non-finite Numbers (NaN, inf, -inf)
    /// - Ignores `DECIMAL_PLACES` and copies the string as-is
    ///
    /// ## Return Value
    ///
    /// Returns `Some(bytes_written)` on success, where `bytes_written` is the
    /// number of bytes written to the buffer. Returns `None` if the buffer is
    /// too small to hold the result.
    ///
    /// ## Buffer Requirements
    ///
    /// The buffer must be large enough to hold the result, [`BUFFER_LEN`] +
    /// `DECIMAL_PLACES` bytes is recommended.
    ///
    /// ## Constant Parameters
    ///
    /// * `DECIMAL_PLACES` - The exact number of decimal places to include in
    ///   the output.
    ///
    /// ## Examples
    ///
    /// ```rust
    /// # use ryuu::Formatter;
    /// let mut buf = [0u8; 32 + 2];
    ///
    /// // Normal case
    /// let formatted = Formatter::format_finite_f64(3.14159);
    /// # let written = formatted.copy_to_bytes::<0>(&mut buf).unwrap();
    /// # assert_eq!(&buf[..written], b"3");
    /// let written = formatted.copy_to_bytes::<4>(&mut buf).unwrap();
    /// assert_eq!(&buf[..written], b"3.1415");
    /// # let written = formatted.copy_to_bytes::<5>(&mut buf).unwrap();
    /// # assert_eq!(&buf[..written], b"3.14159");
    /// let written = formatted.copy_to_bytes::<6>(&mut buf).unwrap();
    /// assert_eq!(&buf[..written], b"3.141590");
    ///
    /// let formatted = Formatter::format_finite_f64(-3.14159);
    /// # let written = formatted.copy_to_bytes::<0>(&mut buf).unwrap();
    /// # assert_eq!(&buf[..written], b"-3");
    /// let written = formatted.copy_to_bytes::<4>(&mut buf).unwrap();
    /// assert_eq!(&buf[..written], b"-3.1415");
    /// # let written = formatted.copy_to_bytes::<5>(&mut buf).unwrap();
    /// # assert_eq!(&buf[..written], b"-3.14159");
    /// let written = formatted.copy_to_bytes::<6>(&mut buf).unwrap();
    /// assert_eq!(&buf[..written], b"-3.141590");
    ///
    /// // For a large / small number that will be formatted in scientific notation form
    /// let formatted = Formatter::format_finite_f64(3e20);
    /// # let written = formatted.copy_to_bytes::<0>(&mut buf).unwrap();
    /// # assert_eq!(&buf[..written], b"3e20");
    /// let written = formatted.copy_to_bytes::<1>(&mut buf).unwrap();
    /// assert_eq!(&buf[..written], b"3.0e20");
    /// # let written = formatted.copy_to_bytes::<2>(&mut buf).unwrap();
    /// # assert_eq!(&buf[..written], b"3.00e20");
    ///
    /// let formatted = Formatter::format_finite_f64(3.14e20);
    /// # let written = formatted.copy_to_bytes::<0>(&mut buf).unwrap();
    /// # assert_eq!(&buf[..written], b"3e20");
    /// let written = formatted.copy_to_bytes::<1>(&mut buf).unwrap();
    /// assert_eq!(&buf[..written], b"3.1e20");
    /// # let written = formatted.copy_to_bytes::<2>(&mut buf).unwrap();
    /// # assert_eq!(&buf[..written], b"3.14e20");
    /// let written = formatted.copy_to_bytes::<3>(&mut buf).unwrap();
    /// assert_eq!(&buf[..written], b"3.140e20");
    ///
    /// // For nonfinite numbers, the output is the same as `as_str()`
    /// let formatted = Formatter::format_f64(f64::NAN);
    /// let written = formatted.copy_to_bytes::<2>(&mut buf).unwrap();
    /// assert_eq!(&buf[..written], b"NaN");
    /// # let formatted = Formatter::format_f64(f64::INFINITY);
    /// # let written = formatted.copy_to_bytes::<2>(&mut buf).unwrap();
    /// # assert_eq!(&buf[..written], b"inf");
    /// # let formatted = Formatter::format_f64(f64::NEG_INFINITY);
    /// # let written = formatted.copy_to_bytes::<2>(&mut buf).unwrap();
    /// # assert_eq!(&buf[..written], b"-inf");
    /// ```
    pub const fn copy_to_bytes<const DECIMAL_PLACES: usize>(
        &self,
        buf: &mut [u8],
    ) -> Option<usize> {
        match self.meta {
            FormattedMeta::Decimal {
                offset_decimal_point,
            } => {
                if DECIMAL_PLACES == 0 {
                    let Some((buf, _)) = buf.split_at_mut_checked(offset_decimal_point) else {
                        return None;
                    };

                    unsafe {
                        ptr::copy_nonoverlapping(
                            self.bytes.as_ptr().cast::<u8>(),
                            buf.as_mut_ptr(),
                            offset_decimal_point,
                        );
                    };

                    Some(offset_decimal_point)
                } else {
                    let target_length = offset_decimal_point + DECIMAL_PLACES + 1; // for the decimal point

                    let Some((buf, _)) = buf.split_at_mut_checked(target_length) else {
                        return None;
                    };

                    unsafe {
                        if target_length <= self.initialized {
                            ptr::copy_nonoverlapping(
                                self.bytes.as_ptr().cast::<u8>(),
                                buf.as_mut_ptr(),
                                target_length,
                            );
                        } else {
                            // SAFETY: target_length > self.initialized
                            let (bytes, zeros) = buf.split_at_mut_unchecked(self.initialized);

                            ptr::copy_nonoverlapping(
                                self.bytes.as_ptr().cast::<u8>(),
                                bytes.as_mut_ptr(),
                                self.initialized,
                            );

                            zeros.as_mut_ptr().write_bytes(b'0', zeros.len());
                        }
                    }

                    Some(target_length)
                }
            }
            FormattedMeta::Exponent {
                offset_decimal_point,
                offset_exponent,
            } => {
                let target_decimal_part = if DECIMAL_PLACES == 0 {
                    0
                } else {
                    DECIMAL_PLACES + 1
                };

                let (actual_integer_part, actual_decimal_part) = match offset_decimal_point {
                    Some(offset_decimal_point) => (
                        offset_decimal_point,
                        (offset_exponent - offset_decimal_point),
                    ),
                    None => (offset_exponent, 0),
                };

                let target_length = self.initialized - actual_decimal_part + target_decimal_part;

                let Some((buf, _)) = buf.split_at_mut_checked(target_length) else {
                    return None;
                };

                let (bytes_integer_part, bytes_decimal_part, bytes_exponent_part) = {
                    let bytes = self.as_bytes();
                    let (bytes_integer_part, bytes) =
                        unsafe { bytes.split_at_unchecked(actual_integer_part) };
                    let (bytes_decimal_part, bytes_exponent_part) =
                        unsafe { bytes.split_at_unchecked(actual_decimal_part) };

                    (bytes_integer_part, bytes_decimal_part, bytes_exponent_part)
                };

                // The integer part
                let (buf_integer_part, buf) =
                    unsafe { buf.split_at_mut_unchecked(bytes_integer_part.len()) };
                unsafe {
                    ptr::copy_nonoverlapping(
                        bytes_integer_part.as_ptr().cast::<u8>(),
                        buf_integer_part.as_mut_ptr(),
                        bytes_integer_part.len(),
                    );
                };

                // The decimal part
                let (buf_decimal_part, buf_exponent_part) =
                    unsafe { buf.split_at_mut_unchecked(target_decimal_part) };
                if target_decimal_part > 0 {
                    match target_decimal_part.checked_sub(actual_decimal_part) {
                        Some(remaining) => {
                            unsafe {
                                if actual_decimal_part == 0 {
                                    // If there is no decimal part, we need to write the decimal
                                    // point
                                    buf_decimal_part.as_mut_ptr().write(b'.');
                                    // Write zeros after the decimal point. Since we already wrote
                                    // the decimal
                                    // point, we need to subtract 1 from
                                    // remaining to account for the decimal point.
                                    if remaining > 0 {
                                        buf_decimal_part
                                            .as_mut_ptr()
                                            .offset(1)
                                            .write_bytes(b'0', remaining - 1);
                                    }
                                } else {
                                    ptr::copy_nonoverlapping(
                                        bytes_decimal_part.as_ptr().cast::<u8>(),
                                        buf_decimal_part.as_mut_ptr(),
                                        actual_decimal_part,
                                    );

                                    buf_decimal_part
                                        .as_mut_ptr()
                                        .add(actual_decimal_part)
                                        .write_bytes(b'0', remaining);
                                }
                            };
                        }
                        None => {
                            unsafe {
                                ptr::copy_nonoverlapping(
                                    bytes_decimal_part.as_ptr().cast::<u8>(),
                                    buf_decimal_part.as_mut_ptr(),
                                    target_decimal_part,
                                );
                            };
                        }
                    }
                }

                // The exponent part.
                unsafe {
                    ptr::copy_nonoverlapping(
                        bytes_exponent_part.as_ptr(),
                        buf_exponent_part.as_mut_ptr(),
                        bytes_exponent_part.len(),
                    );
                };

                Some(target_length)
            }
            FormattedMeta::Nonfinite => {
                let Some((buf, _)) = buf.split_at_mut_checked(self.initialized) else {
                    return None;
                };

                let bytes = self.as_bytes();

                unsafe { ptr::copy_nonoverlapping(bytes.as_ptr(), buf.as_mut_ptr(), bytes.len()) };

                Some(self.initialized)
            }
        }
    }
}

impl Formatter {
    /// Print a floating point number into this buffer and return a reference to
    /// its string representation within the buffer.
    ///
    /// # Special cases
    ///
    /// This function formats NaN as the string "NaN", positive infinity as
    /// "inf", and negative infinity as "-inf" to match std::fmt.
    ///
    /// If your input is known to be finite, you may get better performance by
    /// calling the `format_finite` method instead of `format` to avoid the
    /// checks for special cases.
    ///
    /// Since const traits support is not yet stable, this function is not
    /// `const`: <https://rust-lang.github.io/rust-project-goals/2024h2/const-traits.html>.
    #[inline]
    pub fn format<F: Float>(f: F) -> Formatted {
        f.format()
    }

    #[inline]
    /// Const version of [`format`](Self::format), specifically for `f64`.
    pub const fn format_f64(d: f64) -> Formatted {
        if is_nonfinite_f64(d) {
            let nonfinite_formatted = format_nonfinite_f64(d);

            let mut bytes = [MaybeUninit::uninit(); BUFFER_LEN];

            unsafe {
                ptr::copy_nonoverlapping(
                    nonfinite_formatted.as_ptr(),
                    bytes.as_mut_ptr().cast::<u8>(),
                    nonfinite_formatted.len(),
                );
            };

            Formatted {
                bytes,
                meta: FormattedMeta::Nonfinite,
                initialized: nonfinite_formatted.len(),
            }
        } else {
            Self::format_finite_f64(d)
        }
    }

    #[inline]
    /// Const version of [`format`](Self::format), specifically for `f32`.
    pub const fn format_f32(f: f32) -> Formatted {
        if is_nonfinite_f32(f) {
            let nonfinite_formatted = format_nonfinite_f32(f);

            let mut bytes = [MaybeUninit::uninit(); BUFFER_LEN];

            unsafe {
                ptr::copy_nonoverlapping(
                    nonfinite_formatted.as_ptr(),
                    bytes.as_mut_ptr().cast::<u8>(),
                    nonfinite_formatted.len(),
                );
            };

            Formatted {
                bytes,
                meta: FormattedMeta::Nonfinite,
                initialized: nonfinite_formatted.len(),
            }
        } else {
            Self::format_finite_f32(f)
        }
    }

    /// Print a floating point number into this buffer and return a reference to
    /// its string representation within the buffer.
    ///
    /// # Special cases
    ///
    /// This function **does not** check for NaN or infinity. If the input
    /// number is not a finite float, the printed representation will be some
    /// correctly formatted but unspecified numerical value.
    ///
    /// Please check [`is_finite`] yourself before calling this function, or
    /// check [`is_nan`] and [`is_infinite`] and handle those cases yourself.
    ///
    /// [`is_finite`]: f64::is_finite
    /// [`is_nan`]: f64::is_nan
    /// [`is_infinite`]: f64::is_infinite
    #[inline]
    pub const fn format_finite_f64(d: f64) -> Formatted {
        let mut bytes = [MaybeUninit::uninit(); BUFFER_LEN];

        // Do format
        let offset_full = unsafe { raw::format64_spec(d, bytes.as_mut_ptr().cast::<u8>()) };

        debug_assert!(offset_full.initialized <= BUFFER_LEN);

        Formatted {
            bytes,
            meta: offset_full.meta,
            initialized: offset_full.initialized,
        }
    }

    #[inline]
    /// `f32` version of [`format_finite_f64`](Self::format_finite_f64).
    pub const fn format_finite_f32(f: f32) -> Formatted {
        let mut bytes = [MaybeUninit::uninit(); BUFFER_LEN];

        // Do format
        let offset_full = unsafe { raw::format32_spec(f, bytes.as_mut_ptr().cast::<u8>()) };

        debug_assert!(offset_full.initialized <= BUFFER_LEN);

        Formatted {
            bytes,
            meta: offset_full.meta,
            initialized: offset_full.initialized,
        }
    }
}

#[allow(private_bounds)]
/// A floating point number, f32 or f64, that can be formatted to text.
///
/// This trait is sealed and cannot be implemented for types outside of the
/// `ryu` crate.
pub trait Float: Sealed {}

impl Float for f32 {}
impl Float for f64 {}

trait Sealed: Copy {
    fn format(self) -> Formatted;
}

impl Sealed for f32 {
    #[inline]
    fn format(self) -> Formatted {
        Formatter::format_f32(self)
    }
}

impl Sealed for f64 {
    #[inline]
    fn format(self) -> Formatted {
        Formatter::format_f64(self)
    }
}

// === nonfinite float helpers ===

const NAN: &str = "NaN";
const INFINITY: &str = "inf";
const NEG_INFINITY: &str = "-inf";

#[inline]
const fn is_nonfinite_f32(f: f32) -> bool {
    const EXP_MASK: u32 = 0x7f800000;
    let bits = f.to_bits();
    bits & EXP_MASK == EXP_MASK
}

#[inline]
const fn is_nonfinite_f64(d: f64) -> bool {
    const EXP_MASK: u64 = 0x7ff0000000000000;
    let bits = d.to_bits();
    bits & EXP_MASK == EXP_MASK
}

#[cold]
#[inline]
const fn format_nonfinite_f64(d: f64) -> &'static str {
    const MANTISSA_MASK: u64 = 0x000fffffffffffff;
    const SIGN_MASK: u64 = 0x8000000000000000;
    let bits = d.to_bits();
    if bits & MANTISSA_MASK != 0 {
        NAN
    } else if bits & SIGN_MASK != 0 {
        NEG_INFINITY
    } else {
        INFINITY
    }
}

#[cold]
#[inline]
const fn format_nonfinite_f32(f: f32) -> &'static str {
    const MANTISSA_MASK: u32 = 0x007fffff;
    const SIGN_MASK: u32 = 0x80000000;
    let bits = f.to_bits();
    if bits & MANTISSA_MASK != 0 {
        NAN
    } else if bits & SIGN_MASK != 0 {
        NEG_INFINITY
    } else {
        INFINITY
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::approx_constant)]

    mod test_copy_to_bytes {
        include!("../unittests/buffer_copy_to_bytes.rs");
    }
}