Expand description
Yet another string type for Rust 🦀
- no copy and
constliteral wrapping - no alloc small strings (23 bytes on 64-bit platform)
- no copy owned slices
- zero dependency, except for optional
serdesupport
And byte strings too!
Examples
use hipstr::HipStr;
let simple_greetings = HipStr::borrowed("Hello world");
let clone = simple_greetings.clone(); // no copy
std::thread::spawn(move || { println!("{}", clone); });
let user = "John";
let greetings = HipStr::from(format!("Hello {}", user));
let user = greetings.slice(6..); // no copy
drop(greetings); // the slice is _owned_, it exits even if greetings disappear
let chars = user.chars().count(); // "inherits" `&str` methodsTwo Types
where B is a backend, see below.
Three Representations
Each type has three distinct representations:
- Static borrow, constructed with
HipStr::from_staticorHipByt::from_static - Inline sequence (up to
HipByt::inline_capacity()) - Shared reference (cheaply clonable) and slice (sliceable)
The shared reference can be thread-safe or not, depending on the backend.
⚠️ Warning!
The used representation of the empty string is unspecified and may change between patch versions! It may be borrowed or inlined but will never be allocated.
Two Backends
The crate provides two backends:
ThreadSafe(Arc<Vec<u8>>),Local(Rc<Vec<u8>>).
The crate root also provides some convenience type aliases:
hipstr::HipBytandhipstr::HipStrthat setBtoThreadSafe,hipstr::LocalHipBytandhipstr::LocalHipStrthat setBtoLocal.
Platform Support
This crate is only supported on platforms where:
- pointers have the same memory size has
usize - pointer alignment requirement is strictly greater than 1
For now, most common architectures are like that. However, hipstr will not
work on new and future architectures relying on large tagged pointers
(e.g. CHERI 128-bit pointers).
Modules
- Cheaply clonable, sliceable, and mostly immutable, byte string.
- Cheaply clonable, sliceable, and mostly immutable, string.
Traits
- Sealed marker trait for allocated backend.
Type Definitions
- Thread-safe shared byte sequence.
- Thread-safe shared string.
- Local reference counted backend.
- Thread-local byte sequence.
- Thread-local string.
- Shared (thread-safe) reference counted backend.