Crate hipstr

source ·
Expand description

Yet another string type for Rust 🦀

  • no copy and const literal wrapping
  • no alloc small strings (23 bytes on 64-bit platform)
  • no copy owned slices
  • zero dependency, except for optional serde support

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` methods

Two Types

  • HipByt<B>
    a replacement for both Vec<u8> and [u8]
  • HipStr<B>
    a replacement for both String and str

where B is a backend, see below.

Three Representations

Each type has three distinct representations:

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:

The crate root also provides some convenience type aliases:

  • hipstr::HipByt and hipstr::HipStr that set B to ThreadSafe,
  • hipstr::LocalHipByt and hipstr::LocalHipStr that set B to Local.

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