Skip to main content

tinyvec/
array.rs

1/// A trait for types that are an array.
2///
3/// An "array", for our purposes, has the following properties:
4/// * Owns some number of elements.
5/// * The element type can be generic, but must implement [`Default`].
6/// * The capacity is fixed at compile time, based on the implementing type.
7/// * You can get a shared or mutable slice to the elements.
8///
9/// You are generally **not** expected to need to implement this yourself. It is
10/// already implemented for all array lengths.
11///
12/// ## Safety Reminder
13///
14/// Just a reminder: this trait is 100% safe, which means that `unsafe` code
15/// **must not** rely on an instance of this trait being correct.
16pub trait Array {
17  /// The type of the items in the thing.
18  type Item: Default;
19
20  /// The number of slots in the thing.
21  const CAPACITY: usize;
22
23  /// Gives a shared slice over the whole thing.
24  ///
25  /// A correct implementation will return a slice with a length equal to the
26  /// `CAPACITY` value.
27  fn as_slice(&self) -> &[Self::Item];
28
29  /// Gives a unique slice over the whole thing.
30  ///
31  /// A correct implementation will return a slice with a length equal to the
32  /// `CAPACITY` value.
33  fn as_slice_mut(&mut self) -> &mut [Self::Item];
34
35  /// Create a default-initialized instance of ourself, similar to the
36  /// [`Default`] trait, but implemented for the same range of sizes as
37  /// [`Array`].
38  fn default() -> Self;
39}
40
41mod const_generic_impl;
42
43#[cfg(feature = "generic-array")]
44mod generic_array_impl;