2 unstable releases
0.2.0 | Oct 11, 2024 |
---|---|
0.1.0 | Apr 5, 2024 |
#519 in Rust patterns
219 downloads per month
Used in lightweight-mmap
43KB
931 lines
nanokit
About
A collection of tiny, reusable utility methods that reduce code size and improve performance. Readme for developers/contributors is available at README-DEV.MD.
String Construction
Fast way to concatenate strings:
use nanokit::string_concat::concat_2;
let base_msg = "Hello, ";
let text = "world!";
let result = concat_2(base_msg, text);
assert_eq!(result, "Hello, world!");
And with any type that implements AsRef<str>
:
use nanokit::string_concat::concat_2;
let base_string = String::from("The quick brown fox ");
let text_string = String::from("jumps over the lazy dog.");
let result = concat_2(base_string, text_string);
assert_eq!(result, "The quick brown fox jumps over the lazy dog.");
This is similar to libraries like concat_strs and string_concat, except that instead of pushing strings onto a preallocated String
instance, we instead unsafely create a new String
instance and adjust the length.
This saves around 150 bytes
of code in codebases that otherwise don't use string concatenation (push_str
or add).
Also saves some instructions.
Additional methods concat_3
, concat_4
, concat_5
exist.
Unsafe Concat
You can save on another 2 instructions per concatenation if you know the final string length
is lesser than isize::MAX
.
use nanokit::string_concat::concat_2;
let base_msg = "Hello, ";
let text = "world!";
let result = concat_2_no_overflow(base_msg, text);
assert_eq!(result, "Hello, world!");
Features
no-inline-concat
: Disables inlining of string concat functions (saves code size).
Numeric Utilities
Count Needed Bits
Provides a method to determine the number of bits needed to store a given number.
let number: u64 = 5;
println!("Bits needed for {}: {}", number, number.bits_needed_to_store());
The produced code does not panic, nor does it branch.
Generated x86_64 assembly for u64
:
bits_needed_to_store:
lzcnt rcx, rdi
mov eax, 64
sub eax, ecx
ret
Note: The number of bits needed to store the value 0
is returned as 0
.
This may not match your expectations. If you expect the result to be 1; try
the count-digits crate by nordzilla. Do note that said crate uses ilog2
, which
may panic.
Related Crates
- itoa: Integer to text.
Contributing
See CONTRIBUTING for guidance on how to contribute to this project.
License
nanokit
is part of the Reloaded
suite of libraries.
Licensed under MIT.
If you find this library useful, please contribute back!!