Debian Codegraph @

third_party/rust/arraystring/README.md

raw
1 # ArrayString
2
3 Fixed capacity stack based generic string
4
5 Since rust doesn't have constant generics yet `typenum` is used to allow for generic arrays (U1 to U255)
6
7 Can't outgrow initial capacity (defined at compile time), always occupies `capacity` `+ 1` bytes of memory
8
9 *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)*
10
11 * [Documentation](https://docs.rs/arraystring/0.3.0/arraystring)
12
13 ## Why
14
15 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.
16
17 Why pay the cost of heap allocations of strings with unlimited capacity if you have limited boundaries?
18
19 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).
20
21 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`)
22
23 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.
24
25 Array based strings always occupies the full space in memory, so they may use more memory (in the stack) than dynamic strings.
26
27 ## Features
28
29 **default:** `std`
30
31 - `std` enabled by default, enables `std` compatibility - `impl Error` trait for errors (remove it to be `#[no_std]` compatible)
32 - `serde-traits` enables serde traits integration (`Serialize`/`Deserialize`)
33
34 Opperates like `String`, but truncates it if it's bigger than capacity
35
36 - `diesel-traits` enables diesel traits integration (`Insertable`/`Queryable`)
37
38 Opperates like `String`, but truncates it if it's bigger than capacity
39
40 - `logs` enables internal logging
41
42 You will probably only need this if you are debugging this library
43
44 ## Examples
45
46 ```rust
47 use arraystring::{Error, ArrayString, typenum::U5, typenum::U20};
48
49 type Username = ArrayString<U20>;
50 type Role = ArrayString<U5>;
51
52 #[derive(Debug)]
53 pub struct User {
54 pub username: Username,
55 pub role: Role,
56 }
57
58 fn main() -> Result<(), Error> {
59 let user = User {
60 username: Username::try_from_str("user")?,
61 role: Role::try_from_str("admin")?
62 };
63 println!("{:?}", user);
64
65 Ok(())
66 }
67 ```
68
69 ## Comparisons
70
71 *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*
72
73 ```my_custom_benchmark
74 small-string (23 bytes) clone 4.837 ns
75 small-string (23 bytes) try_from_str 14.777 ns
76 small-string (23 bytes) from_str_truncate 11.360 ns
77 small-string (23 bytes) from_str_unchecked 11.291 ns
78 small-string (23 bytes) try_push_str 1.162 ns
79 small-string (23 bytes) push_str 3.490 ns
80 small-string (23 bytes) push_str_unchecked 1.098 ns
81 -------------------------------------------------------------
82 cache-string (63 bytes) clone 10.170 ns
83 cache-string (63 bytes) try_from_str 25.579 ns
84 cache-string (63 bytes) from_str_truncate 16.977 ns
85 cache-string (63 bytes) from_str_unchecked 17.201 ns
86 cache-string (63 bytes) try_push_str 1.160 ns
87 cache-string (63 bytes) push_str 3.486 ns
88 cache-string (63 bytes) push_str_unchecked 1.115 ns
89 -------------------------------------------------------------
90 max-string (255 bytes) clone 147.410 ns
91 max-string (255 bytes) try_from_str 157.340 ns
92 max-string (255 bytes) from_str_truncate 158.000 ns
93 max-string (255 bytes) from_str_unchecked 158.420 ns
94 max-string (255 bytes) try_push_str 1.167 ns
95 max-string (255 bytes) push_str 4.337 ns
96 max-string (255 bytes) push_str_unchecked 1.103 ns
97 -------------------------------------------------------------
98 string (19 bytes) clone 33.295 ns
99 string (19 bytes) from 32.512 ns
100 string (19 bytes) push str 28.128 ns
101 -------------------------------------------------------------
102 inlinable-string (30 bytes) clone 16.751 ns
103 inlinable-string (30 bytes) from_str 29.310 ns
104 inlinable-string (30 bytes) push_str 2.865 ns
105 -------------------------------------------------------------
106 smallstring crate (20 bytes) clone 60.988 ns
107 smallstring crate (20 bytes) from_str 50.233 ns
108 ```
109
110 ## Licenses
111
112 [MIT](master/license/MIT) and [Apache-2.0](master/license/APACHE)