9 releases

0.3.0 Jan 21, 2019
0.2.5 Jan 13, 2019
0.1.1 Dec 18, 2018

#647 in Data structures

Download history 7001/week @ 2024-07-29 9388/week @ 2024-08-05 9210/week @ 2024-08-12 10743/week @ 2024-08-19 6777/week @ 2024-08-26 9314/week @ 2024-09-02 9027/week @ 2024-09-09 8006/week @ 2024-09-16 6777/week @ 2024-09-23 8455/week @ 2024-09-30 7062/week @ 2024-10-07 9045/week @ 2024-10-14 6169/week @ 2024-10-21 6556/week @ 2024-10-28 8060/week @ 2024-11-04 7549/week @ 2024-11-11

28,762 downloads per month
Used in 28 crates (16 directly)

MIT/Apache

125KB
2K SLoC

ArrayString

Fixed capacity stack based generic string

Since rust doesn't have constant generics yet typenum is used to allow for generic arrays (U1 to U255)

Can't outgrow initial capacity (defined at compile time), always occupies capacity + 1 bytes of memory

Doesn't allocate memory on the heap and never panics in release (all panic branches are stripped at compile time - except Index/IndexMut traits, since they are supposed to)

Why

Data is generally bounded, you don't want a phone number with 30 characters, nor a username with 100. You probably don't even support it in your database.

Why pay the cost of heap allocations of strings with unlimited capacity if you have limited boundaries?

Stack based strings are generally faster to create, clone and append to than heap based strings (custom allocators and thread-locals may help with heap based ones).

But that becomes less true as you increase the array size, CacheString occuppies a full cache line, 255 bytes is the maximum we accept - MaxString (bigger will just wrap) and it's probably already slower than heap based strings of that size (like in std::string::String)

There are other stack based strings out there, they generally can have "unlimited" capacity (heap allocate), but the stack based size is defined by the library implementor, we go through a different route by implementing a string based in a generic array.

Array based strings always occupies the full space in memory, so they may use more memory (in the stack) than dynamic strings.

Features

default: std

  • std enabled by default, enables std compatibility - impl Error trait for errors (remove it to be #[no_std] compatible)

  • serde-traits enables serde traits integration (Serialize/Deserialize)

    Opperates like String, but truncates it if it's bigger than capacity

  • diesel-traits enables diesel traits integration (Insertable/Queryable)

    Opperates like String, but truncates it if it's bigger than capacity

  • logs enables internal logging

    You will probably only need this if you are debugging this library

Examples

use arraystring::{Error, ArrayString, typenum::U5, typenum::U20};

type Username = ArrayString<U20>;
type Role = ArrayString<U5>;

#[derive(Debug)]
pub struct User {
    pub username: Username,
    pub role: Role,
}

fn main() -> Result<(), Error> {
    let user = User {
        username: Username::try_from_str("user")?,
        role: Role::try_from_str("admin")?
    };
    println!("{:?}", user);

    Ok(())
}

Comparisons

These benchmarks ran while I streamed video and used my computer (with non-disclosed specs) as usual, so don't take the actual times too seriously, just focus on the comparison

small-string  (23 bytes)      clone                  4.837 ns
small-string  (23 bytes)      try_from_str          14.777 ns
small-string  (23 bytes)      from_str_truncate     11.360 ns
small-string  (23 bytes)      from_str_unchecked    11.291 ns
small-string  (23 bytes)      try_push_str           1.162 ns
small-string  (23 bytes)      push_str               3.490 ns
small-string  (23 bytes)      push_str_unchecked     1.098 ns
-------------------------------------------------------------
cache-string  (63 bytes)      clone                 10.170 ns
cache-string  (63 bytes)      try_from_str          25.579 ns
cache-string  (63 bytes)      from_str_truncate     16.977 ns
cache-string  (63 bytes)      from_str_unchecked    17.201 ns
cache-string  (63 bytes)      try_push_str           1.160 ns
cache-string  (63 bytes)      push_str               3.486 ns
cache-string  (63 bytes)      push_str_unchecked     1.115 ns
-------------------------------------------------------------
max-string   (255 bytes)      clone                147.410 ns
max-string   (255 bytes)      try_from_str         157.340 ns
max-string   (255 bytes)      from_str_truncate    158.000 ns
max-string   (255 bytes)      from_str_unchecked   158.420 ns
max-string   (255 bytes)      try_push_str           1.167 ns
max-string   (255 bytes)      push_str               4.337 ns
max-string   (255 bytes)      push_str_unchecked     1.103 ns
-------------------------------------------------------------
string (19 bytes)             clone                 33.295 ns
string (19 bytes)             from                  32.512 ns
string (19 bytes)             push str              28.128 ns
-------------------------------------------------------------
inlinable-string (30 bytes)   clone                 16.751 ns
inlinable-string (30 bytes)   from_str              29.310 ns
inlinable-string (30 bytes)   push_str               2.865 ns
-------------------------------------------------------------
smallstring crate (20 bytes)  clone                 60.988 ns
smallstring crate (20 bytes)  from_str              50.233 ns

Licenses

MIT and Apache-2.0

Dependencies

~0.1–1MB
~21K SLoC