lexical_write_integer/
options.rs

1//! Configuration options for writing integers.
2//!
3//! This is a dummy implementation, since writing integers never have options.
4
5use lexical_util::constants::FormattedSize;
6use lexical_util::options::WriteOptions;
7use lexical_util::result::Result;
8use static_assertions::const_assert;
9
10/// Builder for `Options`.
11#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
12pub struct OptionsBuilder {}
13
14impl OptionsBuilder {
15    /// Create new options builder with default options.
16    #[inline(always)]
17    pub const fn new() -> Self {
18        Self {}
19    }
20
21    // BUILDERS
22
23    /// Check if the builder state is valid.
24    #[inline(always)]
25    pub const fn is_valid(&self) -> bool {
26        true
27    }
28
29    /// Build the Options struct with bounds validation.
30    ///
31    /// # Safety
32    ///
33    /// Safe as long as `is_valid` is true.
34    #[inline(always)]
35    pub const unsafe fn build_unchecked(&self) -> Options {
36        Options {}
37    }
38
39    /// Build the Options struct.
40    #[inline(always)]
41    pub const fn build(&self) -> Result<Options> {
42        // SAFETY: always safe, since it must be valid.
43        Ok(unsafe { self.build_unchecked() })
44    }
45}
46
47impl Default for OptionsBuilder {
48    #[inline(always)]
49    fn default() -> Self {
50        Self::new()
51    }
52}
53
54/// Immutable options to customize writing integers.
55///
56/// # Examples
57///
58/// ```rust
59/// use lexical_write_integer::options::Options;
60///
61/// # pub fn main() {
62/// let options = Options::builder()
63///     .build()
64///     .unwrap();
65/// # }
66/// ```
67#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
68pub struct Options {}
69
70impl Options {
71    /// Create options with default values.
72    #[inline(always)]
73    pub const fn new() -> Self {
74        Self {}
75    }
76
77    /// Check if the options state is valid.
78    #[inline(always)]
79    pub const fn is_valid(&self) -> bool {
80        true
81    }
82
83    // BUILDERS
84
85    /// Get OptionsBuilder as a static function.
86    #[inline(always)]
87    pub const fn builder() -> OptionsBuilder {
88        OptionsBuilder::new()
89    }
90
91    /// Create OptionsBuilder using existing values.
92    #[inline(always)]
93    pub const fn rebuild(&self) -> OptionsBuilder {
94        OptionsBuilder {}
95    }
96}
97
98impl Default for Options {
99    #[inline(always)]
100    fn default() -> Self {
101        Self::new()
102    }
103}
104
105impl WriteOptions for Options {
106    #[inline(always)]
107    fn is_valid(&self) -> bool {
108        Self::is_valid(self)
109    }
110
111    #[inline(always)]
112    fn buffer_size<T: FormattedSize, const FORMAT: u128>(&self) -> usize {
113        T::FORMATTED_SIZE
114    }
115}
116
117// PRE-DEFINED CONSTANTS
118// ---------------------
119
120/// Standard number format.
121#[rustfmt::skip]
122pub const STANDARD: Options = Options::new();
123const_assert!(STANDARD.is_valid());