Function lexical_core::write_with_options_unchecked
source · [−]pub unsafe fn write_with_options_unchecked<'a, N: ToLexicalWithOptions, const FORMAT: u128>(
n: N,
bytes: &'a mut [u8],
options: &N::Options
) -> &'a mut [u8]ⓘNotable traits for &'_ mut [u8]impl<'_> Write for &'_ mut [u8]impl<'_> Read for &'_ [u8]
Expand description
Write number to string with custom options.
Returns a subslice of the input buffer containing the written bytes, starting from the same address in memory as the input slice.
FORMAT
- Packed struct containing the number format.value
- Number to serialize.bytes
- Buffer to write number to.options
- Options to customize number parsing.
Safety
If the buffer is not be large enough to hold the serialized number,
it will overflow the buffer unless the safe
feature is enabled.
Buffer overflows are severe security vulnerabilities, and therefore
to ensure the function will not overwrite the buffer, provide a
buffer with at least {integer}::FORMATTED_SIZE
elements. If you
are using custom digit precision control or exponent break points
for writing floats, these constants may be insufficient to store
the serialized number, and up to 1200 bytes may be required with
radix support.
Panics
If the provided FORMAT
is not valid, the function may panic. Please
ensure is_valid()
is called prior to using the format, or checking
its validity using a static assertion.
Example
#[cfg(feature = "write-floats")] {
// import `BUFFER_SIZE` to get the maximum bytes written by the number.
use lexical_core::BUFFER_SIZE;
let mut buffer = [0u8; BUFFER_SIZE];
let float = 3.14159265359_f32;
const FORMAT: u128 = lexical_core::format::STANDARD;
let options = lexical_core::WriteFloatOptions::new();
unsafe {
lexical_core::write_with_options_unchecked::<_, FORMAT>(float, &mut buffer, &options);
}
assert_eq!(&buffer[0..9], b"3.1415927");