1 #![no_std]
2 #![forbid(unsafe_code)]
3 #![cfg_attr(
4 feature = "nightly_slice_partition_dedup",
5 feature(slice_partition_dedup)
6 )]
7 #![cfg_attr(feature = "nightly_const_generics", allow(incomplete_features))]
8 #![cfg_attr(feature = "nightly_const_generics", feature(const_generics))]
9 #![warn(clippy::missing_inline_in_public_items)]
10 #![warn(clippy::must_use_candidate)]
11 #![warn(missing_docs)]
12
13 //! Programmers can have a little vec, as a treat.
14 //!
15 //! ## What This Is
16 //!
17 //! This crate provides 100% safe code alternatives to both
18 //! [arrayvec](https://docs.rs/arrayvec) and
19 //! [smallvec](https://docs.rs/smallvec).
20 //!
21 //! Being 100% safe means that you have to have some sort of compromise compared
22 //! to the versions using `unsafe`. In this case, the compromise is that the
23 //! element type must implement `Default` to be usable in these vecs. However,
24 //! that still allows you to use [quite a few
25 //! types](https://doc.rust-lang.org/std/default/trait.Default.html#implementors),
26 //! so I think that you'll find these vecs useful in many cases.
27 //!
28 //! * [`ArrayVec`](ArrayVec) is an array-backed vec-like structure with a fixed
29 //! capacity. If you try to grow the length past the array's capacity it will
30 //! error or panic (depending on the method used).
31 //! * (`alloc` feature) [`TinyVec`](TinyVec) is an enum that's either an
32 //! "Inline" `ArrayVec` or a "Heap" `Vec`. If it's Inline and you try to grow
33 //! the `ArrayVec` beyond its array capacity it will quietly transition into
34 //! Heap mode and then continue the operation.
35 //!
36 //! ## Crate Goals
37 //!
38 //! 1) The crate is 100% safe code. Not just a safe API, there are also no
39 //! `unsafe` internals. `#![forbid(unsafe_code)]`.
40 //! 2) No required dependencies.
41 //! * We might provide optional dependencies for extra functionality (eg:
42 //! `serde` compatibility).
43 //! 3) The intended API is that, _as much as possible_, these types are
44 //! essentially a "drop-in" replacement for the standard [`Vec`](Vec::<T>)
45 //! type.
46 //! * Stable `Vec` methods that the vecs here also have should be the same
47 //! general signature.
48 //! * Unstable `Vec` methods are sometimes provided via a crate feature, but
49 //! if so they also require a Nightly compiler.
50 //! * Some methods are provided that _are not_ part of the `Vec` type, such
51 //! as additional constructor methods. In this case, the names are rather
52 //! long and whimsical in the hopes that they don't clash with any
53 //! possible future methods of `Vec`.
54 //! * If, in the future, `Vec` stabilizes a method that clashes with an
55 //! existing extra method here then we'll simply be forced to release a
56 //! 2.y.z version. Not the end of the world.
57 //! * Some methods of `Vec` are simply inappropriate and will not be
58 //! implemented here. For example, `ArrayVec` cannot possibly implement
59 //! [`from_raw_parts`](https://doc.rust-lang.org/std/vec/struct.Vec.html#method.from_raw_parts).
60
61 use core::{
62 borrow::{Borrow, BorrowMut},
63 cmp::PartialEq,
64 convert::AsMut,
65 default::Default,
66 fmt::{
67 Binary, Debug, Display, Formatter, LowerExp, LowerHex, Octal, Pointer,
68 UpperExp, UpperHex,
69 },
70 hash::{Hash, Hasher},
71 iter::{Extend, FromIterator, FusedIterator, IntoIterator, Iterator},
72 mem::{needs_drop, replace},
73 ops::{Deref, DerefMut, Index, IndexMut, RangeBounds},
74 slice::SliceIndex,
75 };
76
77 #[cfg(feature = "alloc")]
78 #[doc(hidden)] // re-export for macros
79 pub extern crate alloc;
80
81 mod array;
82 pub use array::*;
83
84 mod arrayvec;
85 pub use arrayvec::*;
86
87 #[cfg(feature = "alloc")]
88 mod tinyvec;
89 #[cfg(feature = "alloc")]
90 pub use crate::tinyvec::*;
91
92 // TODO MSRV(1.40.0): Just call the normal `core::mem::take`
93 #[inline(always)]
take<T: Default>(from: &mut T) -> T94 fn take<T: Default>(from: &mut T) -> T {
95 replace(from, T::default())
96 }
97