This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
504cd2c
commit 77a2934
Showing
3 changed files
with
31 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/// Converts numeric type to a `String` | ||
#[inline] | ||
pub fn lexical_to_bytes<N: lexical_core::ToLexical>(n: N) -> Vec<u8> { | ||
let mut buf = Vec::<u8>::with_capacity(N::FORMATTED_SIZE_DECIMAL); | ||
unsafe { | ||
// JUSTIFICATION | ||
// Benefit | ||
// Allows using the faster serializer lexical core and convert to string | ||
// Soundness | ||
// Length of buf is set as written length afterwards. lexical_core | ||
// creates a valid string, so doesn't need to be checked. | ||
let slice = std::slice::from_raw_parts_mut(buf.as_mut_ptr(), buf.capacity()); | ||
let len = lexical_core::write(n, slice).len(); | ||
buf.set_len(len); | ||
} | ||
buf | ||
} | ||
|
||
/// Converts numeric type to a `String` | ||
#[inline] | ||
pub fn lexical_to_string<N: lexical_core::ToLexical>(n: N) -> String { | ||
unsafe { String::from_utf8_unchecked(lexical_to_bytes(n)) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters