ryuu/
lib.rs

1#![doc = "# Ry\u{16b}\u{16b}\n\n[<img alt=\"github\" src=\"https://img.shields.io/badge/github-hanyu%20dev/ryuu-8da0cb?style=for-the-badge&labelColor=555555&logo=github\" height=\"20\">](https://github.com/hanyu-dev/ryuu)\n[<img alt=\"crates.io\" src=\"https://img.shields.io/crates/v/ryuu.svg?style=for-the-badge&color=fc8d62&logo=rust\" height=\"20\">](https://crates.io/crates/ryuu)\n[<img alt=\"docs.rs\" src=\"https://img.shields.io/badge/docs.rs-ryuu-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs\" height=\"20\">](https://docs.rs/ryuu)\n[<img alt=\"build status\" src=\"https://img.shields.io/github/actions/workflow/status/hanyu-dev/ryuu/ci.yml?branch=master&style=for-the-badge\" height=\"20\">](https://github.com/hanyu-dev/ryuu/actions?query=branch%3Amaster)\n\n> This is a **fork** of the [ryu](https://crates.io/crates/ryu) crate with the following changes:\n>\n> - Const ready, requiring Rust 1.83.\n> - The APIs are changed, so it is not compatible with the original `ryu` crate, though migration is trivial:\n>\n>   The original `ryu` crate provided a `Buffer` type:\n>\n>   ```rust\n>   use ryu::Buffer;\n>   let mut buffer = Buffer::new();\n>   let formatted = buffer.format(1.234f64); // the `formatted` is a `&str`, referencing the buffer\n>   assert_eq!(formatted, \"1.234\");\n>   ```\n>\n>   Now it is:\n>\n>   ```rust\n>   use ryuu::Formatter;\n>   let formatted = Formatter::format(1.234f64);\n>   assert_eq!(&*formatted, \"1.234\"); // the `formatted` is now with a dedicated type `Formatted`\n>   ```\n>\n>   The most significant change is that `Formatted` allows being \"dereferenced\" into a string with specified decimal places.\n>\n>   See the [API documentation](https://docs.rs/ryuu/latest/ryuu/) for more details.\n>\n>   According to the bench results, no significant performance regression was observed.\n\nPure Rust implementation of Ry\u{16b}, an algorithm to quickly convert floating point\nnumbers to decimal strings.\n\nThe PLDI\'18 paper [*Ry\u{16b}: fast float-to-string conversion*][paper] by Ulf Adams\nincludes a complete correctness proof of the algorithm. The paper is available\nunder the creative commons CC-BY-SA license.\n\nThis Rust implementation is a line-by-line port of Ulf Adams\' implementation in\nC, [https://github.com/ulfjack/ryu][upstream].\n\n*Requirements: this crate supports any compiler version back to rustc 1.36; it\nuses nothing from the Rust standard library so is usable from no_std crates.*\n\n[paper]: https://dl.acm.org/citation.cfm?id=3192369\n[upstream]: https://github.com/ulfjack/ryu/tree/77e767f5e056bab96e895072fc21618ecff2f44b\n\n```toml\n[dependencies]\nryuu = \"1.0\"\n```\n\n<br>\n\n## Example\n\n```rust\nfn main() {\n    let formatted = ryuu::Formatter::format_f64(1.234);\n    assert_eq!(&*formatted, \"1.234\");\n}\n```\n\n<br>\n\n## Performance (lower is better)\n\n![performance](https://raw.githubusercontent.com/hanyu-dev/ryuu/master/performance.png)\n\nYou can run upstream\'s benchmarks with:\n\n```console\n$ git clone https://github.com/ulfjack/ryu c-ryu\n$ cd c-ryu\n$ bazel run -c opt //ryu/benchmark:ryu_benchmark\n```\n\nAnd the same benchmark against our implementation with:\n\n```console\n$ git clone https://github.com/hanyu-dev/ryuu rust-ryuu\n$ cd rust-ryuu\n$ cargo run --example upstream_benchmark --release\n```\n\nThese benchmarks measure the average time to print a 32-bit float and average\ntime to print a 64-bit float, where the inputs are distributed as uniform random\nbit patterns 32 and 64 bits wide.\n\nThe upstream C code, the unsafe direct Rust port, and the safe pretty Rust API\nall perform the same, taking around 21 nanoseconds to format a 32-bit float and\n31 nanoseconds to format a 64-bit float.\n\nThere is also a Rust-specific benchmark comparing this implementation to the\nstandard library which you can run with:\n\n```console\n$ cargo bench\n```\n\nThe benchmark shows Ry\u{16b} approximately 2-5x faster than the standard library\nacross a range of f32 and f64 inputs. Measurements are in nanoseconds per\niteration; smaller is better.\n\n## Formatting\n\nThis library tends to produce more human-readable output than the standard\nlibrary\'s to\\_string, which never uses scientific notation. Here are two\nexamples:\n\n- *ryuu:* 1.23e40, *std:* 12300000000000000000000000000000000000000\n- *ryuu:* 1.23e-40, *std:* 0.000000000000000000000000000000000000000123\n\nBoth libraries print short decimals such as 0.0000123 without scientific\nnotation.\n\n<br>\n\n#### License\n\n<sup>\nLicensed under either of <a href=\"LICENSE-APACHE\">Apache License, Version\n2.0</a> or <a href=\"LICENSE-BOOST\">Boost Software License 1.0</a> at your\noption.\n</sup>\n\n<br>\n\n<sub>\nUnless you explicitly state otherwise, any contribution intentionally submitted\nfor inclusion in this crate by you, as defined in the Apache-2.0 license, shall\nbe dual licensed as above, without any additional terms or conditions.\n</sub>\n"include_str!("../README.md")]
2#![no_std]
3#![doc(html_root_url = "https://docs.rs/ryu/1.0.20")]
4#![allow(
5    clippy::cast_lossless,
6    clippy::cast_possible_truncation,
7    clippy::cast_possible_wrap,
8    clippy::cast_sign_loss,
9    clippy::checked_conversions,
10    clippy::doc_markdown,
11    clippy::expl_impl_clone_on_copy,
12    clippy::if_not_else,
13    clippy::many_single_char_names,
14    clippy::missing_panics_doc,
15    clippy::module_name_repetitions,
16    clippy::must_use_candidate,
17    clippy::needless_doctest_main,
18    clippy::similar_names,
19    clippy::too_many_lines,
20    clippy::unreadable_literal,
21    clippy::unseparated_literal_suffix,
22    clippy::wildcard_imports
23)]
24
25mod common;
26mod d2s;
27#[cfg(any(test, not(feature = "small"), feature = "feat-exp-parse"))]
28mod d2s_full_table;
29mod d2s_intrinsics;
30#[cfg(any(test, feature = "small", feature = "feat-exp-parse"))]
31mod d2s_small_table;
32mod digit_table;
33mod f2s;
34mod f2s_intrinsics;
35pub mod format;
36#[cfg(any(test, feature = "feat-exp-parse"))]
37pub mod parse;
38pub mod raw;
39
40pub use crate::format::{Formatted, Formatter};