snailx-0.8.0 has been yanked.
snailx
General information
- MSRV: 1.48.0
- Only benchmarked on latest nightly.
- Licenses: GPL-3.0, MIT
Overview
snailx provides a simple, zero-allocation, zero-dependency API for iterating over program arguments on Unix and MacOS.
Benefits
- No allocations necessary
- Theoretically faster than
std::env::args(), but optimization is in its early stages.
- Theoretically faster than
- Small code and binary size
Downsides
- No Windows support - While Windows support is planned for the future, it is not currently a priority, and use would require allocation.
- Unstable API - The API and entire crate is still undergoing heavy changes, and speed, APIs, and other aspects may change.
Example usage
use Args;
Features
- Zero-allocation argument access
- Iterators over multiple string types
- CStr
- OsStr
- str
no_stdsupport technically- Better performance (WIP)
- You can, under most circumstances, expect
snailxiterators to be at least twice as fast asstd::env::args(), but you should benchmark yourself. In certain cases,snailxis up to 6x faster than stdlib, but much slower in others.
- You can, under most circumstances, expect
API
Functions
Args::new() -> Args- The basic iterator over the program arguments assnailx::CStr<'static>MappedArgs::osstr() -> MappedArgs<&'static OsStr, fn(*const u8) -> Option<&'static std::ffi::OsStr>- Iterator over the program arguments as&'static std::ffi::OsStrMappedArgs::utf8() -> MappedArgs<&'static str, fn(*const u8) -> Option<&'static str>- Iterator over the program arguments as&'static strMappedArgs::new<T, F: Fn(*const u8) -> Option<T>>(map: F)- Iterator over the program arguments asTdirect::argc_argv() -> (u32, *const *const u8)- Raw access to(argc, argv)
Feature flags
std- Enables thestdfeature, which enables all functions relating toOsStrand is one way to enablesnailx::CStr::to_stdlibno_cold- Removes the#[cold]attribute from several functionsto_core_cstr(MSRV 1.64.0) - Enablessnailx::CStr::to_stdlibassume_valid_str- This massively speeds up the iterator returned byMappedArgs::utf8()by disabling validity checks, but can cause UB if the program arguments are invalid UTF-8. Use disrecommended unless you can guarantee the returned&'static strs will be used safely or invalid UTF-8 will never be used.
Types
Args- Iterator over program arguments assnailx::CStr<'static>MappedArgs<T, F>- Generic iterator that applies a mapping function to each argumentCStr<'static>- Minimal C-style string type for zero-allocation argument access. This exists because this crate isno_std, butcore_cstrwas stabilized after its MSRV.
Platform support
- Unix-like systems: Fully supported
- Linux with GNU: Fully supported and tested
- Other variants: Fully supported in theory but untested
- macOS: Fully supported but untested
- Windows: Not yet supported (planned for future releases)
Platform-specific notes
- GNU vs non-GNU: The distinction refers to whether the system uses GNU libc (glibc) or alternative C libraries (musl,
uClibc, etc.).
snailxworks on both in theory but is tested with glibc.
Safety
snailx uses unsafe code to access OS-provided argument storage. The safety guarantees are:
- Arguments are read-only and never modified
- All pointer arithmetic is bounds-checked
- UTF-8 validation is performed unless
assume_valid_stris enabled - The
assume_valid_strfeature trades safety for performance and should only be used when you can guarantee valid UTF-8 input
Examples
Basic argument iteration
use Args;
String arguments with error handling
use MappedArgs;
Custom argument mapping
use MappedArgs;
Indexed parsing
use ;
Contributing
See CONTRIBUTING.md
License
snailx is dual licensed under GPLv3 and MIT.