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