1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//! Types for inlining small collections for avoiding unnecessary heap allocations.
//!
//! It is common to need to use a vector to store a few small elements and end up using a [`Vec`].
//! This type allocates memory on the heap, and if you're only using a few elements then it is more
//! efficient to use the stack, or inline the vector into whatever structure you're operating on.
//!
//! For example, domain names are typically less than 100 characters and cannot be over 255.
//! Instead of using [`String`], that allocates memory on the heap both on creation and on clone,
//! you can use a [`TinyString::<255>`](TinyString), a 256-byte structure that can store up to 255
//! bytes using inline memory, and cloning is as simple as copying memory:
//!
//! ```
//! use inlined::TinyString;
//! use std::fmt::Write;
//!
//! let mut s = TinyString::<32>::new(); // 32 is the maximum length of the string
//!
//! s.push_str("Hello!");
//! assert_eq!(s.as_str(), "Hello!");
//!
//! let _ = write!(s, " Your number is {}.", 1234);
//! assert_eq!(s.as_str(), "Hello! Your number is 1234.")
//! ```
//!
//! # Provided types
//!
//! This crate contains the following types:
//! - The [`InlineVec`] and [`InlineString`] types are analogous to [`Vec`] and [`String`] from the
//! standard library, but are inlined and have a constant, limited capacity.
//! - The [`TinyVec`] and [`TinyString`] types work much the same way, but use an `u8` for the
//! length instead of an `usize`. This makes them more optimal for passing around, or inlining them
//! into other structs.
//! - The [`CompactVec`] is a type that brings together [`Vec`] and [`TinyVec`], representing a
//! vector that stores up to `N` elements inline, but if more capacity is needed will spill into
//! the heap and allocate memory.
//!
//! Since all of these implement [`Deref`](core::ops::Deref) for either `&[T]` or `&str`, they
//! contain many of the methods you're used to having from [`Vec`] and [`String`].
//!
//! # Specifying the capacity
//!
//! All these types make use of
//! [const generics](https://doc.rust-lang.org/reference/items/generics.html#const-generics) to
//! specify their inline capacity. This means you can choose the maximum amount of elements you
//! want your inlined type to store. Whether you want to store just 3, 100, or even thousands of
//! elements, the same types have got you covered.
// TODO: Remove once API is stabilized
pub use CompactVec;
pub use InlineString;
pub use InlineVec;
pub use TinyString;
pub use TinyVec;