tinyvec/
lib.rs

1#![cfg_attr(not(feature = "std"), no_std)]
2#![forbid(unsafe_code)]
3#![cfg_attr(
4  feature = "nightly_slice_partition_dedup",
5  feature(slice_partition_dedup)
6)]
7#![cfg_attr(
8  feature = "debugger_visualizer",
9  feature(debugger_visualizer),
10  debugger_visualizer(natvis_file = "../debug_metadata/tinyvec.natvis")
11)]
12#![cfg_attr(docs_rs, feature(doc_cfg))]
13#![warn(clippy::missing_inline_in_public_items)]
14#![warn(clippy::must_use_candidate)]
15#![warn(missing_docs)]
16#![allow(clippy::borrow_deref_ref)]
17#![allow(unused_imports)]
18#![allow(clippy::write_with_newline)]
19#![allow(clippy::needless_return)]
20
21//! `tinyvec` provides 100% safe vec-like data structures.
22//!
23//! ## Provided Types
24//! With no features enabled, this crate provides the [`ArrayVec`] type, which
25//! is an array-backed storage. You can push values into the array and pop them
26//! out of the array and so on. If the array is made to overflow it will panic.
27//!
28//! Similarly, there is also a [`SliceVec`] type available, which is a vec-like
29//! that's backed by a slice you provide. You can add and remove elements, but
30//! if you overflow the slice it will panic.
31//!
32//! With the `alloc` feature enabled, the crate also has a [`TinyVec`] type.
33//! This is an enum type which is either an `Inline(ArrayVec)` or a `Heap(Vec)`.
34//! If a `TinyVec` is `Inline` and would overflow it automatically transitions
35//! itself into being `Heap` mode instead of a panic.
36//!
37//! All of this is done with no `unsafe` code within the crate. Technically the
38//! `Vec` type from the standard library uses `unsafe` internally, but *this
39//! crate* introduces no new `unsafe` code into your project.
40//!
41//! The limitation is that the element type of a vec from this crate must
42//! support the [`Default`] trait. This means that this crate isn't suitable for
43//! all situations, but a very surprising number of types do support `Default`.
44//!
45//! ## Other Features
46//! * `grab_spare_slice` lets you get access to the "inactive" portions of an
47//!   ArrayVec.
48//! * `serde` provides a `Serialize` and `Deserialize` implementation for
49//!   [`TinyVec`] and [`ArrayVec`] types, provided the inner item also has an
50//!   implementation.
51//!
52//! ## API
53//! The general goal of the crate is that, as much as possible, the vecs here
54//! should be a "drop in" replacement for the standard library `Vec` type. We
55//! strive to provide all of the `Vec` methods with the same names and
56//! signatures. The exception is that the element type of some methods will have
57//! a `Default` bound that's not part of the normal `Vec` type.
58//!
59//! The vecs here also have a few additional methods that aren't on the `Vec`
60//! type. In this case, the names tend to be fairly long so that they are
61//! unlikely to clash with any future methods added to `Vec`.
62//!
63//! ## Stability
64//! * The `1.0` series of the crate works with Rustc `1.34.0` or later, though
65//!   you still need to have Rustc `1.36.0` to use the `alloc` feature.
66//! * The `2.0` version of the crate is planned for some time after the
67//!   `min_const_generics` stuff becomes stable. This would greatly raise the
68//!   minimum rust version and also allow us to totally eliminate the need for
69//!   the `Array` trait. The actual usage of the crate is not expected to break
70//!   significantly in this transition.
71
72#[allow(unused_imports)]
73use core::{
74  borrow::{Borrow, BorrowMut},
75  cmp::PartialEq,
76  convert::AsMut,
77  default::Default,
78  fmt::{
79    Binary, Debug, Display, Formatter, LowerExp, LowerHex, Octal, Pointer,
80    UpperExp, UpperHex,
81  },
82  hash::{Hash, Hasher},
83  iter::{Extend, FromIterator, FusedIterator, IntoIterator, Iterator},
84  mem::{needs_drop, replace},
85  ops::{Deref, DerefMut, Index, IndexMut, RangeBounds},
86  slice::SliceIndex,
87};
88
89#[cfg(feature = "alloc")]
90#[doc(hidden)] // re-export for macros
91pub extern crate alloc;
92
93mod array;
94pub use array::*;
95
96mod arrayvec;
97pub use arrayvec::*;
98
99mod arrayvec_drain;
100pub use arrayvec_drain::*;
101
102mod slicevec;
103pub use slicevec::*;
104
105#[cfg(feature = "alloc")]
106mod tinyvec;
107#[cfg(feature = "alloc")]
108pub use crate::tinyvec::*;